declare @i intset @i = exec('select LeaseQuantity From Packages where class=''A''')消息 156,级别 15,状态 1,第 3 行
关键字 'exec' 附近有语法错误。这个为什么会错啊但 单独这一样句却行 exec('select LeaseQuantity From Packages where class=''A''')

解决方案 »

  1.   

    declare @i intset @i = (select count(LeaseQuantity ) From Packages where class='A')
      

  2.   

    declare @i intset @i = 'select LeaseQuantity From Packages where class=''A'''exec (@i)
      

  3.   

    declare @i intset @i = (select LeaseQuantity From Packages where class='A')
      

  4.   

    declare @i intset @i = select LeaseQuantity From Packages where class='A'exec (@i)
      

  5.   

    SQL code动态批处理中 EXEC 不像 sp_executesql 一样提供接口(这里就讲输出参数) 但是也有方法去解决这个问题
    --环境:
    create table test_3
    (
        id int identity(1,1),
        value int
    )
    insert test_3 
    select 1 union 
    select 5 union 
    select 9
    go
    1.全部写入动态字符串中
    exec (
    'declare @n int 
    select @N=count(*) from test_3 
    select @N '
    )2.INSERT EXEC 形式
    create table #cnt(n int)
    insert #cnt
    exec('select count(*) from test_3 ')
    declare @cnt int
    set @cnt=(select N from #cnt) 
    select @cnt3.动态批处理直接导入临时表
    create table #cnt_2(n int)
    exec (
    'insert #cnt_2 
    select count(*) from test_3'
    )
    declare @cnt int
    set @cnt=(select N from #cnt) 
    select @cnt
      

  6.   

    我不明白啊 为什么向我那样写会出现错误难到使用  动态sql不能为 声明的变量赋值
      

  7.   

    但为什么是张表的话 又可以用 insert将 exe(........)值插入
      

  8.   

    declare @i intexec sp_executesql
    N'select @i=LeaseQuantity From Packages where class=''A''',
    N'@I INT OUTPUT',@I OUTPUTSELECT @I