我正在调用名为Searchprocedure
的存储过程。我在调用它时遇到错误。我没有对过程中传递的参数进行任何更改,并且之前使用相同的调用语句调用了它。
Exec SearchProcedure @firstname = 'Simran', @middlename = 'kaur', @lastname = 'Khurana', @City = 'Delhi'
语法的错误导致错误说明:
' ='附近的语法不正确
编辑: 我所做的陈述:
set @sql = 'declare ' + '@Temp'+ @colVar + ' int'
exec(@sql)
select @sql as 'SQLFORDECLARATIONS
outputs
declare @TempMiddleName int
但是当我尝试在变量中设置值时会给出错误,它应该首先声明它。
set语句导致:
select @TempMiddleName=dbo.[MatchMiddleName](MiddleNameFromUser,MiddleNameFromTable,0)
应该是什么,它无法看到声明的变量
存储过程如下:
create procedure SearchProcedure
(
@firstname nvarchar(20),
@middlename nvarchar(20) = null,
@lastname nvarchar(20),
@DOB Date = null,
@SSN nvarchar(30)= null,
@ZIP nvarchar(10)= null,
@StateOfResidence nvarchar(2)= null,
@City nvarchar(20)= null,
@StreetName nvarchar(20)= null,
@StreetType nvarchar(20)= null,
@BuildingNumber int= null,
@Aptnumber nvarchar(10)= null
)
As
DECLARE @sSQL NVARCHAR(2000), @Where NVARCHAR(1000) = ' '
declare @Percent int,
@FN nvarchar(20),
@MN nvarchar(20) = null,
@LN nvarchar(20),
@DateOfB Date = null,
@SSNumber nvarchar(30)= null,
@ZIPCode nvarchar(10)= null,
@StateOfRes nvarchar(2)= null,
@CityOfRes nvarchar(20)= null,
@StreetNameRes nvarchar(20)= null,
@StreetTypeRes nvarchar(20)= null,
@BuildingNumberRes int= null,
@AptnumberRes nvarchar(10)= null
set @Percent = 0
create table #results
(
firstname nvarchar(20) not null,
middlename nvarchar(20),
lastname nvarchar(20)not null,
PercentageMatch int not null,
DOB Date,
SSN nvarchar(30),
ZIP nvarchar(10),
[State] nvarchar(2),
City nvarchar(20),
StreetName nvarchar(20),
StreetType nvarchar(20),
BuildingNumber int,
Aptnumber nvarchar(10)
)
declare c Cursor local static Read_only
for
SELECT * from dbo.Patients where firstname = @firstname
open c
fetch next from c into @FN,
@MN,
@LN,
@DateOfB,
@SSNumber,
@ZIPCode,
@StateOfRes,
@CityOfRes,
@StreetNameRes,
@StreetTypeRes,
@BuildingNumberRes,
@AptnumberRes
while @@FETCH_STATUS = 0 BEGIN
/*set @Percent = dbo.[MatchLastName](@lastname, @LN, @Percent)
set @Percent = dbo.[MatchMiddleName](@middlename, @MN, @Percent)
set @Percent = dbo.[MatchCity](@City, @CityOfRes, @Percent)*/
Exec [dbo].[OutputProcedure] @lastname, @LN, @middlename, @MN,@City, @CityOfRes, @Percent output
Insert into #results values
(@FN,@MN,@LN,@Percent, @DateOfB,@SSNumber, @ZIPCode,@StateOfRes,@CityOfRes,@StreetNameRes,@StreetTypeRes,@BuildingNumberRes,@AptnumberRes)
fetch next from c into @FN,
@MN,
@LN,
@DateOfB,
@SSNumber,
@ZIPCode,
@StateOfRes,
@CityOfRes,
@StreetNameRes,
@StreetTypeRes,
@BuildingNumberRes,
@AptnumberRes
end
select * from #results order by PercentageMatch desc
IF OBJECT_ID('tempdb..#results') IS NOT NULL DROP TABLE #results
go
OutputProcedure
代码如下:
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 '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 = 'set ' + ' ' + @tempresultStore + '=' + '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
答案 0 :(得分:3)
--Setting variable in Dynamic SQL
DECLARE
@sql NVARCHAR(MAX),
@Name NVARCHAR(100)
SET @Name = '#B2E0EB1A'
SET @sql =
'
DECLARE @MyVar NVARCHAR(100)
SELECT TOP 1 @MyVar = name FROM sys.objects WHERE name LIKE ''%a''--Escaping single quote with double quote
PRINT @MyVar
'
EXEC(@sql)
SET @sql =
'
DECLARE @MyVar NVARCHAR(100)
SET @MyVar = '''+(SELECT TOP 1 name FROM sys.objects WHERE name LIKE '%a')+'''--Escaping single quote with double quote
PRINT @MyVar
'
EXEC(@sql)
SET @sql =
'
DECLARE @MyVar NVARCHAR(100)
SET @MyVar = '''+@Name+'''--Escaping single quote with double quote
PRINT @MyVar
'
EXEC(@sql)
SET @sql =
'
DECLARE @MyVar NVARCHAR(100)
SET @MyVar = ''#B2E0EB1A''--Escaping single quote with double quote
PRINT @MyVar
'
EXEC(@sql)
如果要从动态查询中获取输出变量,则必须使用sp_executesql过程而不是EXEC()
研究此代码
DECLARE @DynamicSQLOutput NVARCHAR(100)
DECLARE @SQL nvarchar(500);
DECLARE @ParmeterDefinition nvarchar(500);
--in this variabe you write the variables which you want to be declared in the dynamic sql without using the declare
SET @ParmeterDefinition = N'@FinalOutputResultInDynamicSQL NVARCHAR(100) OUTPUT';
--here you write your dynamic code
SELECT @SQL = N'SET @FinalOutputResultInDynamicSQL = ''test'' '
EXEC sp_executesql
@SQL, --Execute code
@ParmeterDefinition, -- Define Parameters
@FinalOutputResultInDynamicSQL = @DynamicSQLOutput OUTPUT --Get output
--Note that @FinalOutputResultInDynamicSQL is only defined in @ParmeterDefinition but not outside of the dynamic sql
PRINT @DynamicSQLOutput;
答案 1 :(得分:0)
我找不到任何错误,可以在没有错误消息的情况下运行exec。
编辑:在我创建(现在的avialbe)输出程序并取消注释其调用后,我也收到了错误...
仍在调查
EDIT2:
在您必须使用的输出过程中
set @sql = 'declare ' + '@Temp'+ @colVar + ' int'
(@在Temp之前)和
set @sql3 = 'set ' + ' @' + @tempresultStore"
(在&#39; set&#39;。
顺便说一句:你可以在动态SQL中执行多个语句,这样就可以使用f.e. exec (@sql + '; ' + @sql3);
答案 2 :(得分:0)
我认为Dynamic sql
中有太多错误。我找到了一个
SET @sql3 = 'set ' + ' ' + @tempresultStore + ' = ' + 'dbo.[Match'
+ @colVar + '](' + @colVar + 'FromUser' + ',' + @colVar
+ 'FromTable' + ',' + '0)'
在上面的代码而不是Set
中使用Select
。您似乎正在从UDF
中进行选择。试试这个
SET @sql3 = 'Select ' + ' ' + @tempresultStore + ' = ' + 'dbo.[Match'
+ @colVar + '](' + @colVar + 'FromUser' + ',' + @colVar
+ 'FromTable' + ',' + '0)'
注意:要在Dynamic Sql
之前调试Exec @sql
,只需使用print @sql
检查生成的syntax
中的sql
和其他内容