• qrius (11/4/2012)


    Hi, am trying to pass 'Orange,Guava' argurment to this stored Proc. dbo.TESCOS table has two columns, Fruit varchar(100) and Price int. I keep getting errors >> Msg 207, Level 16, State 1, Line 3

    Invalid column name 'Guava'.

    Any help? thanks!

    USE [Paul]

    GO

    /****** Object: StoredProcedure [dbo].[FruitPrice] Script Date: 11/04/2012 23:07:56 ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    -- Batch submitted through debugger: SQLQuery12.sql|7|0|C:\Users\Paul\AppData\Local\Temp\~vsD79B.sql

    ALTER PROC [dbo].[FruitPrice]

    (

    @OrderList varchar(500)

    )

    AS

    BEGIN

    SET NOCOUNT ON

    DECLARE @SQL varchar(400)

    SET @SQL =

    'SELECT Fruit,Price

    FROM dbo.TESCOS

    WHERE Fruit IN (' + @OrderList + ')'

    EXEC(@SQL)

    END

    You don't need dynamic SQL at all for this:

    ALTER PROC [dbo].[FruitPrice]

    (

    @OrderList varchar(500)

    )

    AS

    BEGIN

    SET NOCOUNT ON

    SELECT Fruit,Price

    FROM dbo.TESCOS

    WHERE Fruit IN (SELECT Item FROM DelimitedSplit8K(@OrderList, ','))

    END

    Where the DelimitedSplit8K FUNCTION can be found here: http://www.sqlservercentral.com/articles/Tally+Table/72993/


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St