|
Home > Archive > Microsoft SQL Server forum > February 2006 > Setting a value for a boolean
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Setting a value for a boolean
|
|
|
| Hi,
I am trying to get a 0 or 1 depending on the sucess or failure of a
t-sql statement, and here is what I have:
Declare @boolDatabaseExists bit
Declare @chvnNewDatabaseName
nvarchar(260)
SET @chvnNewDatabaseNam
e = 'NewDatabase'
SET @boolDatabaseExi
sts =
(
SELECT (1)
FROM master.dbo.sysdatabases
WHERE name = @chvnNewDatabaseName
)
Print @boolDatabaseExists
What it is doing right now is that it prints a 1 for when there is a
match for the @NewDatabaseName in the sysdatabases table. If there is
not a match, then there is no value set for @ boolDatabaseExists
What I would like it to do is if there is no match for the
@NewDatabaseName in the sysdatabases, I would like to set the
boolDatabaseExists to 0
I hope I am clear in my explanation.
Thank you
KR
| |
| Razvan Socol 2006-02-25, 9:45 am |
| Hello, KR
You can use one of the following:
IF EXISTS (
SELECT *
FROM master.dbo.sysdatabases
WHERE name = @chvnNewDatabaseName
)
SET @boolDatabaseExists = 1
ELSE
SET @boolDatabaseExists = 0
or:
SET @boolDatabaseExists = CASE
WHEN EXISTS (
SELECT *
FROM master.dbo.sysdatabases
WHERE name = @chvnNewDatabaseName
) THEN 1 ELSE 0 END
or:
SET @boolDatabaseExists = (
SELECT COUNT(*)
FROM master.dbo.sysdatabases
WHERE name = @chvnNewDatabaseName
)
Razvan
| |
|
| Great! Thanks so much! They all work fine, and I plan to use the
second option
| |
| figital 2006-02-25, 9:45 am |
| You could probably just initialize the bit to 0, too. That way if there
is no result, it is still equal to 0/false. The options provided by
Razvan are good, though.
|
|
|
|
|