我正在尝试使用游标移动创建一个列,其中所有部分都等于之前的字段。例如
| column1 | column 2 |
| 1 | a |
| 2 | b |
| 3 | c |
would to go...
| column1 | column2 | column3 |
| 1 | a | b |
| 2 | b | c |
| 3 | c | NULL |
所以为了做到这一点,我尝试使用基于最后一个fetch语句的游标和更新语句,如下所示:
DECLARE myCursor1 CURSOR READ_ONLY
FOR
SELECT lname AS 'lnamerecoff'
FROM testingThis
ORDER BY lname
OPEN myCursor1
DECLARE @previous char(15)
DECLARE @new char(15)
SET @previous = FETCH NEXT FROM myCursor1
IF NOT EXISTS(SELECT * FROM sys.columns WHERE name = 'lnamerecoff'
AND object_id = OBJECT_ID('testingThis'))
ALTER TABLE testingThis ADD lnamerecoff int
WHILE @@FETCH_STATUS = 0
BEGIN
SET @new = FETCH NEXT FROM myCursor1
UPDATE testingThis
SET lnamerecoff = @new
SET @previous = @new
END
这会在我的fetch语句附近引发语法错误。谁能帮我这个?谢谢!
答案 0 :(得分:1)
应该是:
fetch next from myCursor1 into @previous