WHERE ... IN ... SQL Server 存储过程的问题
我必须在存储过程中实现 SELECT ... FROM ... WHERE .. IN 查询。
下面是我的存储过程中的代码
ALTER PROCEDURE [dbo].[SP_GetQuestionSetMultiCat]
-- Add the parameters for the stored procedure here
@PIN varchar(50),
@CatIds varchar(50),
@Range int,
@Que_Type varchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @qtId as int;
select @qtId = Que_Type_Id from dbo.QuestionType_Tbl where Que_Type=@Que_Type;
-- Insert statements for procedure here
Select Top(@Range)
QId,
Que_Type_Id,
Que_Level_Id,
Que_Category_Id,
Que,
Opt1,
Opt2,
Opt3,
Opt4,
Ans
From
dbo.Que_Tbl
Where
(Que_Category_Id in (cast(@CatIds as varchar)))
and (Que_Type_Id=@qtId)
and (Qid not in (Select Que_Id From dbo.UserQuestion_Mapping where PIN=@PIN and Que_typeID=@qtId))
END
查看 where 条件。 Que_Category_Id是int类型。我想要执行的是 -
Where Que_Category_Id in (1,2,3,4)
我传递的 in 值是从我的 C# 代码转换而来的字符串。
当我执行此查询时,例如 -
exec SP_GetQuestionSetMultiCat '666777','4,5,6',5,'Practice'
它生成错误 -
将 varchar 值“{4,5,6}”转换为数据类型 int 时转换失败。
谁能帮我解决这个问题。
感谢您分享您的宝贵时间。
I have to implement SELECT ... FROM ... WHERE .. IN
query in my stored procedure.
Below is the code from my stored procedure
ALTER PROCEDURE [dbo].[SP_GetQuestionSetMultiCat]
-- Add the parameters for the stored procedure here
@PIN varchar(50),
@CatIds varchar(50),
@Range int,
@Que_Type varchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @qtId as int;
select @qtId = Que_Type_Id from dbo.QuestionType_Tbl where Que_Type=@Que_Type;
-- Insert statements for procedure here
Select Top(@Range)
QId,
Que_Type_Id,
Que_Level_Id,
Que_Category_Id,
Que,
Opt1,
Opt2,
Opt3,
Opt4,
Ans
From
dbo.Que_Tbl
Where
(Que_Category_Id in (cast(@CatIds as varchar)))
and (Que_Type_Id=@qtId)
and (Qid not in (Select Que_Id From dbo.UserQuestion_Mapping where PIN=@PIN and Que_typeID=@qtId))
END
Look at the where condition. The Que_Category_Id is int type. What i want to perform is -
Where Que_Category_Id in (1,2,3,4)
The in values i m passing is a string converted from my C# code.
When I am executing this query like -
exec SP_GetQuestionSetMultiCat '666777','4,5,6',5,'Practice'
it is generating an error -
Conversion failed when converting the varchar value '{4,5,6}' to data type int.
Can anybody help me out how to solve this problem.
Thanks for sharing your valuable time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1)
将varchar值'{4,5,6}'转换为int数据类型时转换失败。
此错误的原因是数据类型优先级。
INT
数据类型的优先级高于VARCHAR
数据类型(16-INT 与 27-VARCHAR)。因此,SQL Server 尝试将
'{4,5,6}'
转换为INT
,反之亦然。2) 相反,我会将
@CatIds
转换为XML
,然后使用 nodes(...) 方法:3) 下一步是使用
重写查询在(从中选择ID @IDs)
而不是in (cast(@CatIds as varchar))
:4) 调用存储过程:
1)
Conversion failed when converting the varchar value '{4,5,6}' to data type int.
The reason of this error is data type precedence.
INT
data type has "higher" precedence thanVARCHAR
data type (16-INT vs. 27-VARCHAR).So, SQL Server is trying to convert
'{4,5,6}'
toINT
and not vice versa.2) Instead, I would convert
@CatIds
toXML
and then to a table variable (@IDs
) using nodes(...) method:3) The next step is to rewrite the query using
IN (SELECT ID FROM @IDs)
instead ofin (cast(@CatIds as varchar))
:4) Call stored procedure: