Generate all database default DDL

,

This will generate all default DDL in a given database.

declare @DefName varchar(128)
declare @OwnerName varchar(128)
declare @DefID int
declare @RowCount int
declare @CountStatus int
declare @DDL varchar(8000)

declare DefCur cursor for
	select so.name, su.name, so.id
	from sysobjects so inner join sysusers su
	  on so.uid = su.uid
	  inner join syscomments sc on sc.id = so.id
	where so.type = 'D'
	and so.id not in (select constid from sysconstraints)
	order by so.name

open DefCur

fetch next from DefCur into @DefName, @OwnerName, @DefID

while @@FETCH_STATUS = 0
	begin
		select @DDL = ctext 
		from syscomments 
		where id = @DefID 
					
		PRINT @DDL
	    PRINT 'GO' + Char(13)

		fetch next from DefCur into @DefName, @OwnerName, @DefID
	end -- @@Fetch = 0 for ViewCur
close DefCur
deallocate DefCur

Rate

Share

Share

Rate