我在運行由SQL Server數據庫項目(Visual Studio 2013)上的發佈>生成腳本對話框生成的SQL腳本時遇到問題。使用SQLCMD執行發佈腳本不會返回
生成的腳本執行得很好,通常在2 - 10秒內運行,如果從SQLCMD模式的SQL Management Studio運行。但是,如果使用SQLCMD.exe運行(通過CMD或批處理文件),它似乎掛起並永遠不會結束。我在猜測,SQL Management Studio正在使用一些額外的參數(預設?)來執行腳本,但我一直無法直接使用SQLCMD。正在生成的SQL腳本通常最多隻有200行。下面是我使用的命令(正在從一個批處理文件運行,以管理員身份運行,在Windows服務器2012R2):
"D:\Program Files\Microsoft SQL Server\110\Tools\Binn\SQLCMD.exe" -S SQLSERVER1 -i "D:\Database\MyDatabase.publish.sql"
我還沒有看到任何錯誤,但SQLCMD只是從來沒有退出,激勵我必須殺死這個過程。下面是腳本由發佈對話框創建的類型的例子:
/*
Deployment script for MyDatabase
This code was generated by a tool.
Changes to this file may cause incorrect behavior and will be lost if
the code is regenerated.
*/
GO
SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;
SET NUMERIC_ROUNDABORT OFF;
GO
:setvar DatabaseName "MyDatabase"
:setvar DefaultFilePrefix "MyDatabase"
:setvar DefaultDataPath "D:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\"
:setvar DefaultLogPath "D:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\"
GO
:on error exit
GO
/*
Detect SQLCMD mode and disable script execution if SQLCMD mode is not supported.
To re-enable the script after enabling SQLCMD mode, execute the following:
SET NOEXEC OFF;
*/
:setvar __IsSqlCmdEnabled "True"
GO
IF N'$(__IsSqlCmdEnabled)' NOT LIKE N'True'
BEGIN
PRINT N'SQLCMD mode must be enabled to successfully execute this script.';
SET NOEXEC ON;
END
GO
IF EXISTS (SELECT 1
FROM [master].[dbo].[sysdatabases]
WHERE [name] = N'$(DatabaseName)')
BEGIN
ALTER DATABASE [$(DatabaseName)]
SET READ_COMMITTED_SNAPSHOT OFF;
END
GO
IF EXISTS (SELECT 1
FROM [master].[dbo].[sysdatabases]
WHERE [name] = N'$(DatabaseName)')
BEGIN
ALTER DATABASE [$(DatabaseName)]
SET PAGE_VERIFY NONE
WITH ROLLBACK IMMEDIATE;
END
GO
USE [$(DatabaseName)];
GO
IF fulltextserviceproperty(N'IsFulltextInstalled') = 1
EXECUTE sp_fulltext_database 'enable';
GO
PRINT N'Dropping FK_ORDR_BPO_ORDR_MASTER...';
GO
ALTER TABLE [dbo].[ORDR_BPO] DROP CONSTRAINT [FK_ORDR_BPO_ORDR_MASTER];
GO
PRINT N'Dropping FK_ORDR_CommonData_ORDR_MASTER...';
GO
ALTER TABLE [dbo].[ORDR_CommonData] DROP CONSTRAINT [FK_ORDR_CommonData_ORDR_MASTER];
GO
PRINT N'Dropping FK_ORDR_BPO_ProductType_ValuationProductCategory...';
GO
ALTER TABLE [dbo].[ORDR_BPO_ProductType] DROP CONSTRAINT [FK_ORDR_BPO_ProductType_ValuationProductCategory];
GO
PRINT N'Creating [dbo].[LOG_ServiceAction]...';
GO
CREATE TABLE [dbo].[LOG_ServiceAction] (
[LOG_ServiceActionID] INT IDENTITY (1, 1) NOT NULL,
[ServiceActionName] VARCHAR (255) NOT NULL,
[User_ID] INT NOT NULL,
[TransactionID] VARCHAR (32) NOT NULL,
[Message] VARCHAR (1000) NOT NULL,
[ActionDate] DATETIME NOT NULL,
[FilePath] VARCHAR (2000) NULL,
[ExpiresDateUTC] DATETIME NOT NULL,
CONSTRAINT [PK_LOG_ServiceAction] PRIMARY KEY CLUSTERED ([LOG_ServiceActionID] ASC)
);
GO
PRINT N'Creating FK_ORDR_BPO_ORDR_MASTER...';
GO
ALTER TABLE [dbo].[ORDR_BPO] WITH NOCHECK
ADD CONSTRAINT [FK_ORDR_BPO_ORDR_MASTER] FOREIGN KEY ([MasterOrder_ID]) REFERENCES [dbo].[ORDR_MASTER] ([MasterOrder_ID]);
GO
PRINT N'Creating FK_ORDR_CommonData_ORDR_MASTER...';
GO
ALTER TABLE [dbo].[ORDR_CommonData] WITH NOCHECK
ADD CONSTRAINT [FK_ORDR_CommonData_ORDR_MASTER] FOREIGN KEY ([MasterOrder_ID]) REFERENCES [dbo].[ORDR_MASTER] ([MasterOrder_ID]);
GO
PRINT N'Checking existing data against newly created constraints';
GO
USE [$(DatabaseName)];
GO
ALTER TABLE [dbo].[ORDR_BPO] WITH CHECK CHECK CONSTRAINT [FK_ORDR_BPO_ORDR_MASTER];
ALTER TABLE [dbo].[ORDR_CommonData] WITH CHECK CHECK CONSTRAINT [FK_ORDR_CommonData_ORDR_MASTER];
GO
PRINT N'Update complete.';
GO
任何幫助,在此,將不勝感激,因爲這是在自動化部署過程的最後一個手動工序。
更新:所以我有這方面的更多信息,可以幫助知道在診斷這個問題的人......我遇到的是,發佈腳本是在開始設置變量,如DatabaseName(請參閱上面的例子)。當從MS SQL Management Studio以SCLCMD模式運行時,該腳本有時不會返回。但是,如果我以非SQLCMD模式運行腳本,讓它失敗,然後在SQLCMD模式下重新運行它,它將成功執行。
有一些代碼,我發現返回一個錯誤(不幸的是,我沒有錯誤消息,但是當我再次得到錯誤,我會更新)時,腳本無法返回SQLCMD模式whilerunning:
IF EXISTS (SELECT 1
FROM [master].[dbo].[sysdatabases]
WHERE [name] = N'$(DatabaseName)')
BEGIN
ALTER DATABASE [$(DatabaseName)]
SET READ_COMMITTED_SNAPSHOT OFF;
END
該腳本在ALTER DATABASE [$(DatabaseName)]命令上失敗。我懷疑這是使用SQLCMD.exe從CMD行運行時發生的相同情況。我希望這可以幫助,但我會繼續找到解決方案,並在我這樣做時編輯這篇文章。謝謝。