修正或添加字段默认值约束的名称
Posted 天晴了
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了修正或添加字段默认值约束的名称相关的知识,希望对你有一定的参考价值。
旧项目中的数据库约束名称不规范,写了个脚本重新修改。
1. 名称重新修改为已有的默认值约束的名称为‘DF_‘ + 表名 + 字段名;
2. 没有设置默认值约束的列设置默认值,字符串为空,数值为0;
3. 时间字段除了addtime不加默认值;
--修正或添加字段默认值约束的名称为DF_表名_字段 /* SELECT tabName= LOWER(O.name), --表名 colName=LOWER(C.name), --字段名 coltype= LOWER(T.name), --类型 dfVal=ISNULL(D.definition,N‘‘), --默认值 dfName=ISNULL(D.name,N‘‘) --默认值约束名称 ,dfSName = (‘DF_‘ + O.name + ‘_‘ + C.name) --默认值标准名称 FROM sys.columns C INNER JOIN sys.objects O ON C.[object_id]=O.[object_id] AND O.type=‘U‘ AND O.is_ms_shipped=0 INNER JOIN sys.types T ON C.user_type_id=T.user_type_id LEFT JOIN sys.default_constraints D ON C.[object_id]=D.parent_object_id AND C.column_id=D.parent_column_id AND C.default_object_id=D.[object_id] WHERE C.is_identity=0 --非标识列 AND C.is_computed= 0 --非计算列 And O.name IN ( ‘fxs_eshop_task‘) --表名 --And O.name IN ( ‘Ck_Proc_List‘,‘fxgw_sys_config‘,‘fxs_duizhang‘,‘Ck_CustomsDocking‘) --表名 --And C.name IN (‘Sort_Id‘,‘Type_Code‘)--字段名 Confirm_Time And C.name IN (‘Confirm_Time‘,‘addTime‘,‘settlementbegintime‘,‘hone‘,‘CkNo‘)--字段名 AND (D.definition LIKE ‘%NULL%‘) --获取为null的约束 ORDER BY O.name,C.name */ --修正已有的默认值约束的名称为‘DF_‘ + 表名 + 字段名 BEGIN TRAN declare Cols_Cursor cursor FOR SELECT tabName= LOWER(O.name), --表名 colName=LOWER(C.name), --字段名 coltype= LOWER(T.name), --类型 dfVal=ISNULL(D.definition,N‘‘), --默认值 dfName=ISNULL(D.name,N‘‘) --默认值约束名称 ,dfSName = (‘DF_‘ + O.name + ‘_‘ + C.name) --默认值标准名称 FROM sys.columns C INNER JOIN sys.objects O ON C.[object_id]=O.[object_id] AND O.type=‘U‘ AND O.is_ms_shipped=0 INNER JOIN sys.types T ON C.user_type_id=T.user_type_id LEFT JOIN sys.default_constraints D ON C.[object_id]=D.parent_object_id AND C.column_id=D.parent_column_id AND C.default_object_id=D.[object_id] WHERE C.is_identity=0 --非标识列 AND C.is_computed= 0 --非计算列 --And O.name IN ( ‘fxs_eshop_task‘) --表名 -- --And C.name IN (‘Sort_Id‘,‘Type_Code‘)--字段名 Confirm_Time -- And C.name IN (‘Confirm_Time‘,‘addTime‘,‘settlementbegintime‘,‘hone‘)--字段名 ORDER BY O.name,C.name open Cols_Cursor declare @tabName varchar(200), @colName varchar(200), @coltype varchar(200), @dfVal varchar(200), @dfName varchar(200), @dfSName varchar(200) fetch next from Cols_Cursor into @tabName , @colName , @coltype , @dfVal , @dfName , @dfSName while(@@fetch_status=0) begin --print @tabName + @dfSName DECLARE @todo INT,@defaultVal VARCHAR(200),@sql VARCHAR(2000); SELECT @todo = 0,@defaultVal = ‘NULL‘,@sql = ‘‘; IF( @coltype = ‘money‘ OR @coltype = ‘real‘ OR @coltype = ‘int‘ OR @coltype = ‘decimal‘ OR @coltype = ‘smallint‘ OR @coltype = ‘numeric‘ OR @coltype = ‘tinyint‘ OR @coltype = ‘float‘ OR @coltype = ‘bigint‘ OR @coltype = ‘bit‘) --字符串类型 BEGIN SELECT @todo = 1,@defaultVal = ‘‘‘‘‘‘; END ELSE IF ( (@coltype = ‘datetime‘ OR @coltype = ‘smalldatetime‘ OR @coltype = ‘datetime2‘) AND @colName = ‘addtime‘)--有些特殊的时间字段,比如发货时间、审核时间不要有默认值 BEGIN SELECT @todo = 1,@defaultVal = ‘GETDATE()‘; END ELSE IF ( @coltype = ‘uniqueidentifier‘) BEGIN SELECT @todo = 1,@defaultVal = ‘NEWID()‘; END ELSE IF ( @coltype = ‘nchar‘ OR @coltype = ‘char‘ OR @coltype = ‘nvarchar‘ OR @coltype = ‘varchar‘ OR @coltype = ‘text‘ OR @coltype = ‘ntext‘ ) BEGIN SELECT @todo = 1,@defaultVal = ‘‘‘‘‘‘; END IF(@dfName = ‘‘)--无默认值约束 BEGIN IF (@todo = 1) BEGIN SELECT @sql = ‘IF NOT exists (select * from sysobjects where id = object_id(N‘‘[‘ + @dfSName +‘]‘‘)) ALTER TABLE [‘ + @tabName + ‘] ADD CONSTRAINT ‘+@dfSName+‘ DEFAULT ‘+ @defaultVal +‘ FOR [‘+@colName+‘]‘ ; print ‘增加约束,‘ +@tabName + ‘表‘ + @colName + ‘无默认值约束,类型为‘ + @coltype + ‘,执行sql:‘ + @sql; EXEC(@sql); END ELSE BEGIN print ‘无约束但不增加,‘ +@tabName + ‘表‘ + @colName + ‘无默认值约束,类型为‘ + @coltype + ‘,不能修改;‘; END END ELSE BEGIN --约束名称不等于‘DF_‘ + 表名 + 字段名,默认值取原有的 IF(@dfName <> @dfSName) BEGIN SELECT @defaultVal = @dfVal; SELECT @sql = ‘ALTER TABLE [‘ + @tabName + ‘] DROP CONSTRAINT [‘+@dfName + ‘];‘ --删除已有约束 SELECT @sql = @sql + ‘ IF NOT exists (select * from sysobjects where id = object_id(N‘‘[‘ + @dfSName +‘]‘‘)) ALTER TABLE [‘ + @tabName + ‘] ADD CONSTRAINT ‘+@dfSName+‘ DEFAULT ‘+ @defaultVal +‘ FOR [‘+@colName+‘] ‘ ; SELECT @sql = @sql + ‘ Else ALTER TABLE [‘ + @tabName + ‘] ADD CONSTRAINT ‘+ (@dfSName+ ‘2‘) +‘ DEFAULT ‘+ @defaultVal +‘ FOR [‘+@colName+‘] ‘ ;--考虑到某些表和字段加起来正好一样,所以再后面加2; print (‘重建约束,‘ +@tabName + ‘表‘ + @colName + ‘有默认值约束‘ + @dfName + ‘,默认值‘ +@dfVal + ‘;删除后重建约束,执行sql:‘ + @sql); EXEC(@sql); -- END ELSE BEGIN print ‘维持约束不变,‘ + @tabName + ‘表‘ + @colName + ‘有默认值约束‘ + @dfName + ‘,默认值‘ +@dfVal + ‘;‘; END END fetch next from Cols_Cursor into @tabName , @colName , @coltype , @dfVal , @dfName , @dfSName end close Cols_Cursor deallocate Cols_Cursor ROLLBACK
以上是关于修正或添加字段默认值约束的名称的主要内容,如果未能解决你的问题,请参考以下文章
sqlserver 下 修改表的某个字段默认值语法是怎么样的