Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SQL Statement using IF

12 views
Skip to first unread message

Buc

unread,
Oct 25, 2006, 12:26:03 PM10/25/06
to
I am trying to figure out how to do a boolean select that works in an IF
statment.
I'm just trying to see if a certain userID exists (actually if it DOESN't)
in a table .
How can this be accomplished? I can do a count into a variable and do the if
on that variable, but it looks like it would use less code and be cleaner if
I could do it right in the IF statement. I am trying to streamline and use
less code..

IF (select count(USERID) from users where UserID = 10)
begin
print 'found ID'
end

If balks and says non-boolean type specified in a context where a condition
is specified, near BEGIN

Thanks,
BUC

Tom Cooper

unread,
Oct 25, 2006, 11:33:56 AM10/25/06
to
If you want to know if a row exists, use

IF Exists (select * from users where UserID = 10)


begin
print 'found ID'
end

Tom

<Buc> wrote in message news:eHUl8mE%23GHA...@TK2MSFTNGP05.phx.gbl...

Lionstone

unread,
Oct 25, 2006, 11:33:39 AM10/25/06
to

<Buc> wrote in message news:eHUl8mE%23GHA...@TK2MSFTNGP05.phx.gbl...

> IF (select count(USERID) from users where UserID = 10)


> begin
> print 'found ID'
> end
>

IF EXISTS(SELECT * FROM users WHERE UserID=10)


DavidR

unread,
Oct 25, 2006, 11:35:53 AM10/25/06
to

<Buc> wrote:

I assume this is in an SP.
If all you want to do is test to see if a usedid is in a table

try
if not exists (select * from users where UserID = 10)
begin
do this
end
else
begin
do the other
end

--CELKO--

unread,
Oct 25, 2006, 11:36:10 AM10/25/06
to
IF EXISTS (SELECT * FROM Users WHERE user_id = 10)
PRINT 'found ID' ;

A SELECT statement does not have a data type in SQL.

news.microsoft.com

unread,
Oct 25, 2006, 11:33:43 AM10/25/06
to
Use exists.

if exists(Select UserID from users where UserID=10)


begin
print 'found ID'
end

<Buc> wrote in message news:eHUl8mE%23GHA...@TK2MSFTNGP05.phx.gbl...

Immy

unread,
Oct 25, 2006, 11:35:36 AM10/25/06
to

You said actually if it doesnt exist... then

if not exists (select * from users where userid =10)
begin
do something
end

and the other way to see if it does exist is

if exists (select * from users where userid =10)
begin
do something
end

Immy


<Buc> wrote in message news:eHUl8mE%23GHA...@TK2MSFTNGP05.phx.gbl...

Sylvain Lafontaine fill the blanks, no spam please

unread,
Oct 25, 2006, 12:27:25 PM10/25/06
to
Besides using the Exists statement, the problem with your code is that your
forgot to convert the numerical value returned by the Select count() to a
logical expression for the IF statement:

IF (select count(USERID) from users where UserID = 10) > 0


begin
print 'found ID'
end

Using Count(*) instead of Count(UserID) is also faster because it won't
check for the possibility of a null value for UserId; something which is
impossible here because of the Where statement (UserId = 10).

--
Sylvain Lafontaine, ing.
MVP - Technologies Virtual-PC
E-mail: http://cerbermail.com/?QugbLEWINF


<Buc> wrote in message news:eHUl8mE%23GHA...@TK2MSFTNGP05.phx.gbl...

0 new messages