简体   繁体   中英

Create SQL Server stored procedure with stock data

I am trying to turn some code I have from SQL Server into a stored procedure with parameters that I can pass through but I am not sure how to do this. I want the 4 letter stock symbol of the URL to be a variable so that I can pass through different symbols, I also need this code to work as a stored procedure.

GOOG is where I need the variable.

http://finance.yahoo.com/webservice/v1/symbols/GOOG/quote '

--RSS FEED
DECLARE @docHandle   INT;
DECLARE @xmlData     XML;
DECLARE @URL         NVARCHAR(255);
DECLARE @file        NVARCHAR(255);
DECLARE @cmd         NVARCHAR(255);
DECLARE @sql         NVARCHAR(255);
DECLARE @tXML        TABLE(data XML);

SET @URL  = 'http://finance.yahoo.com/webservice/v1/symbols/GLUU/quote';
SET @file = 'c:\temp\quotes.xml';

-- Downloading the data
SET @cmd = 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell (new-object System.Net.WebClient).DownloadFile( ''' + @URL + ''',''' + @file + '''  )'
EXEC master.dbo.xp_cmdshell @cmd, no_output

-- Loading the Downloaded File into the XML variable
SET @sql = 'SELECT BulkColumn FROM OPENROWSET( BULK ''' + @file + ''', SINGLE_BLOB ) AS a'
INSERT @tXML EXEC(@sql);
SELECT @xmlData = data from @tXML 

-- Preparing the Relational Table from the XML variable
EXEC sp_xml_preparedocument @docHandle OUTPUT, @xmlData;

INSERT INTO tblstockdata ([Name], [Price], [Symbol], [TS], [Type], [Volume])
SELECT * FROM OPENXML(@docHandle, N'//list/resources/resource')  
  WITH ( Name    VARCHAR(10) 'field[@name="name"]',
         Price   VARCHAR(10) 'field[@name="price"]',
         Symbol  VARCHAR(10) 'field[@name="symbol"]',
         TS      VARCHAR(10) 'field[@name="ts"]',
         Type    VARCHAR(10) 'field[@name="type"]',
         Volume  VARCHAR(10) 'field[@name="volume"]');

EXEC sp_xml_removedocument @docHandle;

Thanks!

:)

You have working SQL, which is the tough part. Now bracket your working SQL with

CREATE PROCEDURE <schema>.<Name>(
   @param   <type>
) as begin

and

end

and you should be good to go. For starters you can use dbo as the value of <schema> until you need to use a non-default schema. It is recommended that you not use the prefix "sp" or "xp" for your procedure name as these are used to identify system and extended procedures by SQL Server.

If you need additional parameters they can be added to the parameter list using a comma as the delimiter.

Below is a stored procedure example gleaned from your code that takes the needed symbol parameter. Here, I used the XML data type methods rather than sp_xml_preparedocument. If multiple users might call this stored procedure at the same time, I suggest you generate a unique name for the file an delete afterwards. You might consider a CLR stored procedure to avoid both the temporary file and OPENROWSET ugliness.

CREATE PROC dbo.usp_GetEquityQuote
    @symbol varchar(10)
AS

DECLARE @URL         NVARCHAR(255);
DECLARE @file        NVARCHAR(255);
DECLARE @cmd         NVARCHAR(255);
DECLARE @sql         NVARCHAR(255);
DECLARE @tXML        TABLE(data XML);

SET @URL  = 'http://finance.yahoo.com/webservice/v1/symbols/' + @symbol + '/quote';
SET @file = 'c:\temp\quotes.xml';

-- Downloading the data
SET @cmd = 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell (new-object System.Net.WebClient).DownloadFile( ''' + @URL + ''',''' + @file + '''  )';
EXEC master.dbo.xp_cmdshell @cmd, no_output;

-- Loading the Downloaded File into the XML variable
SET @sql = 'SELECT BulkColumn FROM OPENROWSET( BULK ''' + @file + ''', SINGLE_BLOB ) AS a';
INSERT @tXML EXEC(@sql);
    SELECT data from @tXML ;

SELECT
     resources.resource.value('field[@name="name"][1]', 'varchar(10)') AS name
     ,resources.resource.value('field[@name="price"][1]', 'decimal(18,4)') AS price
     ,resources.resource.value('field[@name="symbol"][1]', 'varchar(10)') AS symbol
     ,resources.resource.value('field[@name="ts"][1]', 'bigint') AS ts
     ,resources.resource.value('field[@name="type"][1]', 'varchar(10)') AS type
     ,resources.resource.value('field[@name="volume"][1]', 'bigint') AS volume
FROM (SELECT data FROM @tXML) AS tXML(data)
CROSS APPLY data.nodes('/list/resources/resource') AS resources(resource);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM