/******************************************************************************************************* * * 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
时间: 2024-10-12 13:02:56