尝试调用 T-sql 中的存储过程时“=”附近的语法不正确
Incorrect syntax near '=' while trying to call a stored procedure in T-sql
我正在调用名为 Searchprocedure
的存储过程。我在调用它的那一行出错了。我没有对过程中传递的参数进行任何更改,它在之前使用相同的调用语句被调用得很好。
Exec SearchProcedure @firstname = 'Simran', @middlename = 'kaur', @lastname = 'Khurana', @City = 'Delhi'
给出错误的语法有什么问题:
Incorrect syntax near '='
编辑:
我所做的声明:
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
我找不到任何错误,并且可以 运行 exec 而不会出现错误消息。
编辑:在我创建(现在是 avialbe)输出过程并取消注释其调用后,我也遇到了错误...
仍在调查中
编辑2:
在输出过程中,您必须使用
set @sql = 'declare ' + '@Temp'+ @colVar + ' int'
(@在 Temp 之前)和
set @sql3 = 'set ' + ' @' + @tempresultStore"
(在'set'后加@。
顺便说一句:您可以在 Dynamic SQL 中执行多个语句,因此您可以使用 f.e。 exec (@sql + '; ' + @sql3);
我觉得你的问题太多了Dynamic sql
。我找到一个
SET @sql3 = 'set ' + ' ' + @tempresultStore + ' = ' + 'dbo.[Match'
+ @colVar + '](' + @colVar + 'FromUser' + ',' + @colVar
+ 'FromTable' + ',' + '0)'
在上面的代码中使用 Select
而不是 Set
。看起来您是从 UDF
中选择的。试试这个
SET @sql3 = 'Select ' + ' ' + @tempresultStore + ' = ' + 'dbo.[Match'
+ @colVar + '](' + @colVar + 'FromUser' + ',' + @colVar
+ 'FromTable' + ',' + '0)'
注意: 要在 Exec @sql
之前调试 Dynamic Sql
只需使用 print @sql
检查生成的 syntax
和其他内容sql
--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;
我正在调用名为 Searchprocedure
的存储过程。我在调用它的那一行出错了。我没有对过程中传递的参数进行任何更改,它在之前使用相同的调用语句被调用得很好。
Exec SearchProcedure @firstname = 'Simran', @middlename = 'kaur', @lastname = 'Khurana', @City = 'Delhi'
给出错误的语法有什么问题:
Incorrect syntax near '='
编辑: 我所做的声明:
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
我找不到任何错误,并且可以 运行 exec 而不会出现错误消息。
编辑:在我创建(现在是 avialbe)输出过程并取消注释其调用后,我也遇到了错误...
仍在调查中
编辑2:
在输出过程中,您必须使用
set @sql = 'declare ' + '@Temp'+ @colVar + ' int'
(@在 Temp 之前)和
set @sql3 = 'set ' + ' @' + @tempresultStore"
(在'set'后加@。
顺便说一句:您可以在 Dynamic SQL 中执行多个语句,因此您可以使用 f.e。 exec (@sql + '; ' + @sql3);
我觉得你的问题太多了Dynamic sql
。我找到一个
SET @sql3 = 'set ' + ' ' + @tempresultStore + ' = ' + 'dbo.[Match'
+ @colVar + '](' + @colVar + 'FromUser' + ',' + @colVar
+ 'FromTable' + ',' + '0)'
在上面的代码中使用 Select
而不是 Set
。看起来您是从 UDF
中选择的。试试这个
SET @sql3 = 'Select ' + ' ' + @tempresultStore + ' = ' + 'dbo.[Match'
+ @colVar + '](' + @colVar + 'FromUser' + ',' + @colVar
+ 'FromTable' + ',' + '0)'
注意: 要在 Exec @sql
之前调试 Dynamic Sql
只需使用 print @sql
检查生成的 syntax
和其他内容sql
--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;