t-SQL:不正确的语法在' Go'

时间:2015-04-16 14:57:12

标签: sql-server tsql

  

注意:我已经在SO和其他论坛上回顾了几个类似的问题,这些建议都没有被证明是有用的。

以下t-SQL脚本可以正常工作:

USE [MASTER]
GO
BEGIN
IF NOT EXISTS 
    (SELECT name  
     FROM master.sys.server_principals
     WHERE name = 'myTestUser')
BEGIN
    PRINT 'CREATING LOGIN: [myTestUser]'
    CREATE LOGIN [myTestUser] WITH PASSWORD = N'P@55w0rd'
END ELSE BEGIN
    PRINT 'LOGIN [myTestUser] already exists on this server.'
        END
END
GO
BEGIN
IF NOT EXISTS 
        (
            SELECT 1 
            FROM master.dbo.sysdatabases 
            WHERE name ='myTestDb' 
        )
        BEGIN
            PRINT 'CREATING DATABASE [myTestDb]'
            --INSERT CREATE DB SCRIPT --
        END ELSE BEGIN      
            PRINT 'DATABASE [myTestDb] already exists on this server'
        END;
END
GO
USE[myTestDb]

以下脚本 ALSO 正常工作:

use [master]
CREATE DATABASE [myTestDb]
 CONTAINMENT = NONE
 ON  PRIMARY 
( NAME = N'myTestDb', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\myTestDb.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'myTestDb_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\myTestDb_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
--I have several ALTER DATABASE commands to follow, this is the minimum    
--necessary to duplicate the error condition.

HOWEVER当第二个脚本在第一个脚本(少于USE [master]行)中被删除时,它读取:--INSERT CREATE DB SCRIPT--我得

  

在第一个'去'... 如果我删除'Go',则错误将移至下一个'Go'实例。 最后的“Go”至关重要(在新的USE之前)

1 个答案:

答案 0 :(得分:5)

将第二个脚本粘贴到第一个脚本时,是否在第二个脚本的底部包含GO,以便组合脚本看起来像这样?

IF NOT EXISTS 
(
    SELECT 1 
    FROM master.dbo.sysdatabases 
    WHERE name ='myTestDb' 
)
BEGIN
    PRINT 'CREATING DATABASE [myTestDb]'
    CREATE DATABASE [myTestDb]
        CONTAINMENT = NONE
        ON  PRIMARY 
    ( NAME = N'myTestDb', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\myTestDb.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
        LOG ON 
    ( NAME = N'myTestDb_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\myTestDb_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
    GO
END ELSE BEGIN      
    PRINT 'DATABASE [myTestDb] already exists on this server'
END;

如果是这样,问题是GO构造中间有IF...ELSE。请尝试在GO之后移动END,如下所示:

IF NOT EXISTS 
(
    SELECT 1 
    FROM master.dbo.sysdatabases 
    WHERE name ='myTestDb' 
)
BEGIN
    PRINT 'CREATING DATABASE [myTestDb]'
    CREATE DATABASE [myTestDb]
        CONTAINMENT = NONE
        ON  PRIMARY 
    ( NAME = N'myTestDb', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\myTestDb.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
        LOG ON 
    ( NAME = N'myTestDb_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\myTestDb_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
END ELSE BEGIN      
    PRINT 'DATABASE [myTestDb] already exists on this server'
END;
GO