• 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


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)