简体   繁体   English

使用库存数据创建SQL Server存储过程

[英]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. 我试图将我从SQL Server获得的一些代码转换为具有可以传递的参数的存储过程,但是我不确定如何执行此操作。 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. 我希望URL的4个字母的股票符号是一个变量,以便我可以传递不同的符号,我还需要将此代码用作存储过程。

GOOG is where I need the variable. GOOG是我需要变量的地方。

http://finance.yahoo.com/webservice/v1/symbols/GOOG/quote ' 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. 您拥有有效的SQL,这是困难的部分。 Now bracket your working SQL with 现在将您的工作SQL与

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. 对于初学者,可以将dbo用作<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. 建议不要为过程名称使用前缀“ sp”或“ xp”,因为这些名称用于识别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. 下面是从您的代码中收集的存储过程示例,该示例采用了所需的symbol参数。 Here, I used the XML data type methods rather than sp_xml_preparedocument. 在这里,我使用XML数据类型方法而不是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. 您可能会考虑使用CLR存储过程来避免临时文件和OPENROWSET丑陋。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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