November 3, 2010 at 4:03 pm
I am trying to update a table through cursor something like below based on data type
DECLARE @b-2 nvarchar(255)
DECLARE list cursor for
select Column_Name
FROM information_schema.columns
WHERE table_schema = 'dbo'
AND table_name = 'xyz'
and data_type = 'some datatype'
OPEN List
FETCH NEXT FROM List
INTO @b-2
WHILE @@FETCH_STATUS = 0
BEGIN
print @b-2 -- using for checking whether cursor works or not
print @@SERVERNAME -- using for checking whether cursor works or not
UPDATE xyz
SET @b-2 = ''
FETCH NEXT FROM list INTO @b-2
END
CLOSE list
DEALLOCATE list
when i ran this it says the rows are being effected. But when i do a select * from the table it does not have any updation.
plzz help me
November 3, 2010 at 4:14 pm
UPDATE xyz
SET @b-2 = ''
You're updating a variable there... not a column. You also have no criteria in the UPDATE so you'll be updating the whole table. Lookup how to use cursors in Books Online. But before you do that, I recommend you lookup UPDATE so you can see that you don't actually need the slothfullness of a cursor here.
[Edit] I see what you're trying to do and it won't work. You cannot update columns in such a fashion unless you use dynamic SQL.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 3, 2010 at 4:20 pm
Thank you!! Jeff
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply