﻿--同步重阅申请表结构后，再维护该字段

UPDATE VisitTaskReReading
SET TrialId = VisitTask.TrialId
FROM VisitTaskReReading 
INNER JOIN VisitTask ON VisitTaskReReading.OriginalReReadingTaskId = VisitTask.Id;

delete VisitTaskReReading where TrialId is NULL


--数据库列字符串列 长度 
SELECT t.name AS table_name, 
       c.name AS column_name,
       d.name AS constraint_name,
       ty.Name as typeName, 
       c.max_length as length
FROM sys.tables t
INNER JOIN sys.columns c ON  c.object_id = t.object_id
LEFT JOIN sys.default_constraints d ON d.parent_object_id = c.object_id AND d.parent_column_id = c.column_id
INNER JOIN sys.types ty on ty.system_type_id = c.system_type_id 
AND ty.name in ('nvarchar', 'varchar', 'char', 'nchar')
WHERE c.max_length < 100 AND c.max_length > 0

--带维护sql 的查询

SELECT t.name AS table_name, 
       c.name AS column_name,
       d.name AS constraint_name,
       ty.Name as typeName, 
       c.max_length as length,
       'ALTER TABLE ' + QUOTENAME(t.name) + ' DROP CONSTRAINT [' + d.name + ']' AS drop_constraint_sql,			 
       CASE WHEN c.max_length = -1 THEN 'ALTER TABLE ' + QUOTENAME(t.name) + ' ALTER COLUMN ' + QUOTENAME(c.name) + ' NVARCHAR(MAX)' +  CASE WHEN c.is_nullable = 1 THEN ' NULL' ELSE ' NOT NULL' END 
            WHEN c.max_length > 0 AND c.max_length < 100 THEN 'ALTER TABLE ' + QUOTENAME(t.name) + ' ALTER COLUMN ' + QUOTENAME(c.name) + ' NVARCHAR(' + CAST(100 AS VARCHAR(10)) + ')' +  CASE WHEN c.is_nullable = 1 THEN ' NULL' ELSE ' NOT NULL' END 
            WHEN c.max_length = 8000 THEN 'ALTER TABLE ' + QUOTENAME(t.name) + ' ALTER COLUMN ' + QUOTENAME(c.name) + ' NVARCHAR(' + CAST(4000 AS VARCHAR(10)) + ')' +  CASE WHEN c.is_nullable = 1 THEN ' NULL' ELSE ' NOT NULL' END 
            ELSE 'ALTER TABLE ' + QUOTENAME(t.name) + ' ALTER COLUMN ' + QUOTENAME(c.name) + ' ' + ty.name + '(' + CAST(c.max_length AS VARCHAR(10)) + ')' +  CASE WHEN c.is_nullable = 1 THEN ' NULL' ELSE ' NOT NULL' END 
       END AS AlterColumnSQL,
       'ALTER TABLE ' + QUOTENAME(t.name) + ' ADD CONSTRAINT ' +'DF_'+t.name+ '_'+ c.name +  ' DEFAULT ' + d.definition + ' FOR ' + QUOTENAME(c.name) AS add_constraint_sql
FROM sys.tables t
INNER JOIN sys.columns c ON  c.object_id = t.object_id
LEFT JOIN sys.default_constraints d ON d.parent_object_id = c.object_id AND d.parent_column_id = c.column_id
INNER JOIN sys.types ty on ty.system_type_id = c.system_type_id 
AND ty.name in ('nvarchar', 'varchar', 'char', 'nchar')
and (ty.name != 'nvarchar' or (ty.name = 'nvarchar' and (c.max_length = -1 or (c.max_length < 100 and c.max_length > 0))))



--修改默认约束名
SELECT 
    OBJECT_SCHEMA_NAME(dc.parent_object_id) AS schema_name,
    OBJECT_NAME(dc.parent_object_id) AS table_name,
    dc.name AS constraint_name,
    COL_NAME(dc.parent_object_id, dc.parent_column_id) AS column_name,
    CONCAT('EXEC sp_rename N''', dc.name, ''', N''DF_', OBJECT_NAME(dc.parent_object_id), '_', COL_NAME(dc.parent_object_id, dc.parent_column_id), '''') AS executable_sql
FROM 
    sys.default_constraints dc
WHERE 
    dc.is_system_named = 1 -- 只查询自动生成的系统默认约束
    AND dc.name NOT LIKE 'DF\[_%]\[_%]' ESCAPE '\'; -- 排除符合 "DF_表名_列名" 格式的约束名

    SELECT 
       sys.tables.name AS table_name,
       sys.columns.name AS column_name,
       sys.default_constraints.name AS constraint_name,
			 'EXEC sp_rename N''' + sys.default_constraints.name + ''', N''DF_' + sys.tables.name + '_' + sys.columns.name + ''''
          AS executable_sql
FROM sys.default_constraints
INNER JOIN sys.tables ON sys.default_constraints.parent_object_id = sys.tables.object_id
INNER JOIN sys.columns ON sys.default_constraints.parent_column_id = sys.columns.column_id AND sys.tables.object_id = sys.columns.object_id


  --修改主键
  SELECT
    OBJECT_NAME(i.object_id) AS '表名',
    COL_NAME(ic.object_id, ic.column_id) AS '列名',
    i.name AS '索引名',
    CONCAT('EXEC sp_rename ''', i.name, ''', ''PK_', OBJECT_NAME(i.object_id), '_',
           COL_NAME(ic.object_id, ic.column_id), '''') 
           AS '修改索引名为 PK_表名_字段名',
    i.is_primary_key AS '是否主键'
FROM 
    sys.indexes i 
INNER JOIN 
    sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id
WHERE 
    (i.is_primary_key = 1 OR i.is_unique_constraint = 1)
    AND OBJECTPROPERTY(i.object_id, 'IsSystemTable') = 0
    AND OBJECTPROPERTY(i.object_id, 'IsMSShipped') = 0
    AND OBJECT_NAME(i.object_id) <> 'sysdiagrams';




    ---------------------------维护临床数据是否应用  之前默认都是应用  现在添加字段  把状态都改为应用
    update ClinicalDataTrialSet set IsApply=1 