Using In Statement in Stored Procedure?

  • this is my stored procedure

    CREATE PROCEDURE sp_Test_FOR_InStatement

    @sStr as char(50)

    AS

    set @sStr = rtrim(@sStr)

    select * from a2zOrderdata where [Order No] in (@sStr)

    GO

    i pass in this values to stored procedure '146767,142359,146743' and i get Error converting data type varchar to int. as error messge.

    [Order No] is a numeric Field.

    Any idea, suggestion would be really appreicated.

    Mits

  • Hi Guys n gals

    I have found the solution to my above problem

    follow this link http://www.sqlteam.com/item.asp?ItemID=11499

     

    Mits

  • TRY THIS...

    CREATE PROCEDURE sp_Test_FOR_InStatement

    @sStr as char(50)

    AS

    set @sStr = rtrim(@sStr)

    DECLARE @strQuery VARCHAR(500)

    SET @strQuery='select * from a2zOrderdata where [Order No] in (' + @sStr +')'

    EXEC (@strQuery)

    GO

    Ofcourse usage of dynamic sql is not advised.

    Regards

    Prasad Bhogadi
    www.inforaise.com

  • I swear I'm gonna kill the next guy who proposes a dynimaic sql solution for this problem. Guys learn how to code pls b4 answering questions.

  • Needlessly harsh words, Remi.

    It will certainly be more helpful for anyone here if you post a better solution than just exercise rhetorical platitudes, don't you think?

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • Thank you all guys for your response. This is the first time i have asked anything in this forums and i will definitely come back.

     

    Mits

  • Rather than putting your list in a variable, put it into a temporary table as one row per value.  Then just use a join to that temporary table in your stored procedure.

    CREATE PROC sp_Test_FOR_InStatement AS

    SET NOCOUNT ON

    SELECT d.*

    FROM a2zOrderdata d JOIN #t t ON d.[Order No] = t.OrdNo

    CREATE TABLE #t(OrdNo int PRIMARY KEY)

    INSERT #t

    SELECT 146767

    UNION ALL SELECT 142359

    UNION ALL SELECT 146743

     



    --Jonathan

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply