所以我写道:
set @CreateViewStatement = ' USE ['+ @DB +']; CREATE VIEW [dbo].[MyTable] AS SELECT ........something exec (@CreateViewStatement)
它给了我错误:
‘CREATE VIEW’ must be the first statement in a query batch.
如果我删除USE DATABASE语句它工作正常,但然后数据库不再指定….
我怎么解决这个问题?
DECLARE @DB SYSNAME SET @DB = 'tempdb' DECLARE @CreateViewStatement NVARCHAR(MAX) SET @CreateViewStatement = ' USE '+ QUOTENAME(@DB) +'; EXEC('' CREATE VIEW [dbo].[MyTable] AS SELECT 1 AS [Foo] '') ' EXEC (@CreateViewStatement)