dwain.c (11/21/2012)
Would this work?
DECLARE @DB VARCHAR(100) = 'MyDB'
SELECT *
FROM @DB.sys.check_constraints
If not, why would it work in dynamic SQL?
Obviously, you've found an approach that does work though.
I haven't fired up 2k12 yet but I'd just bet that won't work. You cannot use variables for object names like that.
The @ck thing being used in the queries is a pass through variable which is a trait of using sp_Execute SQL. Notice that where it's being used is not an object but acts just like what a variable would do in a norma query.
--Jeff Moden
Change is inevitable... Change for the better is not.