如何在动态中使用存储过程的输入参数SQL
How to use input parameters of stored procedure in dynamic SQL
这是一个带有一些输入参数的过程。现在,我需要在动态 sql 查询中使用这些输入参数,但它给出了错误:
Invalid column name '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
把你的:
select @sql3 as 'check sql query formed'
在你的 EXEC 之前,看看它实际构建了什么,然后你可能会很明显。
这个过程有问题。您收到的错误是因为生成了语句。看
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
就这些了。
这是一个带有一些输入参数的过程。现在,我需要在动态 sql 查询中使用这些输入参数,但它给出了错误:
Invalid column name '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
把你的:
select @sql3 as 'check sql query formed'
在你的 EXEC 之前,看看它实际构建了什么,然后你可能会很明显。
这个过程有问题。您收到的错误是因为生成了语句。看
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
就这些了。