Home Forums SQL Server 2005 T-SQL (SS2K5) Creating Database Objects from a Trigger-invoked stored procedure.... RE: Creating Database Objects from a Trigger-invoked stored procedure....

  • If you put USE into dynamic SQL, you switch the database just for the duration of EXEC of dynamic SQL, because it runs in a different scope. Next execute (with CREATE VIEW) will run again in the original context.

    Originally I wanted to suggest to put USE and CREATE VIEW in the same statement, but that won't work either (error: 'CREATE VIEW' must be the first statement in a query batch). I have no idea whether there is a way to solve it using dynamic SQL or not... I try to avoid dynamic SQL if possible, so I don't have that much experience with it.