我有一堆SQL语句要执行以更新数据库的模式 - 它们都可以单独运行(即当我一个接一个地手动运行它们)但是当我尝试组合时遇到问题将它们整合到一个脚本中,包含一些版本控制逻辑。
我的脚本如下:
DECLARE @VersionCode varchar(20)
DECLARE @Description varchar(50)
DECLARE @Author varchar(20)
DECLARE @AppVersion varchar (10)
SET @VersionCode = 'version code';
SET @Description = 'description';
SET @Author = 'author';
SET @AppVersion = 'app version';
BEGIN
IF NOT EXISTS (SELECT * FROM [Version] WHERE [VersionCode] = @VersionCode)
BEGIN
ALTER TABLE [Journal] ADD [BreakDurationNew] INT NULL
UPDATE [Journal] SET BreakDurationNew = BreakDuration
ALTER TABLE [Journal] DROP COLUMN [BreakDuration]
EXEC sp_rename 'Journal.BreakDurationNew', 'BreakDuration', 'COLUMN';
-- create triggers on JournalBreak so that BreakDuration on Journal is updated
CREATE TRIGGER TR_JournalBreak_Insert ON [JournalBreak]
FOR INSERT
AS
BEGIN
UPDATE [Journal] SET BreakDuration = (coalesce([dbo].[CalculateJournalBreak]([JournalId]),(0))) WHERE [JournalId] IN (SELECT JournalId FROM INSERTED)
END
CREATE TRIGGER TR_JournalBreak_Update ON [JournalBreak]
FOR UPDATE
AS
BEGIN
UPDATE [Journal] SET BreakDuration = (coalesce([dbo].[CalculateJournalBreak]([JournalId]),(0))) WHERE [JournalId] IN (SELECT JournalId FROM INSERTED)
UPDATE [Journal] SET BreakDuration = (coalesce([dbo].[CalculateJournalBreak]([JournalId]),(0))) WHERE [JournalId] IN (SELECT JournalId FROM DELETED)
END
-- insert version record
INSERT INTO [RG].[dbo].[Version] (VersionCode, Description, Author, AppVersion) VALUES
(@VersionCode, @Description, @Author, @AppVersion)
END
END
我的猜测是我有一些东西(或一堆东西!)我的脚本语法错误但我目前无法发现它。有人可以帮忙吗?
喝彩!
答案 0 :(得分:2)
尝试@GordonLinoff评论
DECLARE @VersionCode VARCHAR(20) = 'version code'
DECLARE @Description VARCHAR(50) = 'description'
DECLARE @Author VARCHAR(20) = 'author'
DECLARE @AppVersion VARCHAR(10) = 'app version'
DECLARE @CMD NVARCHAR(Max) = ''
BEGIN
IF NOT EXISTS (
SELECT *
FROM [Version]
WHERE [VersionCode] = @VersionCode
)
BEGIN
SET @CMD = 'ALTER TABLE [Journal] ADD [BreakDurationNew] INT NULL'
EXEC SP_EXECUTESQL @CMD
SET @CMD = 'UPDATE [Journal] SET BreakDurationNew = BreakDuration
ALTER TABLE [Journal] DROP COLUMN [BreakDuration]'
EXEC SP_EXECUTESQL @CMD
EXEC sp_rename 'Journal.BreakDurationNew','BreakDuration','COLUMN';
-- create triggers on JournalBreak so that BreakDuration on Journal is updated
SET @CMD = '
CREATE TRIGGER TR_JournalBreak_Insert ON [JournalBreak]
FOR INSERT
AS
BEGIN
UPDATE [Journal] SET BreakDuration = (coalesce([dbo].[CalculateJournalBreak]([JournalId]),(0))) WHERE [JournalId] IN (SELECT JournalId FROM INSERTED)
END'
EXEC SP_EXECUTESQL @CMD
SET @CMD = '
CREATE TRIGGER TR_JournalBreak_Update ON [JournalBreak]
FOR UPDATE
AS
BEGIN
UPDATE [Journal] SET BreakDuration = (coalesce([dbo].[CalculateJournalBreak]([JournalId]),(0))) WHERE [JournalId] IN (SELECT JournalId FROM INSERTED)
UPDATE [Journal] SET BreakDuration = (coalesce([dbo].[CalculateJournalBreak]([JournalId]),(0))) WHERE [JournalId] IN (SELECT JournalId FROM DELETED)
END
'
EXEC SP_EXECUTESQL @CMD
-- insert version record
INSERT INTO [RG].[dbo].[Version] (VersionCode, Description, Author, AppVersion) VALUES
(@VersionCode, @Description, @Author, @AppVersion)
END
END
答案 1 :(得分:0)
看起来你正试图发明某种补丁机制。我建议制作单独的脚本:
某些脚本会检查您在特定服务器上的版本,并决定是否运行真正的补丁脚本
直截了当"愚蠢"补丁脚本,包含适用于确定版本的所有DDL和DML语句(需要使用GO分隔符)
所以我的建议是开发一个流量控制工具,给它一个功能来确定服务器上的版本和另一个功能 - 将适当的补丁脚本发送到sqlcmd.exe
。并且可能检查错误(如果有的话)。这甚至可以是批处理文件(* .cmd)。