Script for Default Constraints

  • does anyone have a script to extract default constraints only?

    ¤ §unshine ¤

  • Select * from sys.default_constraints

  • I'm sorry, this is for sql server 2000 and I need a script that will create the alter statements to add them to the database in case of loss.

    ¤ §unshine ¤

  • See if this helps.

    Create Table #Temp

    (TableName varchar(100),

    ColumnName varchar(100),

    DefaultName varchar(500),

    DefaultText varchar(100)

    )

    INSERT INTO #Temp

    Exec sp_msforeachtable'

    Select ''?'' TableName ,c.Name, object_name(c.cdefault) DefaultName,

    t.text

    from dbo.syscolumns c

    join dbo.syscomments t on t.id = c.cdefault and t.colid = 1

    and not exists (select * from dbo.syscomments where id = c.cdefault and colid = 2)

    '

    Select * from #Temp

    Drop Table #Temp

  • thats great. Is there a way to add the alter table add constraint in there?

    ¤ §unshine ¤

  • Create Table #Temp

    (Script varchar(1000)

    )

    INSERT INTO #Temp

    Exec sp_msforeachtable'

    Select ''Alter Table '' + ''?'' + '' ADD CONSTRAINT '' + object_name(c.cdefault) + '' DEFAULT '' + t.text + '' FOR '' + c.Name AS Script

    from dbo.syscolumns c

    join dbo.syscomments t on t.id = c.cdefault and t.colid = 1

    and not exists (select * from dbo.syscomments where id = c.cdefault and colid = 2)

    '

    Select * from #Temp

    Drop Table #Temp

  • You are awesome! Thank you so much! You should post as a script in the scripts section.

    Thanks again!

    ¤ §unshine ¤

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply