Why can't we use variables in DDL?

  • This is my first forum post (I think). I've done a search and can't find any discussion on this.

    I appreciate that there are many programmatic ways to achieve the same end result, but I'm wondering what is the core reason why in general we can't use variables in place of identifiers in DDL statements.

    For example, it would seem intuitive and not obviously bad to be able to write statements like;

    CREATE USER @newuser FOR LOGIN @newuser

    I'm assuming it's to do with the mechanics of the parsing or binding in the query optimisation...?

  • You can't use variables to replace object names. That goes for DDL, DML, DCL or anything in T-SQL.

    You could use dynamic SQL for this.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Ah yes, that's true.

    It must be fundamental...I just can't see why.

    the resolution of an object name can be done at runtime, as per the synonym functionality...it seems like a straightforward language extension to me, but I'm not a software architect.

Viewing 3 posts - 1 through 2 (of 2 total)

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