0

I have the following statement:

IF (SELECT IntId 
FROM MyTable
WHERE Name = 'Jon') IS NOT NULL
BEGIN
    PRINT IntId
END
ELSE
BEGIN
    PRINT 'Not an id'
END

The IntId is the PK of the table and I want to see if it exists for the Name = Jon but it is out of scope so how do I get it to print?

2
  • 3
    What happens when you inevitably have more than 1 person called Jon? Commented Mar 3, 2022 at 18:29
  • Yes sorry that was bad psuedo code their as I changed the exactness of what I put on SO but the name is an AK in the database for this example, but that is a valid point. Commented Mar 3, 2022 at 18:45

3 Answers 3

3

What you could do in this scenario if using SQL Server 2017+ is use string_agg which would give you all ID values if there's duplicates:

declare @id varchar(max);
select @id=String_Agg(IntId, ',')
from MyTable
where [Name] = 'jon';

print isnull(@id, 'not an id');
Sign up to request clarification or add additional context in comments.

2 Comments

An alternative is to replace the case with Coalesce( @id, 'not an id' ).
Yes that woud be even more compact 👍
2

A few ways you might be able to accomplish this.

Using a variable

DECLARE @IntId int;

SET @IntId = (
    /* I added a TOP 1, in case your query returns more
    than one row. If it does return more than 1 row, you're
    looking at looping data to a print statement, and that's
    a different question. */

    SELECT TOP 1 IntId
    FROM MyTable
    WHERE Name = 'Jon'
    ORDER BY IntId
)

IF @IntId IS NOT NULL
BEGIN
    PRINT @IntId
END
ELSE
BEGIN
    PRINT 'Not an id'
END

IF EXISTS, select something

IF EXISTS (SELECT 1 FROM MyTable WHERE Name = 'Jon')
BEGIN
    SELECT IntId FROM MyTable WHERE Name = 'Jon'
END
ELSE
BEGIN
    PRINT 'Not an id'
END

6 Comments

I honestly disagree with dumping a TOP 1 in that subquery, as it simply hides the problem. If you are going to use a TOP 1 at least put an ORDER BY there so that it doesn't return an arbitrary value.
@Larnu I agree. That's why I put a comment there. Maybe the OP just wants the top values, so it's there as an example. The OP question was about getting something into a PRINT statement, which that achieves.
That doesn't change my point that there should be an ORDER BY for consistent results though. Either remove the TOP or add the ORDER BY.
You're correct. I updated my answer. The point of my answer was more to provide a working example of passing a variable value to print. That was short-sited on my part to not include the ORDER BY. Stu actually provides a better overall answer than mine, anyways.
IF EXISTS (SELECT 1 FROM MyTable WHERE Name = 'Jon') IS NOT NULL provides the blessing "Incorrect syntax near the keyword 'IS'." Since exists returns a boolean result which cannot be null there isn't much need to check for null. Aside: Performing two queries to get one result isn't likely to improve performance in this case.
|
1

You have two option. Repeat that select again which is not recommended because of performance issue. and the second it to store in a variable.

Declare @IntId int
SELECT @IntId = IntId 
FROM MyTable
WHERE Name = 'Jon'

IF (@IntId) IS NOT NULL
BEGIN
    PRINT @IntId
END
ELSE
BEGIN
    PRINT 'Not an id'
END

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.