0

@Table_R nvarchar(50)

FETCH NEXT FROM M_cursor INTO @M_col    

PRINT 'Mandatory Feilds ' + @M_col 

Select count(*) from @Table_R where @M_col is null'    


FETCH NEXT FROM M_cursor INTO @M_col 

I will send the table name as parameter ' @Table_R' , But in the cursor it throws an error.

  1. How to use dynamic table in the Sql Cursor.

thanks

user1027076
  • 175
  • 1
  • 4
  • 12
  • possible duplicate of [Sql - tablename as variable](http://stackoverflow.com/questions/2838490/sql-tablename-as-variable) – GSerg Apr 09 '13 at 13:35

2 Answers2

2

You should use dynamic sql command sp_executesql (http://msdn.microsoft.com/en-us/library/ms188001.aspx). See example:

DECLARE @A numeric
EXEC SP_EXECUTESQL N'Select @a=count(*) from '+ @Table_R+ ' where ' + @M_col + ' is null',
      N'@A numeric OUTPUT', 
      @A OUTPUT;
Alex
  • 8,827
  • 3
  • 42
  • 58
0

Replace

Select count(*) from @Table_R where @M_col is null

with

EXEC('Select count(*) from '+@Table_R+' where '+@M_col+' is null')
Azmi Kamis
  • 891
  • 5
  • 20