首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

問兩個語句,该如何处理

2012-01-29 
問兩個語句問題一、declare@strSqlnvarchar(200)set@strSqlPWA,PWC,PWB,PWXselectDISTINCTProductTypefrom

問兩個語句
問題一、
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)

热点排行