AZGroups.com

Bringing together Arizona Technology Users And Enhancing the Careers of Developers
Welcome to AZGroups.com Sign in | Join | Help
in Search

delete set null and objectproperty

Last post 06-05-2007, 12:21 PM by kenhales. 0 replies.
Sort Posts: Previous Next
  •  06-05-2007, 12:21 PM 780

    delete set null and objectproperty

    I have a situation where I need to use a DELETE SET NULL constraint on a foreign key.  I have implemented it and it works fine.

    My problem is, I need some DDL T-SQL that looks at the current foreign key in the database to determine if it is using the DELETE SET NULL attribute.  If not, I want to drop the foreign key and re-create it with the attribute.

    Something like this:

    DECLARE @SqlString varchar(1000)

    @SqlString varchar(1000)

    DECLARE @ConstraintName varchar(100), @isDelete bit, @isCascadeDelete bit

    SELECT @ConstraintName = object_name(constid), @isCascadeDelete = OBJECTPROPERTY(constid, N'CnstIsDeleteCascade')

    @ConstraintName varchar(100), @isDelete bit, @isCascadeDelete bit

    SELECT @ConstraintName = object_name(constid), @isCascadeDelete = OBJECTPROPERTY(constid, N'CnstIsDeleteCascade')

    @ConstraintName = object_name(constid), @isCascadeDelete = OBJECTPROPERTY(constid, N'CnstIsDeleteCascade')

    FROM sysreferences INNER JOIN sysobjects f ON sysreferences.fkeyid = f.id

    sysreferences INNER JOIN sysobjects f ON sysreferences.fkeyid = f.id

    INNER JOIN sysobjects r ON sysreferences.rkeyid = r.id

    JOIN sysobjects r ON sysreferences.rkeyid = r.id

    WHERE f.name = 'ScheduledServices' AND r.name = 'Transactions'

    f.name = 'ScheduledServices' AND r.name = 'Transactions'

    IF @ConstraintName IS NOT NULL AND @isCascadeDelete = 0

    @ConstraintName IS NOT NULL AND @isCascadeDelete = 0

    BEGIN

    SET @SqlString = 'ALTER TABLE ScheduledServices DROP CONSTRAINT ' + @ConstraintName

    EXECUTE (@SqlString)

    SET @SqlString = 'ALTER TABLE ScheduledServices DROP CONSTRAINT ' + @ConstraintName

    EXECUTE (@SqlString)

    EXECUTE (@SqlString)

    SET @ConstraintName = NULL

    SET @ConstraintName = NULL

    END

    -- Check to see if constraint with cascade set null exists. If not, add it.

    IF @ConstraintName IS NULL

    @ConstraintName IS NULL

    BEGIN

    EXECUTE ('ALTER TABLE ScheduledServices ADD CONSTRAINT FK_ScheduledServices_TransactionId'

    EXECUTE ('ALTER TABLE ScheduledServices ADD CONSTRAINT FK_ScheduledServices_TransactionId'

    + ' FOREIGN KEY (TransactionId) REFERENCES Transactions (TransactionId) ON DELETE SET NULL')

    + ' FOREIGN KEY (TransactionId) REFERENCES Transactions (TransactionId) ON DELETE SET NULL')

    END

    My problem is that the value CnstIsDeleteCascade is not the objectproperty I want.  I wan the one that designates the constraint as a "delete set null" constraint.  Google and MSDN searches have not turned up what the magic value is.  Or perhaps there is another way?

    Any help would be appreciated!

    Ken

  • View as RSS news feed in XML
    Powered by Community Server, by Telligent Systems