Checking if a stored procedure paramter is null

  • Hi All,

    I have a stored procedure that requests one parameter which is a bit field. In the stored procedure I want to check if the parameter is null and then perform a different select statement but I can't get my head around how to do this. Any thoughts that might lead me in the right direction?

    Cheers

    ab

  • andy behrendt (4/21/2010)


    Hi All,

    I have a stored procedure that requests one parameter which is a bit field. In the stored procedure I want to check if the parameter is null and then perform a different select statement but I can't get my head around how to do this. Any thoughts that might lead me in the right direction?

    Cheers

    ab

    CREATE PROC dbo.Test (

    @BitField bit = NULL -- so that an entry isn't required

    )

    AS

    SET NOCOUNT ON

    IF @BitField IS NULL print '@BitField is null'

    ELSE IF @BitField = 'TRUE' print '@BitField is True'

    ELSE IF @BitField = 'FALSE' print '@BitField is False'

    GO

    exec dbo.Test

    exec dbo.Test 'True'

    exec dbo.Test 'False'

    GO

    DROP PROC dbo.Test

    GO

    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

  • Cheers for that. I realise now my syntax was at fault as I was trying variations of IsNull and getting the wrong results.

    ab

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

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