• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

Check Value of @ID

I have the following code

declare @ID INT

SET @ID = (SELECT MyID
      Table1
      WHERE Field1 = '1')

if (DATALENGTH(@ID) = 0)
    BEGIN
        SET @ID = 1
        PRINT @ID
    END
else
    BEGIN
        set @ID = DATALENGTH(@ID)
        PRINT @ID
    END

I need to perform an Insert but I need to make sure that @ID does not contain a value.  How do I check?
0
CipherIS
Asked:
CipherIS
  • 2
1 Solution
 
cyberkiwiCommented:
Does not contain a value, so it is null?

If (@ID is null)
0
 
cyberkiwiCommented:
Datalength(int) returns 4 by the way, since it takes 4 bytes to store any int
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now