/******************************************************************************************************* * * Decription: 用於修改處理數據表字段類型 * 1) 要求先刪除對應字段的默認約束 * 2) 修改字段類型,從新添加默認約束 * 3) 修改配置表的信息爲 INPUT 類型 ALTER TABLE PdStandardArtMain DROP CONSTRAINT DF_PdStandardArtMain_Is_TransferAXGOALTER TABLE PdStandardArtMain ALTER COLUMN Is_TransferAX INT GOALTER TABLE PdStandardArtMain ADD CONSTRAINT DF_PdStandardArtMain_Is_TransferAX DEFAULT 0 FOR Is_TransferAX GO ********** 須要處理的數據庫 ********** * 請手動更改 USE 下的幾個數據庫 ******************************************************************************************************/ USE DB GO DECLARE @Table_Name NVARCHAR(50) = N''DECLARE @Column_Name NVARCHAR(50) = N'Is_TransferAX'DECLARE @Constraint_Name NVARCHAR(100) = N''DECLARE @SQLCMD NVARCHAR(2000) = N''DECLARE @strCRLF NVARCHAR(10) = NCHAR(13) + NCHAR(10) --查找當前數據庫下全部表 Is_TransferAX 字段的類型SELECT ST.[name] AS "Table_Name", SC.[name] AS "Column_Name", SD.definition AS "Default_Value", SD.[name] AS "Constraint_Name" INTO #tmp_ReTypeColumn FROM sys.tables ST INNER JOIN sys.syscolumns SC ON ST.[object_id] = SC.[id] INNER JOIN sys.default_constraints SD ON ST.[object_id] = SD.[parent_object_id] AND SC.colid = SD.parent_column_id WHERE SC.NAME = 'Is_TransferAX' ORDER BY ST.[name], SC.colid --用一個本地遊標來處理DECLARE cur_ReTypeColumn CURSOR LOCAL FOR SELECT Table_Name, Column_Name, Constraint_Name FROM #tmp_ReTypeColumn OPEN cur_ReTypeColumn fetch next from cur_ReTypeColumn INTO @Table_Name, @Column_Name, @Constraint_Namewhile @@fetch_Status=0 begin SET @SQLCMD += N' ALTER TABLE dbo.' + @Table_Name + N' DROP CONSTRAINT ' + @Constraint_Name + N' ;' + @strCRLF + N' ALTER TABLE dbo.' + @Table_Name + N' ALTER COLUMN ' + @Column_Name + N' INT ' + N' ;' + @strCRLF + N' ALTER TABLE dbo.' + @Table_Name + N' ADD CONSTRAINT DF_' + @Table_Name + @Column_Name + N' DEFAULT 0 FOR ' + @Column_Name + N';' + @strCRLF + N' ' + @strCRLF fetch next from cur_ReTypeColumn INTO @Table_Name, @Column_Name, @Constraint_Nameend CLOSE cur_ReTypeColumn DEALLOCATE cur_ReTypeColumn --開始更新到正式庫BEGIN TRANEXEC (@SQLCMD) --同步配置表的信息爲普通類型,不是CheckBox類型UPDATE systemdb..pbTableFieldList SET Type='INPUT' WHERE Table_Name='pbYarnBasalProcess' AND Field_Name = 'Is_TransferAX'UPDATE systemdb..pbTableFieldList SET Type='INPUT' WHERE Table_Name='pbYarnProcessList' AND Field_Name = 'Is_TransferAX' UPDATE systemdb..pbTableFieldList SET Type='INPUT' WHERE Table_Name='pbYarnBasalComp' AND Field_Name = 'Is_TransferAX'UPDATE systemdb..pbTableFieldList SET Type='INPUT' WHERE Table_Name='pbYarnCompList' AND Field_Name = 'Is_TransferAX' UPDATE systemdb..pbTableFieldList SET Type='INPUT' WHERE Table_Name='pbYarnProductList' AND Field_Name = 'Is_TransferAX'UPDATE systemdb..pbTableFieldList SET Type='INPUT' WHERE Table_Name='pbYarnTypeMapping' AND Field_Name = 'Is_TransferAX' UPDATE systemdb..pbTableFieldList SET Type='INPUT' WHERE Table_Name='pbYarnContentList' AND Field_Name = 'Is_TransferAX'UPDATE systemdb..pbTableFieldList SET Type='INPUT' WHERE Table_Name='pbYarnCountList' AND Field_Name = 'Is_TransferAX' IF @@ERROR<>0 BEGIN ROLLBACK PRINT N'更新過程當中出現錯誤,請檢查....' + @strCRLF PRINT @strCRLF + @SQLCMD + @strCRLF ENDELSEBEGIN COMMIT PRINT @strCRLF + N'處理完成....'END DROP TABLE #tmp_ReTypeColumn RETURN