declare @dsql varchar(100)
declare   @count int
declare @tj varchar(100)
declare @cm numeric
select @tj='where rq>0'
select @dsql='select @cm=sum(数量) from aj_tempstore_d'+@tj
exec(dsql)             
GO
-------------------------------
declare @dsql varchar(100)
declare   @count int
declare @tj varchar(100)
declare @cm numeric
select @tj='where rq>0'
select @dsql='select @cm=sum(数量) from aj_tempstore_d'+@tj
exec sp_executeSql @dsql,N'@cm int output',@count output          
GO

解决方案 »

  1.   

    declare @dsql varchar(100)
    declare @tj varchar(100)
    declare @cm numeric
    select @tj='where rq>0'
    select @dsql='select '+  @cm +' =sum(数量) from aj_tempstore_d'+@tj --这里修改
    exec(dsql)             
    GO
      

  2.   

    set nocount on 
    不返回数据的sql
     set nocount off
    返回数据的sql
      

  3.   

    http://blog.csdn.net/xluzhong/articles/366984.aspx
      

  4.   

    exec sp_executeSql @dsql,N'@cm int output',@count output     
    ---------------
    N'@cm int output',@count output     是什么意思??
    能解释一下吗?小弟对SQL不熟悉.
      

  5.   

    set nocount on 
    不返回数据的sql
     set nocount off
    返回数据的sql----------------
    怎么好像不管用阿,无论是ON 还是OFF 多返回两个SELECT ??晕!
      

  6.   

    declare @dsql varchar(100)
    declare @tj varchar(100)
    declare @cm numeric
    set @tj='where rq>0'
    set @dsql='select '+@cm+'=sum(数量) from aj_tempstore_d'+@tj
    exec(@dsql)             
    GO
    --这样可以吗?
      

  7.   

    set nocount on 
    不返回数据的sql
     set nocount off
    返回数据的sql
    --------------------
    这个好像不管用阿,怎样不返回用SELECT 赋值的那个变量值??
      

  8.   

    set @dsql='select '+@cm+'=sum(数量) from aj_tempstore_d'+@tj
    exec(@dsql)  
    -------------------
    不行.我试验国了
      

  9.   

    --建表
    create table t11(
    A varchar(10) unique,
    C int
    )insert into t11 values('234',40)
    insert into t11 values('523',40)
    insert into t11 values('4',40)
    insert into t11 values('34',40)
    insert into t11 values('53',40)
    insert into t11 values('34',40)--建存储过程
    create procedure pr_d(
    @tj varchar(100),
    @cm varchar(100)
    )
    as
    declare @dsql varchar(100)
    --select @tj=' where cast(A as int)>0 '
    select @dsql='select '+  @cm + ' = sum(cast(A as int)) from t11'+@tj
    exec(@dsql)             
    GO--执行存储过程
    execute pr_d ' where cast(A as int)>0 ','值'
      

  10.   

    怎样不返回用SELECT 赋值的那个变量值??
      

  11.   

    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附加参数的值的占位符。这些值只能是常量或变量,而不能是更复杂的表达式,例如函数或使用运算符生成的表达式。
      

  12.   

    declare @dsql varchar(100)
    declare @tj varchar(100)
    declare @cm numeric
    select @cm=2
    select @tj='where rq>0'
    select @dsql='select '+cast(@cm as varchar(10))+'=sum(数量) from aj_tempstore_d '+@tj
    exec (@dsql)
      

  13.   

    怎样不返回用SELECT 赋值的那个变量值??
    -------------------
    你说的是那条语句呀?
      

  14.   

    我知道了是用SET给变量赋值.就不返回了.
      

  15.   

    我对SQL不熟悉,让大家见笑了.