to understand some sql statement

  • I see some code in our database, I'm trying to figure out what it means:

    I have a hard time to understand esp. this line of code:

    EXEC @rc = Mydb.dbo.Usp_mytable_Insert

    @ProcessId,

    @dEnddate,

    @sMessage,

    I know this is call a stored procedure, but not sure what this syntax is, it calls the procedure and with other parameters?

    What does NULL, NULL, NULL, 0, 1, 'Error', NULL, NULL

    mean?

    -----------------------------------------------------

    IF @iError <> 0

    BEGIN

    --Log error

    SET @dEnd = GETDATE()

    EXEC @rc = Mydb.dbo.Usp_mytable_Insert

    @ProcessId,

    @dEnddate,

    @sMessage,

    NULL, NULL, NULL, 0, 1, 'Error', NULL, NULL

    RAISERROR(@sMessage,11,1) WITH LOG, NOWAIT

    RETURN -1

    END

    RETURN 0

    END

    -----------------------------

    Thanks

  • EXEC @rc = Mydb.dbo.Usp_mytable_Insert

    @ProcessId,

    @dEnddate,

    @sMessage,

    NULL, NULL, NULL, 0, 1, 'Error', NULL, NULL

    All of that is the command to execute the stored procedure. These are parameter values:

    @ProcessId,

    @dEnddate,

    @sMessage,

    NULL, NULL, NULL, 0, 1, 'Error', NULL, NULL

    The NULLs are just "no value for the parameter". The rest are values.

    This part:

    EXEC @rc = Mydb.dbo.Usp_mytable_Insert

    Is just the execute command. "@rc = " between "EXEC" and the name of the procedure means it will assign the return value from the procedure to the "@rc" variable. By default, procedures return their error code. By default, error code 0 means it ran without any errors.

    Does that help?

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Yes, it helps a lot.

    I understand most part now.

    only for Exec @rc=

    it returns the succssful or failure result and it stored in @rc,

    but why store it?

    I see there is another line of code below

    Return 0,

    Does it mean @rc=0,

    Can the line also be written like:

    Exec stored procedurename

    ......

    And can still write Return 0?

    Thanks

  • If the code doesn't later use the @rc variable for anything, then no, there's no reason to store the return-value in it.

    You can use the Return command to return any value you like. It doesn't matter what's been returned by error codes, you can still have it return any value you like. Doesn't even have to be a number. It can be a date, a string, binary data, whatever.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Thank you, it helps a lot

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

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