请协助。我正在尝试编写一个脚本来更改表中具有依赖项的列的数据类型。所以我决定组合脚本并从创建临时表开始,将数据从正确的表中移开,这样我就可以删除它,然后使用相关列的新数据类型重新创建它。组合的脚本执行得很好,但现在它们似乎锁定了数据库中的其他事务,当我关闭脚本时,我得到标题中的错误。这是我的代码......
SET NOCOUNT ON
BEGIN TRY
BEGIN TRAN
PRINT '1.Drop Temporary Table'
IF EXISTS (SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'EmptyDepot.StoreStockTakeTemp')
AND type in (N'U'))
DROP TABLE EmptyDepot.StoreStockTakeTemp
PRINT '====Table Dropped'
PRINT '2.Create Temporary Table'
CREATE TABLE EmptyDepot.StoreStockTakeTemp
(
StoreStockTakeID INT NOT NULL,
CntrNum VARCHAR(11) NOT NULL,
DepotID INT NOT NULL,
StockCaptureDate DATE NOT NULL,
ChUser VARCHAR(50) NOT NULL,
ChDate SMALLDATETIME NOT NULL,
ChStamp TIMESTAMP NOT NULL,
)
PRINT '====Temporary Table Created'
PRINT '3.Populate Temporary Table'
INSERT INTO EmptyDepot.StoreStockTakeTemp
(
StoreStockTakeID,
CntrNum,
DepotID,
StockCaptureDate,
ChUser,
ChDate
)
SELECT StoreStockTakeID,
CntrNum,
DepotID,
StockCaptureDate,
ChUser,
ChDate
FROM EmptyDepot.StoreStockTake
PRINT '====Temporary Table Populated'
PRINT 'Begin Transaction'
PRINT '================='
PRINT '4.Drop Table'
IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[EmptyDepot].[PK_StoreStockTakeID]') AND parent_object_id = OBJECT_ID(N'[EmptyDepot].[StoreStockTake]'))
ALTER TABLE [EmptyDepot].[StoreStockTake] DROP CONSTRAINT [PK_StoreStockTakeID]
IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[EmptyDepot].[UX_CntrNum_DepotID]') AND parent_object_id = OBJECT_ID(N'[EmptyDepot].[StoreStockTake]'))
ALTER TABLE [EmptyDepot].[StoreStockTake] DROP CONSTRAINT [UX_CntrNum_DepotID]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[EmptyDepot].[StoreStockTake]') AND type in (N'U'))
DROP TABLE [EmptyDepot].[StoreStockTake]
PRINT '====Table Dropped'
PRINT '5.Re-Create Table'
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
SET ANSI_PADDING ON
CREATE TABLE EmptyDepot.StoreStockTake
(
StoreStockTakeID INT NOT NULL,
CntrNum VARCHAR(11) NOT NULL,
DepotID INT NOT NULL,
StockCaptureDate DATE NOT NULL,
ChUser VARCHAR(50) NOT NULL,
ChDate SMALLDATETIME NOT NULL,
ChStamp TIMESTAMP NOT NULL,
CONSTRAINT PK_StoreStockTakeID PRIMARY KEY CLUSTERED
(
StoreStockTakeID ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON "PRIMARY"
) ON "PRIMARY"
PRINT '====Table Created'
PRINT '6.Populate Table'
INSERT
INTO EmptyDepot.StoreStockTake
(
StoreStockTakeID,
CntrNum,
DepotID,
StockCaptureDate,
ChUser,
ChDate
)
SELECT StoreStockTakeID,
CntrNum,
DepotID,
StockCaptureDate,
ChUser,
ChDate
FROM EmptyDepot.StoreStockTakeTemp
PRINT '====Table Populated'
COMMIT TRAN
PRINT 'Transaction Committed'
PRINT '====================='
END TRY
BEGIN CATCH
PRINT ERROR_NUMBER()
PRINT ERROR_MESSAGE()
PRINT ERROR_LINE()
ROLLBACK
PRINT 'Transaction Rolled Back'
PRINT '======================='
END CATCH
答案 0 :(得分:0)
脚本有一些问题..
关于未通信的事务 - 无法看到原因,但也许您在执行所描述的命令之前启动了一些事务?