[英]SQL Server Stored Procedure syntax error when adding several OR clauses
I am trying to select some products from products table using a stored procedure in SQL Server 2016 but when I am adding several or clause it gives me this syntax error: 我试图使用SQL Server 2016中的存储过程从产品表中选择一些产品,但是当我添加多个或子句时,出现此语法错误:
Msg 102, Level 15, State 1, Line 5 Incorrect syntax near '100'.
消息102,级别15,状态1,第5行'100'附近的语法不正确。
here is the line of stored procedure that when I add it, it gives me the error. 这是存储过程的行,当我添加它时,它会给我错误。 (without this line, it is ok):
(没有此行,可以):
@StoreId int = null,
//the code is quite verbose and I added storeid variable as a clarification here
//and in the actual code it is defined in the proper line(not here) and the part
//that is creating the error is the below part cause I have tested it and When I
//remove it, it performs ok
IF @ShowUnavailable = 1
set @sql =
'SELECT Distinct P.ProductId, P.BrandId, P.[' + @OrderIndex + '] FROM Products P INNER JOIN ProductStores PS ON P.ProductId = PS.ProductId ' +
'Where PS.StoreId = @StoreId and (PS.Status IN (' + @availabilityStatus + ') or (PS.StoreId != @StoreId and PS.Status IN(0,2))'
To me it seems like @OrderIndex
is of datatype INT
and while string concatenation an Implicit conversion is happening creating the error. 在我看来,
@OrderIndex
的数据类型为INT
并且在字符串连接时发生了隐式转换,从而产生了错误。 Try converting @OrderIndex
as cast (@OrderIndex as varchar)
to resolve the error `[Conversion failed when converting the nvarchar value ...to data type int.] 尝试将
@OrderIndex
转换为cast (@OrderIndex as varchar)
转换cast (@OrderIndex as varchar)
转换cast (@OrderIndex as varchar)
以解决错误`[将nvarchar值转换为数据类型int时转换失败。]
Edit: If above is not the case and error is coming not while code compilation but while executing the dynamic sql @sql
string then explicitly convert int datatype to varchar for string concatenation as cast (@StoreID as varchar)
. 编辑:如果不是上述情况,并且错误不是在代码编译时而是在执行动态sql
@sql
字符串时出现,然后将int数据类型显式转换为varchar,以将字符串转换为cast (@StoreID as varchar)
。
1, If the variable is an integer, you need to convert it to varchar. 1,如果变量是整数,则需要将其转换为varchar。 Because @sql is a string.
因为@sql是字符串。
2, Please clarify the data type of @availabilityStatus. 2,请阐明@availabilityStatus的数据类型。 I used '1,2' as the sample.
我使用“ 1,2”作为样本。
declare @StoreID int = 0
declare @OrderIndex int = 0
declare @availabilityStatus varchar(max) = '1,2'
declare @sql nvarchar(max)
set @sql =
'SELECT Distinct P.ProductId, P.BrandId, P.[' + cast(@OrderIndex as varchar(100)) + '] FROM Products P INNER JOIN ProductStores PS ON P.ProductId = PS.ProductId ' +
'Where PS.StoreId = '+ cast(@StoreId as varchar(100))+ ' and (PS.Status IN (' + @availabilityStatus + ') or (PS.StoreId != '+cast(@StoreId as varchar(100))+' and PS.Status IN(0,2))'
Print @sql
Test Result: DB <> Fiddle 测试结果: DB <> Fiddle
In 在
'Where PS.StoreId = **@StoreId** and (PS.Status IN (' + @availabilityStatus + ') or (PS.StoreId != **@StoreId** and PS.Status IN(0,2))'
the way you have passed @StoreId is seems to be some thing wrong. 您传递@StoreId的方式似乎有些错误。
please use proper cast or conversion of nvarchar to int if needed. 如果需要,请使用正确的强制转换或将nvarchar转换为int。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.