所以我过去几周一直在编写一个数据迁移脚本,并且已经确定它已经变得太长而且很难编辑为一个长脚本。所以我想要做的是将其拆分为单独的脚本,并让一个脚本运行其他脚本。
从四处查看,我发现您可以通过将查询设置为在SQLCMD模式下运行来调用每个脚本。这确实有效但是我只有前两个脚本(一个是程序)得到了很多错误:
-- Setting path variable
----> This will need to be changed to the correct location before running
:setvar path "Directory\DIR"
-- Create Procedures
:r $(path)\~alterFieldSize.sql
-- Call Migration Scripts
:r $(path)\~prerequisites.sql
这给了我以下错误:
Msg 154,Level 15,State 1,Procedure alterFieldSize,Line 43 [Batch 起始行0]过程中不允许使用USE数据库语句, 功能或触发器。
消息134,级别15,状态1,过程alterFieldSize,第81行[批处理 起始行0]变量名'@column'已经声明。 变量名在查询批处理中必须是唯一的或存储的 程序。
Msg 134,Level 15,State 1,Procedure alterFieldSize,Line 82 [Batch 起始行0]已声明变量名'@table'。 变量名在查询批处理中必须是唯一的或存储的 程序。
Msg 134,Level 15,State 1,Procedure alterFieldSize,Line 83 [Batch Start Line 0]已经声明了变量名'@fieldLen'。 变量名在查询批处理中必须是唯一的或存储的 程序。
Msg 154,Level 15,State 1,Procedure alterFieldSize,Line 91 [Batch 起始行0]过程中不允许使用USE数据库语句, 功能或触发器。
当我单独运行这两个脚本或运行整个迁移脚本时,不会发生这些错误。 alterFieldSize.sql和prerequisites.sql脚本如下:
alterFieldSize.sql - 程序
CREATE PROCEDURE alterFieldSize @column NVARCHAR(MAX), @table NVARCHAR(MAX), @prsColumnLen INT
AS
-- DECLARE VARIABLES
DECLARE @sql NVARCHAR(MAX)
DECLARE @fieldLen INT
DECLARE @columnLen INT
-- SET VARIABLES
SET @fieldLen = @prsColumnLen
SET @columnLen = (SELECT CHARACTER_MAXIMUM_LENGTH FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE @column AND TABLE_NAME LIKE @table)
SET @fieldLen = CASE WHEN @columnLen > @fieldLen THEN @columnLen WHEN @fieldLen <=500 THEN @fieldLen ELSE 500 END
SET @sql = 'ALTER TABLE ' +@table+ ' ALTER COLUMN '+ @column + ' NVARCHAR('+cast(@fieldLen as VARCHAR(20))+')'
-- Execution
EXEC(@sql)
prerequisites.sql
PRINT '-----------------------------------------------------------------------------------------------------------------------------'
PRINT '---------------------------------------------------- PREREQUISITES ----------------------------------------------------------'
PRINT '-----------------------------------------------------------------------------------------------------------------------------'
-- CREATE COLUMNS
USE [DB1];
-- ALTER TABLE tblPerson ADD welPersonnelID NVARCHAR(20);
-- ALTER TABLE tblPerson ADD aucPersonnelID NVARCHAR(20);
-- ALTER TABLE Client ADD welSiteID NVARCHAR(20);
-- ALTER TABLE Client ADD aucSiteID NVARCHAR(20);
-- ALTER TABLE tblContact ADD COwelPersonnelID NVARCHAR(20);
-- ALTER TABLE tblContact ADD COaucPersonnelID NVARCHAR(20);
-- ALTER TABLE tblCandidate ADD CAwelPersonnelID NVARCHAR(20);
-- ALTER TABLE tblCandidate ADD CAaucPersonnelID NVARCHAR(20);
-- ALTER TABLE AM ADD AMwelPersonnelID NVARCHAR(20);
-- ALTER TABLE AM ADD AMaucPersonnelID NVARCHAR(20);
-- ALTER TABLE AM ADD TRISID NVARCHAR(20);
-- ALTER TABLE Job ADD welVacancyID NVARCHAR(20);
-- ALTER TABLE Job ADD aucVacancyID NVARCHAR(20);
-- ---- DB2
-- USE [DB2];
-- ALTER TABLE Sites ADD siteID#TRIS NVARCHAR(20);
-- ALTER TABLE personnel ADD personID#TRIS NVARCHAR(20);
-- ALTER TABLE personnel ADD personStatus NVARCHAR(20);
-- ALTER TABLE personnel ADD titleID int;
-- ALTER TABLE Vacancies ADD jobID#TRIS NVARCHAR(20);
-- ALTER TABLE Vacancies ADD ContactID#TRIS NVARCHAR(20);
-- ALTER TABLE Vacancies ADD ClientID#TRIS NVARCHAR(20);
-- ---- DB3
-- USE [DB3];
-- ALTER TABLE Sites ADD siteID#TRIS NVARCHAR(20);
-- ALTER TABLE personnel ADD personID#TRIS NVARCHAR(20);
-- ALTER TABLE personnel ADD personStatus NVARCHAR(20);
-- ALTER TABLE personnel ADD titleID int;
-- ALTER TABLE Vacancies ADD jobID#TRIS NVARCHAR(20);
-- ALTER TABLE Vacancies ADD ContactID#TRIS NVARCHAR(20);
-- ALTER TABLE Vacancies ADD ClientID#TRIS NVARCHAR(20);
-- PURPOSE: Declares variables and creates columns that are used for linking
-- DECLARE VARIABLES
DECLARE @column NVARCHAR(MAX);
DECLARE @table NVARCHAR(MAX);
DECLARE @fieldLen INT;
DECLARE @fieldLenA INT;
DECLARE @fieldLenW INT;
-- SET VARIABLES
-- CHANGE FIELD LENGTHS
---- CLIENT
USE [DB1]
SET @fieldLenA = (SELECT MAX(LEN(s.StreetAddress)) FROM [DB2].dbo.Sites s)
SET @fieldLenW = (SELECT MAX(LEN(s.StreetAddress)) FROM [DB3].dbo.Sites s)
SET @fieldLen = CASE WHEN @fieldLenA > @fieldLenW THEN @fieldLenA ELSE @fieldLenW END
EXEC alterFieldSize @column = 'ClientAddress', @table = 'Client', @prsColumnLen = @fieldLen;
SET @fieldLenA = (SELECT MAX(LEN(s.PostalAddress)) FROM [DB2].dbo.Sites s)
SET @fieldLenW = (SELECT MAX(LEN(s.PostalAddress)) FROM [DB3].dbo.Sites s)
SET @fieldLen = CASE WHEN @fieldLenA > @fieldLenW THEN @fieldLenA ELSE @fieldLenW END
EXEC alterFieldSize @column = 'ClientPOAddress', @table = 'Client', @prsColumnLen = @fieldLen;
SET @fieldLenA = (SELECT MAX(LEN(s.Phone)) FROM [DB2].dbo.Sites s)
SET @fieldLenW = (SELECT MAX(LEN(s.Phone)) FROM [DB3].dbo.Sites s)
SET @fieldLen = CASE WHEN @fieldLenA > @fieldLenW THEN @fieldLenA ELSE @fieldLenW END
EXEC alterFieldSize @column = 'ClientTelephone', @table = 'Client', @prsColumnLen = @fieldLen;
提前致谢!