Id Name Name1
1 John Bob
2 Steve John
TableName - AllColumnUpdate
This Script is not working.
declare @col_name varchar(max)
,@sql nvarchar(max)
DECLARE Curupdate CURSOR FOR
SELECT column_name from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME =
'AllColumnUpdate' and ordinal_position > 1
OPEN Curupdate
FETCH NEXT FROM Curupdate INTO @col_name
WHILE @@FETCH_STATUS = 0
BEGIN
select case when @col_name = 'John' then 1 else 0 end from AllColumnUpdate
FETCH NEXT FROM Curupdate INTO @col_name
END
CLOSE Curupdate
DEALLOCATE Curupdate
But This is working
declare @col_name varchar(max)
,@sql nvarchar(max)
DECLARE Curupdate CURSOR FOR
SELECT column_name from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME =
'AllColumnUpdate' and ordinal_position > 1
OPEN Curupdate
FETCH NEXT FROM Curupdate INTO @col_name
WHILE @@FETCH_STATUS = 0
BEGIN
set @sql = 'select case when '+@col_name+' = ''John'' then 1 else 0 end from
AllColumnUpdate'
EXEC (@sql)
FETCH NEXT FROM Curupdate INTO @col_name
END
CLOSE Curupdate
DEALLOCATE Curupdate
Simple case statement is not working in Cursor but case with dynamic query is working fine in cursor...What could be the possible reason?