我有以下几点:6 Z& v. m% k4 B0 ]) P7 w/ K& J9 M
IF OBJECT_ID(N'[dbo].[webpages_Roles_UserProfiles_Target]', 'xxxxx') IS NOT NULL0 `) U% ]" F- c9 L# C+ M2 p# y4 G
DROP CONSTRAINT [dbo].[webpages_Roles_UserProfiles_Target] * y% K8 N2 ^$ s( y( c. k( V我希望能够在删除约束之前检查是否存在约束。我在表中使用上面的代码,类型为“ U”。" |* x- a' W, I( O: @
我如何修改上面的代码(更改xxxx)以使其检查约束的存在?% f2 [) z+ V" V; b" R7 d6 r
* T% H% Q6 k9 g2 Y解决方案:8 y+ i, p4 V D# |/ F( j. @
. a; k _) {; H4 T: F
% J4 k5 p% ?& R1 w, L. ^2 @/ d! \* u6 T9 V3 P
SELECT( }; p0 S; @3 s' J1 n
* 2 g9 t4 f* ~9 j' d J2 I FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS 6 p# r n1 N) m, `, W1 m
否则尝试一下 * h9 M* _9 G2 N$ n; L0 }% u SELECT OBJECT_NAME(OBJECT_ID) AS NameofConstraint, E1 X$ |) A) c! nSCHEMA_NAME(schema_id) AS SchemaName,& y. K# @/ \: a$ A% m6 K" z4 D
OBJECT_NAME(parent_object_id) AS TableName, # b; \. b2 f' H9 r9 Btype_desc AS ConstraintType$ y% d/ g4 ~/ z. J D8 u' c: t
FROM sys.objects 3 \$ v) c8 a, }* f- S, jWHERE type_desc LIKE '%CONSTRAINT' 2 W6 Z1 A; }4 P: u或者) P2 R) V7 E+ c1 G8 F0 L( X
IF EXISTS(SELECT 1 FROM sys.foreign_keys WHERE parent_object_id = OBJECT_ID(N'dbo.TableName'))9 ?2 C+ P( Y N' T
BEGIN & F9 l* `* N( S3 GALTER TABLE TableName DROP CONSTRAINT CONSTRAINTNAME ' k p1 `2 x8 f! L
END