Getting text to evaluate

  • I'm more familiar with Oracle than SQL Server, so pardon my ignorance. As in many situations, I know what I want to do, but not what its called. Is there a way for T-SQL to evaluate whats in a declared variable?

    For example, something like:

    Declare @MyDatabase varchar(30)

    SELECT @MyDatabase = 'SwitchTo'

    Use @MyDatabase

    Go

    TIA

    AndreQ

  • Use either Exec() or sp_executesql, the latter is usually preferred because SQL will try to cache the query plan. In either case the string executed run's with the callers permissions. We've got a few good articles here on the site about it, search for dynamic sql.

    Andy

    http://www.sqlservercentral.com/columnists/awarren/

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

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