declare @temp_int smallint
declare @temp_sql varchar(200)set @temp_int=2
exec('declare @temp2 char(5);set @temp'+@temp_int+'=''try'';select @temp2')

解决方案 »

  1.   

    很謝謝你,但我還想動態變量的返回值輸出,即我得思路應該是:exec('declare @temp'+@temp_int+' char(5) output;set @temp'+@temp_int+'=''try'''),但這樣卻通不過。
      

  2.   

    sp_executesql
    执行可以多次重用或动态生成的 Transact-SQL 语句或批处理。Transact-SQL 语句或批处理可以包含嵌入参数。语法
    sp_executesql [@stmt =] stmt
    [
        {, [@params =] N'@parameter_name  data_type [,...n]' }
        {, [@param1 =] 'value1' [,...n] }
    ]参数
    [@stmt =] stmt包含 Transact-SQL 语句或批处理的 Unicode 字符串,stmt 必须是可以隐式转换为 ntext 的 Unicode 常量或变量。不允许使用更复杂的 Unicode 表达式(例如使用 + 运算符串联两个字符串)。不允许使用字符常量。如果指定常量,则必须使用 N 作为前缀。例如,Unicode 常量 N'sp_who' 是有效的,但是字符常量 'sp_who' 则无效。字符串的大小仅受可用数据库服务器内存限制。stmt 可以包含与变量名形式相同的参数,例如:N'SELECT * FROM Employees WHERE EmployeeID = @IDParameter'stmt 中包含的每个参数在 @params 参数定义列表和参数值列表中均必须有对应项。[@params =] N'@parameter_name  data_type [,...n]'字符串,其中包含已嵌入到 stmt 中的所有参数的定义。该字符串必须是可以隐式转换为 ntext 的 Unicode 常量或变量。每个参数定义均由参数名和数据类型组成。n 是表明附加参数定义的占位符。stmt 中指定的每个参数都必须在 @params 中定义。如果 stmt 中的 Transact-SQL 语句或批处理不包含参数,则不需要 @params。该参数的默认值为 NULL。[@param1 =] 'value1'参数字符串中定义的第一个参数的值。该值可以是常量或变量。必须为 stmt 中包含的每个参数提供参数值。如果 stmt 中包含的 Transact-SQL 语句或批处理没有参数,则不需要值。n附加参数的值的占位符。这些值只能是常量或变量,而不能是更复杂的表达式,例如函数或使用运算符生成的表达式。返回代码值
    0(成功)或 1(失败)
      

  3.   

    declare @temp_int smallint
    declare @temp_sql nvarchar(200)
    declare @结果 char(5)
    set @temp_int=2set @temp_sql=N'set @temp'+cast(@temp_int as nvarchar)+'=''try'';select @temp2'
    exec sp_executesql @temp_sql,N'@temp2 char(5) output',@结果 output
      

  4.   

    大力,因為@temp2是根據@temp+@temp_int來決定的,所以不知該如何output
      

  5.   

    declare @temp_int smallint
    declare @temp_sql1 nvarchar(200),@temp_sql2 nvarchar(200)
    declare @结果 char(5)
    set @temp_int=2set @temp_sql1=N'set @temp'+cast(@temp_int as nvarchar)+'=''try'';select @temp'+cast(@temp_int as nvarchar)
    set @temp_sql2=N'@temp'+cast(@temp_int as nvarchar)+' char(5) output'exec sp_executesql @temp_sql1,@temp_sql2,@结果 output
      

  6.   

    大力,很謝謝,真的。但還有點小問題,我的程序如下:
    declare @temp2 char(5)
    declare @temp_int smallint
    declare @temp_sql1 nvarchar(200),@temp_sql2 nvarchar(200),@temp_sql3 nvarchar(200)
    declare @结果 char(5)
    set @temp2='haha' ---檢測是否有被賦值
    set @temp_int=2
    set @temp_sql1=N'set @temp'+cast(@temp_int as nvarchar)+'=''try'';print @temp'+cast(@temp_int as nvarchar)
    set @temp_sql2=N'@temp'+cast(@temp_int as nvarchar)+' char(5) output'
    set @temp_sql3='@temp'+cast(@temp_int as nvarchar)+' char(5)'
    exec sp_executesql @temp_sql1,@temp_sql2,@temp_sql3
    print @temp2  -------打錢出的還是'haha'而不是'try'
    print @temp_sql2
    得到的結果如下:
      try
      haha   ----好像沒有被賦值
      temp2 char(5) output   辛苦你了。寫得較亂,你將它貼到查詢器可能會清楚點,謝謝。
      

  7.   

    declare @temp2 char(5)
    declare @temp_int smallint
    declare @temp_sql1 nvarchar(200),@temp_sql2 nvarchar(200),@temp_sql3 nvarchar(200)
    set @temp2='haha' ---檢測是否有被賦值
    set @temp_int=2set @temp_sql1=N'set @temp'+cast(@temp_int as nvarchar)+'=''try'';print @temp'+cast(@temp_int as nvarchar)set @temp_sql2=N'@temp'+cast(@temp_int as nvarchar)+' char(5) output'
    exec sp_executesql @temp_sql1,@temp_sql2,@temp2 output----------------------------------------^^^^^^^^^这里不能用'字符'这里随便放个变量来得到输出结果这里的@temp2<>exec里面的@temp2print @temp2
      

  8.   

    但我最終是想得到全局的﹒Temp2,因為它是不固定的,那麼我又該如何將
    exec的@temp2賦值呢?真是麻煩你啦。
      

  9.   

    exec sp_executesql @temp_sql1,@temp_sql2,@这个变量名你可以随便给 output如果还不行那你用##全局临时表exec('create table ## (a int,b int)')insert ## values(1,2)
      

  10.   

    我明白你的意思,如  exec sp_executesql @temp_sq1l,@temp_sql2,@temp_var output
      但我的原意是想將@temp2的值改變。但由於改變哪個變量值是由另一變量
    值約束,所以不能採用全局臨時表來解決問題!
      

  11.   

    问题是sql没有提供这样的工具给我们呀!