在T-SQL中,我希望通过逐行读取来遍历存储过程中的表。
DECLARE @IMAX INT,
@ICOUNT INT,
@INTERFACE_ID_36 INT,
@INTERFACE_ID_38 INT
SELECT * FROM INTERFACE_36_DATA
SET @IMAX = @@ROWCOUNT
SET @ICOUNT = 1
WHILE(@ICOUNT <= @IMAX)
BEGIN
SELECT @INTERFACE_ID_36 = Interface_ID
FROM INTERFACE_36_DATA
WHERE ROW_NUMBER() OVER (ORDER BY id) AS = @ICOUNT --syntax error here
IF @INTERFACE_ID_36 = 10
SET @INTERFACE_ID_38 = 0
ELSE IF @INTERFACE_ID_36 =
答案 0 :(得分:1)
我建议使用游标重写它,如下所示:
DECLARE @IMAX INT,
@ICOUNT INT,
@INTERFACE_ID_36 INT,
@INTERFACE_ID_38 INT
DECLARE db_cursor CURSOR FOR
SELECT Interface_ID FROM INTERFACE_36_DATA
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @INTERFACE_ID_36
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @INTERFACE_ID_36
-- All your other selects
FETCH NEXT FROM db_cursor INTO @INTERFACE_ID_36
END
CLOSE db_cursor
DEALLOCATE db_cursor
答案 1 :(得分:0)
更改您的查询,如下所示。 因为我们不能在where条件中使用row_number。 Check the link here for more descr
WITH cte
AS ( SELECT Interface_ID ,
ROW_NUMBER() OVER ( ORDER BY id ) AS RN
FROM INTERFACE_36_DATA
)
SELECT @INTERFACE_ID_36 = Interface_ID
FROM cte
WHERE RN = @ICOUNT
答案 2 :(得分:0)
您可以尝试这样做:
DECLARE @IMAX INT,
@ICOUNT INT,
@INTERFACE_ID_36 INT,
@INTERFACE_ID_38 INT
select *,ROW_NUMBER() OVER (ORDER BY Interface_ID) RowId into #tmptbl from INTERFACE_36_DATA
Declare @I BIGINT = 1
declare @count BIGINT = (select Count(*) from #tmptbl)
while(@I < = @count)
begin
SELECT @INTERFACE_ID_36 = Interface_ID
FROM #tmptbl
where RowId = @I
--- do yor additional stuff
set @I =@I +1
end