問題一、
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.   

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

  2.   

    set @strSql='select …………'
    declare my_cur cursor scroll
    for
    exec sp_executesql  @strSql
    游标不能这样用
      

  3.   

    问题1除了用动态语句外,可以试试:
    declare @strSql nvarchar(200)set @strSql='PWA,PWC,PWB,PWX' select DISTINCT ProductType from MyTestTable where charindex(','+PackageType+',',','+@strSql+',')>0
      

  4.   

    roy_88(中国风_无限创新!!!) 兄,我知道遊標是不能這麼用的,現在是這樣的,我想獲得這樣一個遊標,
    給定一個條件字符串,如:set @strSql='aa','bb','cc'
    set @strSql='select testdata from testtable where id in('+@strSql+')'
    怎麼能獲得這個@strSql的招待結果的遊標
      

  5.   

    --1, try
    declare @strSql nvarchar(200)
    set @strSql=' ''PWA'',''PWC'',''PWB'',''PWX'' ' 
    exec('select DISTINCT ProductType from MyTestTable where PackageType in ('+@strSql+')')
      

  6.   

    问题1楼主的语句为:如下语句是没有问题,表里应该是没有这个条件的资料
    执行一下下面语句有结果就有数据,没有表里就没有数据
    select DISTINCT ProductType from MyTestTable where PackageType in ( 'PWA','PWC','PWB','PWX' )
      

  7.   

    set @strSql='declare my_cur cursor scroll for  select …………'
    exec sp_executesql  @strSql
    这样看看
      

  8.   

    --2, trydeclare my_cur cursor scroll
    for 
    select * from tbName
      

  9.   

    create table #tmptb(testdata datetime)
    insert #tmptb(testdata) exec(@Sql)
      

  10.   

    然后:
    declare my_cur cursor scroll
    for select testdata from #tmptb
      

  11.   

    declare @strSql nvarchar(200)set @strSql=' ''PWA'',''PWC'',''PWB'',''PWX'' ' 
    exec('select DISTINCT ProductType from MyTestTable where PackageType in ('+@strSql+')')