Published on

May 15, 2020

Scheduling Stock Downloads in SQL Server

When it comes to assessing stocks before buying or evaluating the performance of stocks you already own, it’s crucial to have access to regular stock data. In this article, we will explore how to schedule stock downloads using SQL Server.

Downloading Stocks

To download stock history, we will utilize SQL Server’s built-in functionality along with a few helper packages. These packages will ensure that the required components are installed and handle the downloading process seamlessly.

Here is an example of how to load the necessary packages:

USE [YourDatabase]
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DownloadStocks]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE [dbo].[DownloadStocks]
AS
BEGIN
    -- Your code here
END
'
END
GO

EXEC dbo.sp_executesql @statement = N'
ALTER PROCEDURE [dbo].[DownloadStocks]
AS
BEGIN
    -- Your code here
END
'
GO

Loading Configuration Parameters

All the configuration parameters can be stored in a table or a configuration file. In this example, we will use a table named “StockConfig” with columns for stock symbols, start date, end date, file path, and append option.

Here is an example of how to load the configuration parameters:

SELECT * FROM [YourDatabase].[dbo].[StockConfig]

Saving the Stocks History

Once the configuration parameters are loaded, we can proceed with downloading the stock data and saving it to a file. We will use SQL Server’s built-in functions to retrieve the stock data and the BULK INSERT statement to write it to a CSV file.

Here is an example of how to download and save the stocks history:

USE [YourDatabase]
GO

ALTER PROCEDURE [dbo].[DownloadStocks]
AS
BEGIN
    DECLARE @FromDate DATE
    DECLARE @ToDate DATE
    DECLARE @FilePath NVARCHAR(100)
    DECLARE @Append BIT

    -- Load configuration parameters into variables
    SELECT @FromDate = StartDate, @ToDate = EndDate, @FilePath = FilePath, @Append = Append
    FROM [YourDatabase].[dbo].[StockConfig]

    -- Download stock data
    SELECT *
    INTO #StockData
    FROM [YourDatabase].[dbo].[StockData]
    WHERE Date BETWEEN @FromDate AND @ToDate

    -- Save stock data to CSV file
    DECLARE @Sql NVARCHAR(MAX)
    SET @Sql = 'BULK INSERT [YourDatabase].[dbo].[StockDataCSV]
                FROM ''' + @FilePath + '''
                WITH (FORMAT = ''CSV'', FIELDTERMINATOR = '','', ROWTERMINATOR = ''\n'', FIRSTROW = 2'

    IF @Append = 1
        SET @Sql = @Sql + ', APPEND)'

    EXEC sp_executesql @Sql

    DROP TABLE #StockData
END
GO

Scheduling Stock Downloads

To automate the stock download process, we can use SQL Server Agent to schedule the execution of the stored procedure. This allows us to specify the frequency and time of the downloads.

Here is an example of how to schedule the stock downloads:

USE [YourDatabase]
GO

EXEC msdb.dbo.sp_add_job
    @job_name = N'DownloadStocksJob',
    @enabled = 1,
    @description = N'Job to download stock data',
    @category_name = N'Database Maintenance'

EXEC msdb.dbo.sp_add_jobstep
    @job_name = N'DownloadStocksJob',
    @step_name = N'DownloadStocksStep',
    @subsystem = N'TSQL',
    @command = N'EXEC [dbo].[DownloadStocks]',
    @database_name = N'YourDatabase'

EXEC msdb.dbo.sp_add_schedule
    @schedule_name = N'DailySchedule',
    @enabled = 1,
    @freq_type = 4,
    @freq_interval = 1,
    @active_start_time = 180000

EXEC msdb.dbo.sp_attach_schedule
    @job_name = N'DownloadStocksJob',
    @schedule_name = N'DailySchedule'

EXEC msdb.dbo.sp_add_jobserver
    @job_name = N'DownloadStocksJob',
    @server_name = N'(local)'

GO

With this setup, the stored procedure “DownloadStocks” will be executed daily at 6:00 PM, downloading the latest stock data and saving it to the specified file.

Conclusion

Scheduling stock downloads in SQL Server allows you to automate the process of retrieving and saving stock data. By utilizing SQL Server’s built-in functionality and scheduling capabilities, you can ensure that you always have up-to-date stock information at your fingertips.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.