如何在动态SQL中使用存储过程的输入参数

时间:2015-01-16 11:30:18

标签: sql sql-server tsql ssms

这是一个包含一些输入参数的过程。现在,我需要在动态sql查询中使用这些输入参数,但它会出错:

  

列名称无效' MiddleNameFromTable'。

和其他所有人一样。

我理解动态SQL查询创建自己的会话,不能使用其他地方的声明,但必须有一些替代方案吗?

 CREATE Procedure OutputProcedure
 ( 
     @LastNameFromUser nvarchar(20) = null, 
     @LastNameFromTable nvarchar(20), 
     @MiddleNameFromUser nvarchar(20) = null, 
     @MiddleNameFromTable nvarchar(20) = null, 
     @CityFromUser nvarchar(20) = null,
     @CityFromTable nvarchar(20) = null,
    @Percentage int out
)
AS
BEGIN
    select @LastNameFromTable as 'LASTNAMEFROMTABLE'
    select @LastNameFromUser as 'LASTNAMEFROMUser'
    select 'OUTPUTPROCEDURECALLED'

    declare @maxvalue int

    DECLARE @variableTable TABLE (
       idx int identity(1,1),
       matchvalue nvarchar(15)
    )

    INSERT INTO @variableTable(matchvalue) values ('MiddleName')
    INSERT INTO @variableTable(matchvalue) values ('LastName')
    INSERT INTO @variableTable(matchvalue) values ('City')

    SELECT * FROM @variableTable 

    DECLARE @counter int
    declare @sql nvarchar(100)
    declare @sql2 nvarchar(25), @finalResult nvarchar(100)
    declare @sql3 nvarchar(300), @sql4 nvarchar(15),  @tempresultStore nvarchar(20), @temp int, @temp2 int, @average int

    SET @counter = 1
    SELECT @maxvalue = (SELECT MAX(idx) FROM @variableTable)

select @maxvalue as 'MAXVALUE'
WHILE(@counter < @maxvalue)
BEGIN
    DECLARE @colVar nvarchar(15)

    SELECT @colVar = matchvalue FROM @variableTable WHERE idx = @counter
    /*set @sql = 'declare ' + '@Temp'+ @colVar + ' int'
    exec(@sql)
    select @sql as 'SQLFORDECLARATIONS'*/

    /*set @temp = CHARINDEX(' ',@sql)
    select @temp as 'resultofcharindex'
    set @temp2 = LEN(@sql) - (@temp)
    SELECT @temp2 AS 'AFTERADDING1'
    set @tempresultStore = right(@sql, @temp2)*/
    set @tempresultStore = 'Temp'+@colVar
    SELECT @tempresultStore AS 'FINALCUTPART'

set @sql3 = 'declare @Temp' + @colVar + ' int = dbo.[Match' + @colVar + '](' + @colVar + 'FromUser,' + @colVar + 'FromTable, 0)' 
EXEC(@sql3)  
select @sql3 as 'check sql query formed'

set @finalResult = @finalResult + @tempresultStore
select @finalResult as 'SUM'
    SET @counter = @counter + 1
    select @counter as 'COUNTERVALUE'
    END
    set @Percentage = @finalResult/@maxvalue
    SELECT @Percentage AS 'FINALRESULT'
RETURN 
END

2 个答案:

答案 0 :(得分:0)

把你的:

select @sql3 as 'check sql query formed'

在您的EXEC之前,看看它实际构建的是什么,那么对您来说可能是显而易见的。

答案 1 :(得分:0)

此程序有问题。您获得的错误是由于生成的语句。看看

declare @TempMiddleName int = dbo.[MatchMiddleName](MiddleNameFromUser,MiddleNameFromTable, 0)

我想你想要这个

declare @TempMiddleName int = dbo.[MatchMiddleName](@MiddleNameFromUser,@MiddleNameFromTable, 0)

首先改变

set @sql3 = 'declare @Temp' + @colVar + ' int = dbo.[Match' + @colVar + '](' + @colVar + 'FromUser,' + @colVar + 'FromTable, 0)'

set @sql3 = 'declare @Temp' + @colVar + ' int = dbo.[Match' + @colVar + '](@' + @colVar + 'FromUser,@' + @colVar + 'FromTable, 0)' 

但这还不够。您必须为动态查询提供参数,如下所示:

在程序顶部添加

DECLARE @ParamDefs NVARCHAR(MAX) = N'@MiddleNameFromUser NVARCHAR(20), @MiddleNameFromTable NVARCHAR(20)'

并更改

EXEC(@sql3) 

EXEC sp_executesql @sql3, @ParamDefs, @MiddleNameFromUser, @MiddleNameFromTable

就是这样。