gpt4 book ai didi

c# - 'CREATE VIEW' 必须是查询批处理中的第一个语句

转载 作者:太空宇宙 更新时间:2023-11-03 19:15:31 26 4
gpt4 key购买 nike

这是我在 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' must be the first statement in a query batch.

请帮帮我。

最佳答案

信息不言自明; 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...');

关于c# - 'CREATE VIEW' 必须是查询批处理中的第一个语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17164727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com