简体   繁体   English

如何使用存储过程从 SQL 服务器数据库中提取数据

[英]How to pull data from SQL Server database using a stored procedure

I am tasked to create a stored procedure for a publisher application to retrieve employee data from our SQL Server.我的任务是为发布者应用程序创建一个存储过程,以从我们的 SQL 服务器检索员工数据。

The aim: We have EmpTable (lets call this the source).目标:我们有 EmpTable(让我们称之为源)。 We also have EmpData table.我们还有 EmpData 表。 It has a column called 'Status' with a default value: 'UNPROCESSED'.它有一个名为“状态”的列,默认值为:“未处理”。 Our aim is to create a SP(lets call this SP1) so that: it polls all the data from the EmpTable in batches of 100 rows.我们的目标是创建一个 SP(让我们称之为 SP1),以便:它以 100 行为一组轮询 EmpTable 中的所有数据。 It should then continue polling data from this table until SP returns zero records.然后它应该继续从该表中轮询数据,直到 SP 返回零记录。 Once the above completed processing, another SP(lets call this SP2) is created/called to change the status to 'COMPLETED'.一旦完成上述处理,就会创建/调用另一个 SP(让我们称之为 SP2)以将状态更改为“已完成”。 If the record processing is failed permanently in the app, that is due to errors such as schema or validation errors (none retryable), SP2 to change the status to FAILED.如果记录处理在应用程序中永久失败,即由于架构或验证错误(不可重试)等错误,SP2 将状态更改为 FAILED。 The results is then populated in the EmpData table We aim to run this batch job once a day.然后将结果填充到 EmpData 表中。我们的目标是每天运行一次此批处理作业。 Hope this make sense希望这是有道理的

I am wondering how this can be queried.我想知道如何查询这个。 I started the query:我开始查询:

DECLARE @id_check INT
DECLARE @batchSize INT
DECLARE @results INT

SET @results = 1 --stores the row count after each successful batch
SET @batchSize = 100 --How many rows you want to operate on each batch
SET @id_check = 0 --current batch 

-- when 0 rows returned, exit the loop
WHILE (@results > 0) 
BEGIN
   SELECT * -- This is just an example to generalize result for now
   FROM empdata

   SET @results = @@ROWCOUNT

   -- next batch
   SET @id_check = @id_check + @batchSize
END

The result I am aiming for is to return batch 1 to return 100 values, then batch 2 to return the next 100 and so on我的目标是返回第 1 批返回 100 个值,然后第 2 批返回下一个 100,依此类推

Any help would be appreciated!任何帮助,将不胜感激!

Unfortunately without clear requirements its hard to assist you.不幸的是,如果没有明确的要求,很难为您提供帮助。

However, if what you want is to pull all the matching records, but in batches (which doesn't make a lot of sense), then you can use the following stored procedure.但是,如果您想要的是提取所有匹配的记录,但是分批(这没有多大意义),那么您可以使用以下存储过程。

It will return the first 100 rows which meet whatever criteria you have.它将返回满足您拥有的任何条件的前 100 行。 Then when they are loaded in your app, you call the SP again, passing in the maximum ID you received in the previous recordset.然后,当它们被加载到您的应用程序中时,您再次调用 SP,并传入您在前一个记录集中收到的最大 ID。

Your app continues to call this SP until no rows are returned.您的应用程序会继续调用此 SP,直到没有返回任何行。

CREATE OR ALTER PROCEDURE dbo.MyTestProcedure1
(
    -- Pass in the last ID processed
    @LastIdChecked int = 0
)
AS
BEGIN
    SET NOCOUNT, XACT_ABORT ON;

    SET @LastIdChecked = COALESCE(@LastIdChecked,0);

    DECLARE @BatchSize int = 100;

    -- Return the next @BatchSize records after the last Id checked
    SELECT TOP(@BatchSize) *
    FROM dbo.EmpTable
    WHERE Id > @LastIdChecked
    ORDER BY Id ASC;

    RETURN 0;
END;

A more expected process would be, you use the SP to pull your first 100 records, then you fully process them in your app.一个更令人期待的过程是,您使用 SP 提取前 100 条记录,然后在您的应用程序中完全处理它们。 Then you call the SP again, and the SP knows which records have been processed and filters them out in the WHERE clause.然后你再次调用 SP,SP 知道哪些记录已经被处理,并在WHERE子句中将它们过滤掉。 Then you run this until completion.然后你运行它直到完成。

That solution would look like this:该解决方案如下所示:

CREATE OR ALTER PROCEDURE dbo.MyTestProcedure2
AS
BEGIN
    SET NOCOUNT, XACT_ABORT ON;

    DECLARE @BatchSize int = 100;

    -- Return the next @BatchSize records
    SELECT TOP(@BatchSize) *
    FROM dbo.EmpTable T
    WHERE {Record is unprocessed}
    -- e.g. if you are expeceting 1 record a day per dbo.EmpTable record.
    -- WHERE NOT EXISTS (
    --    SELECT 1
    --    FROM dbo.EmpData D
    --    WHERE T.EmpId = D.EmpId AND D.Date = CONVERT(date, GETDATE())
    --)
    ORDER BY Id ASC;

    RETURN 0;
END;

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

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