WHERE ... IN ... SQL Server 存储过程的问题

发布于 2024-12-11 18:12:45 字数 1460 浏览 0 评论 0原文

我必须在存储过程中实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁的年少不轻狂 2024-12-18 18:12:45

1)将varchar值'{4,5,6}'转换为int数据类型时转换失败。

此错误的原因是数据类型优先级INT 数据类型的优先级高于 VARCHAR 数据类型(16-INT 与 27-VARCHAR)。
因此,SQL Server 尝试将 '{4,5,6}' 转换为 INT,反之亦然。

2) 相反,我会将 @CatIds 转换为 XML,然后使用 nodes(...) 方法:

SET ANSI_WARNINGS ON;

DECLARE @CatIds VARCHAR(50) = '4,5,6';

DECLARE @x XML;
SET     @x = '<node>' + REPLACE(@CatIds, ',', '</node> <node>') + '</node>';
DECLARE @IDs TABLE
(
    ID INT PRIMARY KEY
);
INSERT  @IDs(ID)
SELECT  t.c.value('.', 'INT')
FROM    @x.nodes('/node') t(c);

--Test    
SELECT  *
FROM    @IDs

3) 下一步是使用 重写查询在(从中选择ID @IDs) 而不是 in (cast(@CatIds as varchar))

SET ANSI_WARNINGS ON;

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;

    --Start: New T-SQL code
    DECLARE @x XML;
    SET     @x = '<node>' + REPLACE(@CatIds, ',', '</node> <node>') + '</node>';
    DECLARE @IDs TABLE
    (
        ID INT PRIMARY KEY
    );
    INSERT  @IDs(ID)
    SELECT  t.c.value('.', 'INT')
    FROM    @x.nodes('/node') t(c);
    --End

    -- 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 
            --The search condition is rewritten using IN(subquery)
            Que_Category_Id in (SELECT ID FROM @IDs)
            and (Que_Type_Id=@qtId) 
            and (Qid not in (Select Que_Id From dbo.UserQuestion_Mapping where PIN=@PIN and Que_typeID=@qtId))


END

4) 调用存储过程:

exec SP_GetQuestionSetMultiCat '666777','4,5,6',5,'Practice'

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 than VARCHAR data type (16-INT vs. 27-VARCHAR).
So, SQL Server is trying to convert '{4,5,6}' to INT and not vice versa.

2) Instead, I would convert @CatIds to XML and then to a table variable (@IDs) using nodes(...) method:

SET ANSI_WARNINGS ON;

DECLARE @CatIds VARCHAR(50) = '4,5,6';

DECLARE @x XML;
SET     @x = '<node>' + REPLACE(@CatIds, ',', '</node> <node>') + '</node>';
DECLARE @IDs TABLE
(
    ID INT PRIMARY KEY
);
INSERT  @IDs(ID)
SELECT  t.c.value('.', 'INT')
FROM    @x.nodes('/node') t(c);

--Test    
SELECT  *
FROM    @IDs

3) The next step is to rewrite the query using IN (SELECT ID FROM @IDs) instead of in (cast(@CatIds as varchar)):

SET ANSI_WARNINGS ON;

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;

    --Start: New T-SQL code
    DECLARE @x XML;
    SET     @x = '<node>' + REPLACE(@CatIds, ',', '</node> <node>') + '</node>';
    DECLARE @IDs TABLE
    (
        ID INT PRIMARY KEY
    );
    INSERT  @IDs(ID)
    SELECT  t.c.value('.', 'INT')
    FROM    @x.nodes('/node') t(c);
    --End

    -- 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 
            --The search condition is rewritten using IN(subquery)
            Que_Category_Id in (SELECT ID FROM @IDs)
            and (Que_Type_Id=@qtId) 
            and (Qid not in (Select Que_Id From dbo.UserQuestion_Mapping where PIN=@PIN and Que_typeID=@qtId))


END

4) Call stored procedure:

exec SP_GetQuestionSetMultiCat '666777','4,5,6',5,'Practice'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文