declare @where nvarchar(100)
declare @NotGDChnCount int
declare @sql nvarchar(1000)
set @sql=N'select @Count=COUNT(distinct id) from GoldCardReplacementData
where DataType=1 '
EXEC sp_executesql @sql,N'@Count int output',@NotGDChnCount output
select @NotGDChnCount
print @sql
以上为我的代码,在@sql中要是不加上@where变量,语句能正常执行,如果加了@where,就查不出任何东西,请问各位,这是为什么呢?

解决方案 »

  1.   

    解决了,@where变量初始化下就能用了
      

  2.   

    try
        declare @where nvarchar(100)
        declare @NotGDChnCount int
        declare @sql nvarchar(1000)
        SET @where=' where DataType=1 '
        set @sql=N'select @Count=COUNT(distinct id) from GoldCardReplacementData'+@where
        EXEC sp_executesql @sql,N'@Count int output',@NotGDChnCount output
        select @NotGDChnCount
        print @sql
      

  3.   

    where 里面给成1=1 先给个 让where恒成立
      

  4.   

    是啊,你定义了@where,可根本没用上