SQLServer实现两个库的字段长度自动更新
2021/12/31 2:08:49
本文主要是介绍SQLServer实现两个库的字段长度自动更新,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
-- 以src -- 1.src数据插入记录表Meta_Src_CharType SELECT T.TABLE_CATALOG,T.TABLE_NAME,T.TABLE_SCHEMA,T.TABLE_TYPE,C.COLUMN_NAME,C.DATA_TYPE,C.CHARACTER_MAXIMUM_LENGTH,C.CHARACTER_OCTET_LENGTH FROM DB_Src.INFORMATION_SCHEMA.TABLES T INNER JOIN DB_Src.INFORMATION_SCHEMA.COLUMNS C ON (T.TABLE_CATALOG = C.TABLE_CATALOG AND T.TABLE_SCHEMA = C.TABLE_SCHEMA AND T.TABLE_NAME = C.TABLE_NAME) WHERE T.TABLE_TYPE = 'BASE TABLE' AND C.DATA_TYPE LIKE '%CHAR%' -- 2.对比ODS层,生成ALTER语句,插入Meta_log_AutoModify表 SELECT A.* ,B.TABLE_CATALOG NewTABLE_CATALOG ,B.TABLE_SCHEMA NewTABLE_SCHEMA ,B.CHARACTER_MAXIMUM_LENGTH NewCHARACTER_MAXIMUM_LENGTH ,B.CHARACTER_OCTET_LENGTH NewCHARACTER_OCTET_LENGTH ,getdate() ModifyDate ,'ALTER TABLE ['+B.TABLE_CATALOG+'].['+B.TABLE_SCHEMA+'].[' +B.TABLE_NAME+ '] ALTER COLUMN [' + B.COLUMN_NAME + '] ' + B.DATA_TYPE + '('+CASE WHEN A.CHARACTER_MAXIMUM_LENGTH = -1 THEN 'MAX' ELSE CAST(A.CHARACTER_MAXIMUM_LENGTH AS VARCHAR) END +')' EXECSQL FROM (SELECT *,ROW_NUMBER() OVER(PARTITION BY A.TABLE_NAME,COLUMN_NAME ORDER BY CHARACTER_MAXIMUM_LENGTH DESC ) RN FROM Meta_Src_CharType A) A INNER JOIN DB_ODS.INFORMATION_SCHEMA.COLUMNS B ON (A.TABLE_NAME = B.TABLE_NAME AND A.COLUMN_NAME = B.COLUMN_NAME AND A.DATA_TYPE = B.DATA_TYPE) WHERE A.RN = 1 AND CASE WHEN A.CHARACTER_MAXIMUM_LENGTH=-1 THEN 999999 ELSE A.CHARACTER_MAXIMUM_LENGTH END > CASE WHEN B.CHARACTER_MAXIMUM_LENGTH=-1 THEN 999999 ELSE B.CHARACTER_MAXIMUM_LENGTH END -- 3.执行步骤2中的EXECSQL
这篇关于SQLServer实现两个库的字段长度自动更新的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-01-08Docker下的SqlServer发布订阅启用
- 2023-06-05Docker安装MS SQL Server并使用Navicat远程连接
- 2023-05-25深入浅出 SQL Server CDC 数据同步
- 2023-05-12通过空间占用和执行计划了解SQL Server的行存储索引
- 2023-04-24以SQLserver为例的Dapper详细讲解
- 2022-11-30SQL server高级函数查询
- 2022-11-26SQL SERVER数据库服务器CPU不能全部利用原因分析
- 2022-11-21SQL Server 时间算差值/常用函数
- 2022-11-20调试Archery连接SQL Server提示驱动错误
- 2022-10-22SQL Server 完整、差异备份+完整、差异还原(详细讲解,规避错误)