I am trying to use a cursor to move create a column that has all parts equal to a field that was before it. In example
| column1 | column 2 |
| 1 | a |
| 2 | b |
| 3 | c |
would to go...
| column1 | column2 | column3 |
| 1 | a | b |
| 2 | b | c |
| 3 | c | NULL |
So in order to do this I attempted using a cursor and an update statement based on the last fetch statement as below:
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
This is throwing an syntax error near my fetch statement. Can anyone help me with this? Thanks!