日期:2014-05-18  浏览次数:20569 次

問兩個語句
問題一、
declare   @strSql   nvarchar(200)

set   @strSql= '   ' 'PWA ' ', ' 'PWC ' ', ' 'PWB ' ', ' 'PWX ' '   '  

select   DISTINCT   ProductType   from   MyTestTable   where   PackageType   in   (@strSql)

執行後,沒數據。(當然,MyTestTable表裡是有數據的)

問題2、
set   @strSql= 'select   ………… '
declare   my_cur   cursor   scroll
for
exec   sp_executesql     @strSql
這句話提示語法錯誤,不知應該如何寫。


多謝

------解决方案--------------------
--1, try
declare @strSql nvarchar(200)
set @strSql= ' ' 'PWA ' ', ' 'PWC ' ', ' 'PWB ' ', ' 'PWX ' ' '
exec( 'select DISTINCT ProductType from MyTestTable where PackageType in ( '+@strSql+ ') ')
------解决方案--------------------
问题1楼主的语句为:如下语句是没有问题,表里应该是没有这个条件的资料
执行一下下面语句有结果就有数据,没有表里就没有数据
select DISTINCT ProductType from MyTestTable where PackageType in ( 'PWA ', 'PWC ', 'PWB ', 'PWX ' )
------解决方案--------------------
set @strSql= 'declare my_cur cursor scroll for select ………… '
exec sp_executesql @strSql


这样看看
------解决方案--------------------
create table #tmptb(testdata datetime)
insert #tmptb(testdata) exec(@Sql)