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 

我会将表名作为参数'@Table_R'发送,但在游标中会引发错误。

  1. 如何在 Sql 游标中使用动态表。

谢谢

4

2 回答 2

2

您应该使用动态 sql 命令 sp_executesql ( http://msdn.microsoft.com/en-us/library/ms188001.aspx )。参见示例:

DECLARE @A numeric
EXEC SP_EXECUTESQL N'Select @a=count(*) from '+ @Table_R+ ' where ' + @M_col + ' is null',
      N'@A numeric OUTPUT', 
      @A OUTPUT;
于 2013-04-09T13:31:50.817 回答
0

代替

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

EXEC('Select count(*) from '+@Table_R+' where '+@M_col+' is null')
于 2013-04-09T13:37:10.793 回答