stored procedure variables

  • I have a stored procedure with a local variable that i want to set to a result from a query, like this

    DECLARE @ExistingID AS bigint

    SELECT @ExistingID = ID FROM Table WHERE ReferenceID = @ReferenceID;

    what would the value of @ExistingID be if there was no data found ? would it be null ? or 0, or some other value ?

    ** ReferenceID is a string passed into the SP as a parameter

  • I would suggest adding a "select @ExistingID" to the end of your code, and see what you get.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • If no records is found based on your WHERE clause, you'll get a null value. But as stated above, do a select / print on that variable afterwards to confirm

Viewing 3 posts - 1 through 2 (of 2 total)

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