1

这是我的脚本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' 必须是查询批处理中的第一条语句。

请帮我。

4

2 回答 2

5

信息不言自明;the create viewmust be the first statement - 但你可以作弊。我的创建脚本(如果我需要从 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...');
于 2013-06-18T09:13:26.013 回答
1

拆分成两个脚本先运行

IF OBJECT_ID(N''RealEstate.vwContract'', N''V'') IS NOT NULL
   DROP VIEW RealEstate.vwContract

然后剩下的

于 2013-06-18T09:13:14.587 回答