这是C#
中的脚本:
exec sp_executesql N'
IF OBJECT_ID(N''RealEstate.vwContract'', N''V'') IS NOT NULL
DROP VIEW RealEstate.vwContract
CREATE VIEW RealEstate.vwContract
AS
SELECT RealEstate.Contract.ID .... (Rest of Statement omitted for brevity)
出现错误:
Msg 111,Level 15,State 1,Line 1
'CREATE VIEW'必须是查询批处理中的第一个语句。
请帮帮我。
答案 0 :(得分:5)
这条信息不言而喻; create view
必须是第一个声明 - 但你可以作弊。我的创建脚本(如果我需要从ADO.NET运行它们,所以没有GO
)往往看起来很像:
if not exists(select 1 from sys.tables where name='SomeTable')
begin
exec('create table SomeTable ....blah not shown');
-- more to add indexing etc
end
if not exists(select 1 from sys.tables where name='SomeOtherTable')
begin
exec('create table SomeOtherTable ....blah not shown');
-- more to add indexing etc
end
您可以使用sys.views
执行相同的操作。也许,未经测试:
if exists (select 1 from sys.views where name = 'MyView')
exec ('drop view MyView');
exec ('create view MyView ...blah not shown...');
答案 1 :(得分:1)
将其拆分为两个脚本并先运行
IF OBJECT_ID(N''RealEstate.vwContract'', N''V'') IS NOT NULL
DROP VIEW RealEstate.vwContract
然后剩下的