带输出参数的动态查询

时间:2010-02-16 12:38:27

标签: sql-server

你们中的任何人都可以帮助我将此查询转换为动态查询

DECLARE @Measure_Code nvarchar(50)
DECLARE @Transaction_Date datetime
DECLARE @Spec_Code nvarchar(max)
DECLARE @Error_Code nvarchar(500)
DECLARE @Operation_Status bit
DECLARE @Query nvarchar(1000)

SET @Measure_Code = 'CFL_WATTS'
SET @Transaction_Date = 02/16/2010

PRINT @Transaction_Date

SET @Spec_Code = 'CFL_Watts = 45'
SET @Error_Code = NULL 
SET @Operation_Status = NULL

SET @Query = 'USP_TRM_MEASURE_EVALUATE ' + '@Measure_Code'+','+'@Transaction_Date'+','+'@Spec_Code'+','+
                '@Error_Code output'+','+'@OPERATION_STATUSoutput'

PRINT @Query

EXEC(@Query)

我希望输出为

@QUERY = USP_TRM_MEASURE_EVALUATE @MeasureCode,@Transaction_Date, @Spec_Code, @Error_Code output, @Operation_Status output 

1 个答案:

答案 0 :(得分:0)

您不能将字符串传递给Exec()函数,并期望该字符串中的局部变量将被视为来自调用范围的变量,用于传入proc或从proc返回。

直接调用proc:

exec USP_TRM_MEASURE_EVALUATE @MeasureCode,@Transaction_Date, 
@Spec_Code, @Error_Code output, @Operation_Status output