if in stored procedure

  • iam using if in SP

    if (@lused = '')

    begin

    @lused = 0

    @balleave = Convert.ToInt32(@lused)

    end

    slq statement s follows

    but i get error incorrect syntax near @lused = 0

  • Use SET.

    John

  • ssurekha2000 (5/16/2012)


    iam using if in SP

    if (@lused = '')

    begin

    @lused = 0

    @balleave = Convert.ToInt32(@lused)

    end

    slq statement s follows

    but i get error incorrect syntax near @lused = 0

    As John said above you need to use SET to assign values to variables. Other than that you need to use CAST or CONVERT functions of SQL Server for type casting.

    It should be some thing like this:

    IF (@lused = '')

    BEGIN

    SET @lused = '0'

    SET @balleave = CAST(@lused AS INT)

    END


    Sujeet Singh

  • Thanks

  • Divine Flame (5/16/2012)

    IF (@lused = '')

    BEGIN

    SET @lused = '0'

    SET @balleave = CAST(@lused AS INT)

    END

    Or you can do this:

    IF (@lused = '') SELECT @lused = '0', @balleave = 0


    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

Viewing 5 posts - 1 through 4 (of 4 total)

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