views:

40

answers:

1

Can I somehow use a specific database given a specific condition? To clarify I will give a naive example:

CASE 
    WHEN @dbnum = 1 THEN USE Db1
        ELSE USE DefaultDb
END
+5  A: 

You can do it with an IF:

IF @dbnum = 1
    USE Db1;
ELSE
    USE DefaultDb;
Paul
+1 - CASE is not decision logic in SQL Server.
OMG Ponies
Superb, thanks! @OMG Ponies could you explain that statement? When to use it and when not to.
picknick
Quite alright. CASE should be used _within_ a query to put a conditional on individual values of a column (from the SELECT, ORDER or GROUP), IF is used _outside_ individual queries, to determine which queries should be run.
Paul
Thank you for enlightening me
picknick