@sql里面是个字符串,是另外的条件,比如@sql=“and 1=1”现在要连接上,可以总报语法错误,我还不想用SQL=‘select @totalrec=count(*) from spzs where datediff(day,idate,getdate()) <= @tl’+@sql exec(@sql)的形式,就想直接用select @totalrec=count(*) from spzs where datediff(day,idate,getdate()) <= @tl +@sql 明白吗?
好像做不到吧! 如你所说的: set @sql=N'select @totalrec=count(*) from spzs where datediff(day,idate,getdate()) <= @tl'+@sql exec(@sql) --也不会得到正确结果的。 而应该是: set @sql='select @totalrec=count(*) from spzs where datediff(day,idate,getdate()) <= '+@tl+@sql EXECUTE sp_executesql @SQL,N'@totalrec int output',@count output
exec(@sql)的形式,就想直接用select @totalrec=count(*) from spzs where datediff(day,idate,getdate()) <= @tl +@sql
明白吗?
如你所说的:
set @sql=N'select @totalrec=count(*) from spzs where datediff(day,idate,getdate()) <= @tl'+@sql
exec(@sql)
--也不会得到正确结果的。
而应该是:
set @sql='select @totalrec=count(*) from spzs where datediff(day,idate,getdate()) <= '+@tl+@sql
EXECUTE sp_executesql @SQL,N'@totalrec int output',@count output