zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    redis 内部数据结构 ziplist
    redis 内部数据结构 robj
    redis 内部数据结构 sds
    redis 内部数据结构 dict
    【工具篇 intellij idea】像eclipse的 Ctrl + T 那样查看类的继承结构
    【分享系列】+【提炼总结】一文掌控面试必备的TCP知识
    【搬砖系列】一图搞懂 取余 和 取模 运算
    IOS代理(初级)
    UITableView
    UITextField属性
  • 原文地址:https://www.cnblogs.com/kylan/p/15749383.html
Copyright © 2011-2022 走看看