从最后执行的行开始执行。 [英] Start execution from the last executed row.

查看:95
本文介绍了从最后执行的行开始执行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

点网络代码正在调用存储过程,该存储过程正在从一个表中读取数据并插入到其他表中。



问题:

我想读几行顶行(比如10行)并将这些记录插入另一个表控件应该转移到代码。

当代码再次调用这个存储过程时读取数据应该从下一行开始(即第11行)



我们有什么办法可以获得最后一行读取的引用并将其返回给代码。因此,如果代码使用最后执行的行的引用作为参数调用该存储过程,则数据的读取应该从后续行开始。



我有什么尝试过:



我通过实现游标尝试了它。但是这不起作用。

解决方案

我发布了一个更全面的回复来包装它并为了遇到其他任何人的利益......


在这种情况下,有几种方法可以获得行号,但遗憾的是你受到SQL的一个特性的约束,其中

Quote:

窗口函数只能出现在SELECT或ORDER BY子句中。

这意味着你不能做这样的事情:

  SELECT 国家/地区,CountryCode 
FROM 国家/地区
WHERE ROW_NUMBER() OVER ORDER BY CountryCode)> 4



要解决此问题,您可以使用公用表表达式,或者为了更灵活地使用临时表或表变量例如

  CREATE   PROCEDURE  [dbo]。[SP3] 
< span class =code-sdkkeyword> @ param int
AS
BEGIN
SET NOCOUNT ON ;
DECLARE @ T TABLE (rn INT ,国家 varchar 30 ),CountryCode int

INSERT INTO @ T
SELECT ROW_NUMBER() OVER ORDER BY CountryCode),Country,CountryCode
FROM 国家

SELECT TOP 5 国家/地区,CountryCode 来自 @ T WHERE rn> @ param

RETURN SELECT MAX(rn) FROM @ T WHERE rn> @ param AND rn< = @param + 5
END
< span class =code-keyword> GO



你甚至可以在该表变量上有一个标识列,并完全省略ROW_NUMBER函数(尽管你会仍然需要ORDER BY)例如

 创建 程序 [dbo]。[SP2] 
@ param int
< span class =code-keyword> AS
BEGIN
SET NOCOUNT ON ;
DECLARE @ T TABLE (rn INT IDENTITY 1 ,< span class =code-digit> 1 ),国家 varchar 30 ), CountryCode int

INSERT INTO @ T
SELECT 国家/地区,CountryCode
FROM 国家/地区
ORDER BY 国家/地区

SELECT TOP 5 Country,CountryCode 来自 @ T WHERE rn> @ param

RETURN SELECT MAX(rn) FROM @ T WHERE rn> @ param AND rn< = @param + 5
END
< span class =code-keyword> GO





然后您可以像这样调用存储过程:

  DECLARE   @ nextValue   int  =  0  
EXEC @ nextValue = dbo.SP2 @ nextValue
exec <温泉n class =code-sdkkeyword> @ nextValue = dbo.SP2 @ nextValue
EXEC @ nextValue = dbo.SP2 @ nextValue
exec @ nextValue = dbo.SP2 @ nextValue

对于我的数据(见下文),我得到的查询返回5条记录,5条记录,4条记录,然后是0条记录。

顺便说一下,这就是我使用

  WHERE  rn>  @ param   AND  rn< =  @ param  +  5  

来自我的两个SP的RETURN值...记录数可能不会精确地除以5(或者你的数字是多少)选择)。如果其他用户在运行这些查询时向表中添加数据,则需要确保使用

  RETURN <不小心错过任何行/ span>  @ param  +  5  





为了完整性,这里是我用来测试这些例子的数据:

  CREATE   [dbo]。[Country](
[id] [ int ] < span class =code-keyword> NULL ,
[Country] [ varchar ]( 30 NULL
[CountryCode] [ int ] NULL
[RatePerMin] [ decimal ]( 15 2 NULL
ON [ PRIMARY ]
INSERT INTO 国家 VALUES
248 ' 尼泊尔' 97 3 20 ),
1 ' 阿富汗' 93 4 00 ),
2 ' 阿尔巴尼亚' 355 3 50 ),
3 ' Algeria' 213 2 20 ),
4 ' American Samoa' 1684 2 00 ),
5 ' Andorra' 376 4 20 ),
6 ' 安哥拉 244 4 20 ),
7 ' 安圭拉' 1264 4 50 ),
8 ' 南极洲/诺福克岛' 672 5 00 ),
9 ' 安提瓜和巴布达' 1268 4 50 ),
10 ' 苏丹' 249 1 40 ),
11 ,< span class =code-string>' Q. atar' 974 1 50 ),
12 ' 埃及' 20 1 60 ),
13 ' 斯里兰卡' 94 1 70



