如何解决如何定位列名的默认值约束
我注意到,当默认值为“0001-01-01”且未提供有效日期时,数据库表中的某些日期列给我的 Power App 带来了困难(修补)。请注意,我不能只在我的 Power App 中设置默认值,因为其他应用程序正在使用 sql 表。
我目前的想法是我使用 Tsql 将默认值更改为“1001-01-01”,以便我可以尽可能地自动化。
- 获取列的 (DF_TABLENAME_XXXX) 约束名称,然后将其推入变量 @mytablename
- 删除此约束名称
- 重新创建约束
ALTER TABLE @MyTable DEFAULT '1001-12-31' FOR @ColumnName
我在步骤 1) atm 中挣扎。此外,我很高兴采用比我概述的方法更好的方法。
解决方法
我前段时间写了这段代码来创建各种数据字典。它使用所有 sys 表来获取有关数据库中表的信息。只需更改最上面一行,您就应该准备就绪。
USE [INSERT YOUR DATABASE NAME HERE]
GO
SELECT
DB_NAME() AS [DatabaseName],OBJECT_SCHEMA_NAME(TBL.[object_id],DB_ID()) AS [Schema],TBL.[name] AS [TableName],AC.[name] AS [ColumnName],UPPER(TY.[name]) AS [DataType],AC.[max_length] AS [Length],AC.[precision] AS [Precision],AC.[scale] AS [Scale],AC.[is_nullable] AS [IsNullable],ISNULL(SI.is_primary_key,0) AS [IsPrimaryKey],SKC.name AS [PrimaryKeyConstarint],CASE
WHEN SIC.index_column_id > 0
THEN 1
ELSE 0
END AS [IsIndexed],ISNULL(is_included_column,0) AS [IsIncludedIndex],SI.name AS [IndexName],OBJECT_NAME(SFC.constraint_object_id) AS [ForeignKeyConstraint],OBJECT_SCHEMA_NAME(TBL_PRIM.[object_id],DB_ID()) AS [ParentTableSchema],OBJECT_NAME(SFC.referenced_object_id) AS [ParentTableName],COL_PRIM.name AS [ParentTableColumnName],DB_ID()) + '.' + TBL.[name] + '.' + AC.[name]
+ ' = ' + OBJECT_SCHEMA_NAME(TBL_PRIM.[object_id],DB_ID()) + '.'
+ OBJECT_NAME(SFC.referenced_object_id) + '.' + COL_PRIM.name AS [JoinCondition],SDC.name AS [DefaultConstraint],SDC.[definition] AS [DefaultValue],AC.collation_name AS [Collation],SEP.value AS [Comments]
FROM [sys].[tables] AS TBL
JOIN [sys].[all_columns] AS AC
ON TBL.[object_id] = AC.[object_id]
JOIN [sys].[types] AS TY
ON AC.[system_type_id] = TY.[system_type_id]
AND AC.[user_type_id] = TY.[user_type_id]
LEFT JOIN [sys].[index_columns] AS SIC
ON sic.[object_id] = TBL.[object_id]
AND AC.column_id = SIC.column_id
LEFT JOIN [sys].[indexes] AS SI
ON SI.[object_id] = TBL.[object_id]
AND SIC.index_id = SI.index_id
LEFT JOIN [sys].[foreign_key_columns] AS SFC
ON SFC.parent_object_id = TBL.[object_id]
AND SFC.parent_column_id = AC.column_id
LEFT JOIN [sys].[key_constraints] AS SKC
ON skc.parent_object_id = TBL.[object_id]
AND SIC.index_column_id = SKC.unique_index_id
LEFT JOIN [sys].[default_constraints] AS SDC
ON SDC.parent_column_id = AC.column_id
LEFT JOIN [sys].[extended_properties] AS SEP
ON SEP.major_id = TBL.[object_id]
AND SEP.minor_id = AC.column_id
LEFT JOIN [sys].[tables] AS TBL_PRIM
ON SFC.referenced_object_id = TBL_PRIM.[object_id]
LEFT JOIN [sys].[columns] AS COL_PRIM
ON SFC.referenced_object_id = COL_PRIM.[object_id]
AND SFC.referenced_column_id = COL_PRIM.column_id
,
在 where 子句中填写问号,这将为您提供默认约束的名称。
SELECT
SchemaName = SCH.[name],TableName = TAB.[name],ColumnName = COL.[name],DefaultConstraint = DFC.[name]
FROM sys.default_constraints AS DFC
JOIN sys.columns AS COL
ON DFC.parent_object_id = COL.[object_id]
AND DFC.parent_column_id = COL.column_id
JOIN sys.tables AS TAB
ON COL.[object_id] = TAB.[object_id]
JOIN SYS.schemas AS SCH
ON TAB.[schema_id] = SCH.[schema_id]
WHERE SCH.[name] = ?
AND TAB.[name] = ?
AND COL.[name] = ?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。