Check existence of a database

  • Comments posted to this topic are about the item Check existence of a database

    Thanks.

  • Why didn't you use an ELSE statement to prevent the need for 2 calls?

  • I don't know how "Featured Scripts" are chosen. As LuckyNickyBoy observed the procedure is inefficient as it executes 4 tests where 2 are needed. Also, it uses the deprecated compatibility view sysdatabases instead of the replacement catalog view sys.databases. From BOL https://msdn.microsoft.com/en-us/library/ms179900.aspx "This SQL Server 2000 system table is included as a view for backward compatibility. We recommend that you use the current SQL Server system views instead." Even if I chose to use a deprecated view for some reason, I would use the sys schema, not dbo. Finally, returning 'YES' or 'NO' is not a choice I would make.

  • Check out DB_ID() https://msdn.microsoft.com/en-us/library/ms186274.aspx

  • I agree. You'd expect the specifically-chosen (?) "featured script" to be something at least moderately good/useful, but most often they are not.

    As for this code, I would suggest simply:

    SELECT CASE WHEN DB_ID (@dbname) IS NULL THEN 'NO' ELSE 'YES' END

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • Thanks for the tip.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply