How to check if a value (null/non null) exists in database table using stored pro

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • qwedster
    New Member
    • Jul 2008
    • 24

    How to check if a value (null/non null) exists in database table using stored pro

    Howdy folks!

    I am using stored procedure to see if a value exists in database table and return 0 if exists or else -1, in the following SQL queries.

    However how to check if a value (that is null) exists in database table and return 0 if exists or else -1 using stored procedure? Please help.

    USE [master]
    GO

    IF EXISTS (SELECT name FROM sys.databases WHERE name = 'ExampleDatabas e')
    DROP DATABASE [ExampleDatabase];
    GO

    CREATE DATABASE [ExampleDatabase];
    GO

    USE [ExampleDatabase];
    GO

    IF EXISTS (SELECT NAME FROM SYS.TABLES WHERE NAME = 'ExampleTable')
    DROP TABLE dbo.ExampleTabl e;
    GO

    CREATE TABLE
    dbo.ExampleTabl e
    (
    Name NVARCHAR(50) NULL,
    Phone NVARCHAR(50) NULL,
    Fax NVARCHAR(50) NULL
    CONSTRAINT UNIQUE_Phone UNIQUE(Phone),
    CONSTRAINT FK_Fax FOREIGN KEY(Fax) REFERENCES ExampleTable(Ph one)
    );
    GO

    SELECT * FROM ExampleTable;
    GO

    INSERT INTO dbo.ExampleTabl e
    (
    Name,
    Phone,
    Fax
    )
    VALUES
    (
    'Bill',
    '11111111',
    '11111111'
    );
    GO

    SELECT * FROM ExampleTable;
    GO

    INSERT INTO dbo.ExampleTabl e
    (
    Name
    )
    VALUES
    (
    'Larry'
    );
    GO

    SELECT * FROM ExampleTable;
    GO

    INSERT INTO dbo.ExampleTabl e
    (
    Name,
    Phone,
    Fax
    )
    VALUES
    (
    'Steve',
    '77777777',
    '11111111'
    );
    GO

    SELECT * FROM ExampleTable;
    GO


    USE [ExampleDatabase];
    GO

    IF EXISTS(SELECT NAME FROM SYS.PROCEDURES WHERE NAME = 'CheckForeignKe yFax')
    DROP PROCEDURE dbo.CheckForeig nKeyFax;
    GO

    CREATE PROCEDURE dbo.CheckForeig nKeyFax
    (
    @Fax NVARCHAR(50)
    )
    AS
    DECLARE @ResultFax [int]
    IF EXISTS
    (
    SELECT
    NULL
    FROM
    dbo.ExampleTabl e WITH (UPDLOCK)
    WHERE
    Phone = @Fax
    )
    BEGIN
    SELECT @ResultFax = 0
    END
    ELSE
    BEGIN
    SELECT @ResultFax = -1
    END

    RETURN @ResultFax


    DECLARE @ReturnValue INT
    EXEC @ReturnValue = CheckForeignKey Fax @Fax = '11111111'
    SELECT ReturnValue=@Re turnValue;
    GO


    DECLARE @ReturnValue INT
    EXEC @ReturnValue = CheckForeignKey Fax @Fax = NULL --RETURNS -1
    --EVEN THOUGH Phone has NULL. Please help?
    SELECT ReturnValue=@Re turnValue;
    GO
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Option 1:

    Code:
    WHERE isnull(Phone,'NULL') = isnull(@Fax,'NULL')
    Option 2:

    Code:
    WHERE (@Fax is NULL and Phone is NULL) or (@Fax = Phone)
    You might also consider how you treat NULL values entirely.

    Good luck!!!

    --- CK

    Comment

    • qwedster
      New Member
      • Jul 2008
      • 24

      #3
      Thanks buddy. You hit the nail on its head!

      Comment

      • qwedster
        New Member
        • Jul 2008
        • 24

        #4
        But how to check if the string is empty?

        Hi!

        Your answer failed when I called the stored procedure from my C# code as the code actually sends string.Empty or "" as the parameter or when queried using Query Analyzer it'd be like:

        DECLARE @ReturnValue INT
        EXEC @ReturnValue = CheckForeignKey Fax @Fax = '' -- FAILS
        SELECT ReturnValue=@Re turnValue;
        GO

        Please help.

        Comment

        • ck9663
          Recognized Expert Specialist
          • Jun 2007
          • 2878

          #5
          In your code:

          Code:
          DECLARE @ReturnValue INT
          EXEC @ReturnValue = CheckForeignKeyFax @Fax = '' -- FAILS
          SELECT ReturnValue=@ReturnValue;
          GO
          @Fax is not NULL, it has a value which is blank/empty.Do you consider NULL, space(1) and '' all the same?

          --- CK

          Comment

          Working...