请注意,如果要将每次调用时提取的行数设置为存储的参数之一过程然后你将不得不在SP的主体内动态生成SQL查询 - 本文解释了构建动态SQL存储过程 [ ^ ]


dot net code is calling a stored procedure and that stored procedure is reading data from one table and inserting into other table.

Problem:
I want after reading few top rows (let's say 10 rows) and inserting those records into another table The control should get transfer to the code.
when code call this stored procedure again the reading of the data should begin from the next row (i.e 11th row)

Is there any way by which we can get the reference of last read row and returning it to the code. so if the code call that stored procedure with the reference of last executed row as parameter the reading of data should begin from the subsequent row.

What I have tried:

I have tried it by implementing cursor. But that is not working.

解决方案

I'm posting a fuller response to wrap this up and for the benefit of anyone else coming across this...

There are a couple of ways you can get a "row number" in this instance, but unfortunately you are constrained by a feature of SQL where

Quote:

Windowed functions can only appear in the SELECT or ORDER BY clauses.

Which means you can't do something like this:

SELECT Country, CountryCode
FROM Country
WHERE ROW_NUMBER() OVER (ORDER BY CountryCode) > 4


To overcome the issue you could use a Common Table expression, or for more flexibility a temporary table or a table variable e.g.

CREATE PROCEDURE [dbo].[SP3]
@param int
AS
BEGIN
	SET NOCOUNT ON;
	DECLARE @T TABLE (rn INT, Country varchar(30), CountryCode int)

	INSERT INTO @T
		SELECT ROW_NUMBER() OVER (ORDER BY CountryCode), Country, CountryCode
		FROM Country 

	SELECT TOP 5 Country, CountryCode from @T WHERE rn > @param

	RETURN (SELECT MAX(rn) FROM @T WHERE rn > @param AND rn <= @param + 5)
END
GO


You could even have an identity column on that table variable and omit the ROW_NUMBER function altogether (although you will still need the ORDER BY) E.g.

CREATE PROCEDURE [dbo].[SP2]
@param int
AS
BEGIN
	SET NOCOUNT ON;
	DECLARE @T TABLE (rn INT IDENTITY(1,1), Country varchar(30), CountryCode int)

	INSERT INTO @T
		SELECT Country, CountryCode
		FROM Country 
		ORDER BY Country

	SELECT TOP 5 Country, CountryCode from @T WHERE rn > @param

	RETURN (SELECT MAX(rn) FROM @T WHERE rn > @param AND rn <= @param + 5)
END
GO



You can then call the Stored Procedure like this:

DECLARE @nextValue int = 0
EXEC @nextValue = dbo.SP2 @nextValue
exec @nextValue = dbo.SP2 @nextValue
EXEC @nextValue = dbo.SP2 @nextValue
exec @nextValue = dbo.SP2 @nextValue

For my data (see below) I get queries returning 5 records, 5 records, 4 records then 0 records.
Incidentally that is why I used the

WHERE rn > @param AND rn <= @param + 5

for the RETURN value from both my SPs ... the number of records may not divide exactly by 5 (or whatever number you choose). If other users are adding data to the table when you are running these queries you want to make sure you don't accidentally miss any rows by using

RETURN @param + 5



For completeness here is the data I used to test these examples:

CREATE TABLE [dbo].[Country](
	[id] [int] NULL,
	[Country] [varchar](30) NULL,
	[CountryCode] [int] NULL,
	[RatePerMin] [decimal](15, 2) NULL
) ON [PRIMARY]
INSERT INTO Country VALUES
(248,'Nepal'	                ,97  ,3.20),
(1,	 'Afghanistan'	            ,93  ,4.00),
(2,	 'Albania'	                ,355 ,3.50),
(3,	 'Algeria'	                ,213 ,2.20),
(4,	 'American Samoa'	        ,1684,2.00),
(5,	 'Andorra'	                ,376 ,4.20),
(6,	 'Angola'	                ,244 ,4.20),
(7,	 'Anguilla'	                ,1264,4.50),
(8,	 'Antarctica/Norfolk Island',672 ,5.00),
(9,	 'Antigua and Barbuda'	    ,1268,4.50),
(10, 'Sudan'	                ,249 ,1.40),
(11, 'Qatar'	                ,974 ,1.50),
(12, 'Egypt'	                ,20  , 1.60),
(13, 'Sri Lanka'	            ,94  , 1.70)


Note that if you want to make the number of rows that you are extracting on each call one of the parameters to the stored procedure then you will have to generate the SQL query dynamically within the body of the SP - this article explains how Building Dynamic SQL In a Stored Procedure[^]


这篇关于从最后执行的行开始执行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