Drop Table
Support Forum for database administrators and web based access to important newsgroups related to databasesHi, 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
Post Follow-up to this messageHello, 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
Post Follow-up to this messageGreat! Thanks so much! They all work fine, and I plan to use the second option
Post Follow-up to this messageYou 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.
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread