简体   繁体   English

T-SQL - 需要批定界符

[英]T-SQL - batch delimiter required

I am trying to build Visual Studio solution for Azure SQL Database.我正在尝试为 Azure SQL 数据库构建 Visual Studio 解决方案。

I have SQL script with CREATE TABLE and its extended attributes:我有带有CREATE TABLE及其扩展属性的 SQL 脚本:

create table schema.table_name (
   table_name_Key integer not null,
   constraint PK_table_Name primary key (table_Name_Key, Some_Column)
)
go

if exists (select 1 from  sys.extended_properties
           where major_id = object_id('table_name') and minor_id = 0)
begin 
   execute sp_dropextendedproperty 'MS_Description',  
   'user', 'schema', 'table', 'table_name' ;
end 

execute sp_addextendedproperty 'MS_Description',  
   'This table will be used for ....', 
   'user', 'schema', 'table', 'table_name';
go

if exists (select 1 from sys.extended_properties p 
           where p.major_id = object_id('table_name')
             and p.minor_id = (select c.column_id from sys.columns c 
                               where c.object_id = p.major_id 
                                 and c.name = 'table_name_Key'))
begin
   execute sp_dropextendedproperty 'MS_Description', 
   'user', 'schema', 'table', 'table_name', 'column', 'table_name_Key';
end

execute sp_addextendedproperty 'MS_Description', 
   'Primary Key of table_name table',
   'user', 'schema', 'table', 'table_name', 'column', 'table_name_Key';
go

I am still getting errors when building that I am missing batch delimiter:构建时我仍然遇到错误,我缺少批定界符:

Error SQL71006: Only one statement is allowed per batch.错误 SQL71006:每批只允许一个语句。 A batch separator, such as 'GO', might be required between statements.语句之间可能需要批分隔符,例如“GO”。

What am I missing and where?我错过了什么,在哪里? I already tried to add go after end but still error.我已经尝试在end后添加go但仍然出错。

If you are using SQL Server Data Tools (SSDT) database projects in Visual Studio then you need to think about things differently (and work through some of the tutorials , or this one ).如果您在 Visual Studio 中使用 SQL 服务器数据工具 (SSDT) 数据库项目,那么您需要以不同的方式思考问题(并完成一些教程教程)。

Basically each object is held in a single script and you do not have to write conditional statements (like IF EXISTS... ).基本上每个 object 都保存在一个脚本中,您不必编写条件语句(如IF EXISTS... )。 You simply define the object as you want it and SqlPackage.exe works out at deploy-time what the definition should be.您只需根据需要定义 object,SqlPackage.exe 在部署时计算出定义应该是什么。

A simple table with one extended property:具有一个扩展属性的简单表:

CREATE TABLE [testSchema].[table_name] (
    [table_name_Key] INT          NOT NULL,
    [Some_Column]    VARCHAR (10) NOT NULL,
    CONSTRAINT [PK_table_Name] PRIMARY KEY CLUSTERED ([table_name_Key] ASC, [Some_Column] ASC)
);


GO
EXECUTE sp_addextendedproperty @name = N'Caption', @value = 'Some_column is a required column.', @level0type = N'SCHEMA', @level0name = N'testSchema', @level1type = N'TABLE', @level1name = N'table_name', @level2type = N'COLUMN', @level2name = N'Some_Column';

Define your table in a similar manner to above, press Build and work through the errors.以与上述类似的方式定义您的表,按构建并解决错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM