Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. USE [EuroInvestorStockDB]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[ParseXMLClosePrice ] Script Date: 2/22/2017 1:43:19 PM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8.  
  9.  
  10.  
  11. ALTER PROCEDURE [dbo].[ParseXMLClosePrice ]
  12. @XML XML
  13. WITH RECOMPILE
  14. AS
  15. BEGIN
  16. SET NOCOUNT ON;
  17. declare @sTime datetime
  18. set @stime = getdate()
  19.  
  20. DECLARE @TempStock TABLE
  21. (
  22. Date datetime,
  23. ExchangeId integer,
  24. InstrumentID int,
  25. OpenPrice decimal(11,4),
  26. High decimal(11,4),
  27. Low decimal(11,4),
  28. ClosePrice decimal(11,4),
  29. TotVol bigint,
  30. Multiplier integer,
  31. CombinedSymbol NVARCHAR(100),
  32. Currency NVARCHAR(3),
  33. Symbol VarChar(20),
  34. PrevClose decimal(11,4),
  35. CloseDate datetime,
  36. change decimal(11,4)
  37. );
  38.  
  39.  
  40.  
  41. INSERT INTO
  42. @TempStock
  43. (Date,OpenPrice,High,Low,CombinedSymbol,ClosePrice,Currency,TotVol,CloseDate,change,PrevClose)
  44. SELECT
  45. Stock.value('Date[1]','datetime') AS Date,
  46. Stock.value('Open[1]','decimal(11,4)') AS OpenPrice,
  47. Stock.value('High[1]','decimal(11,4)') AS High,
  48. Stock.value('Low[1]','decimal(11,4)') AS Low,
  49. Stock.value('Security[1]/Symbol[1]','NVARCHAR(100)') AS CombinedSymbol,
  50. Stock.value('Last[1]','decimal(11,4)') AS ClosePrice,
  51. Stock.value('Currency[1]','NVARCHAR(3)') AS Currency,
  52. Stock.value('Volume[1]','bigint') AS TotVol,
  53. Stock.value('EndOfDayPriceDate[1]','datetime') AS CloseDate,
  54. Stock.value('ChangeFromLastClose[1]','decimal(11,4)') AS change,
  55. Stock.value('LastClose[1]','decimal(11,4)') AS PrevClose
  56. FROM @XML.nodes('EndOfDayQuote')Catalog(Stock)
  57.  
  58. print 'time to finish XML read -->' + convert( varchar(50) ,(cast (getdate()-@stime as float )*24*60*60 ))
  59.  
  60. --Get the Symbol and Exchange
  61. update @Tempstock
  62. SET
  63. -- Go from the beginning to the last '.'
  64. Symbol = substring(CombinedSymbol, 1, LEN(combinedSymbol)- charindex('.', reverse(CombinedSymbol))),
  65. ExchangeId = ( select [ExchangeID]
  66. FROM [EuroInvestorStockDB].[dbo].[MicExchange] with(nolock)
  67. -- Go from the last '.' till the end
  68. where [Mic]= substring(combinedSymbol, LEN(combinedSymbol)- charindex('.', reverse(combinedSymbol))+2, LEN(combinedSymbol)))
  69.  
  70. print 'time to finish Reading the symbol -->' + convert( varchar(50) ,(cast (getdate()-@stime as float )*24*60*60 ))
  71. --take care of E: case
  72. update @Tempstock
  73. SET Symbol = ( IIF (EXISTS (select Top 1 [Symbol] from Instrument where Symbol = CONCAT('E:',SI.Symbol) and ExchangeId=SI.ExchangeId ),CONCAT('E:',SI.Symbol),SI.Symbol))
  74. from @TempStock AS SI
  75. -- Take care of conversion
  76. print 'time to finish the E: case -->' + convert( varchar(50) ,(cast (getdate()-@stime as float )*24*60*60 ))
  77.  
  78.  
  79.  
  80. UPDATE @Tempstock
  81. SET
  82. SI.Change = RAN.Change,
  83. SI.InstrumentID = RAN.ID,
  84. SI.Multiplier = IIF(RAN.Currency = 'GBX',100,1)
  85. from @TempStock AS SI
  86. INNER JOIN Instrument AS RAN with(nolock)
  87. ON SI.Symbol=RAN.Symbol and SI.Exchangeid=RAN.Exchangeid
  88.  
  89. print 'time to finish the GBX conversion -->' + convert( varchar(50) ,(cast (getdate()-@stime as float )*24*60*60 ))
  90.  
  91.  
  92. -- Insert into Closeprice
  93.  
  94. INSERT INTO
  95. Close_Price (Date, ExchangeID, InstrumentID, OpenPrice,High,Low,ClosePrice,TotVol)
  96. SELECT
  97. IP.Date AS Date,
  98. IP.ExchangeID AS ExchangeID,
  99. IP.InstrumentID as InstrumentID,
  100. IP.OpenPrice* IP.Multiplier as OpenPrice,
  101. IP.High * IP.Multiplier as High,
  102. IP.Low * IP.Multiplier as Low,
  103. Ip.ClosePrice * IP.Multiplier as ClosePrice,
  104. IIF ( IP.TotVol > 2147483647, 2147483647, CONVERT(INT,IP.TotVol)) as TotVol
  105. FROM @TempStock AS IP
  106. IF @@RowCount = 0
  107. BEGIN
  108. print 'Did not update Close_Price Table '
  109. END
  110.  
  111. print 'time to finish insertion to CLosePRICE -->' + convert( varchar(50) ,(cast (getdate()-@stime as float )*24*60*60 ))
  112.  
  113. -- Update the instrument table upon market closing
  114. UPDATE SI
  115. SET
  116. Timestamp = GETDATE(),
  117. SI.PrevClose = ISNULL(RAN.PrevClose*RAN.Multiplier, SI.PrevClose),
  118. -- Upon closing, the last price is close price
  119. SI.Last = RAN.ClosePrice*RAN.Multiplier,
  120. -- Setting yesterday volume
  121. SI.YesterdayVol = IIF ( RAN.TotVol > 2147483647, 2147483647, CONVERT(INT,RAN.TotVol))
  122. from Instrument AS SI
  123. INNER JOIN @TempStock AS RAN
  124. ON SI.Id=RAN.InstrumentID
  125. IF @@RowCount = 0
  126. BEGIN
  127. print 'Did not update Instrument Table '
  128. END
  129. Select * from @TempStock
  130. print 'time to finish -->' + convert( varchar(50) ,(cast (getdate()-@stime as float )*24*60*60 ))
  131.  
  132. RETURN;
  133. END;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement