At first I rebelled against the very idea of doing this but I was eventually convinced and now all my tables have zero rows. This doesn’t mean there are no rows in the tables, this means that whenever I create a new table I insert a special row with a primary key of zero and default values in the other fields. So my table create scripts look something like this:
/*
**modified 2005/07/26 for SQL Server 2005 by Terry Grignon
**change row “set @TableName = ‘tblyourtable’” to whatever table you want to ‘zero’
Credits:
Tatsu & Bob Wiechman
*/
declare @TableName sysname
declare @IdentityInsert int
declare @ColName sysname
declare @ColType tinyint
declare @DebugMode bit
declare @ColList nvarchar(4000)
declare @ValList nvarchar(4000)
declare @NewList nvarchar(4000)
declare @SQL1 nvarchar(4000)
declare @SQL2 nchar(10)
declare @SQL3 nchar(1000)
set @TableName = ‘tblyourtable’
set @IdentityInsert = 1 — set to 0 if you DON’T want IDENTITY_INSERT turned on
set @DebugMode = 1 — set to 1 if you only want a script
set @ColList = ”
set @ValList = ”
set @SQL1 = ‘insert into ‘ + @TableName + ‘ (‘
set @SQL2 = ‘) values (‘
set @SQL3 = ‘)’
–set @SQL3 = ‘)”, ”””null”””, ”null”) from ‘ + @TableName
–print @sql1 + @sql2 + ‘)’
print ‘cut below’
declare csrColumns cursor local fast_forward for
select c.name, c.system_type_id –xtype
from sys.columns c –select * from sys.columns where name = ‘vbarpassword’
inner join sys.objects o –select * from sys.objects
on o.object_id = c.object_id
where o.name = @TableName
and o.type in (‘U’, ‘S’)
order by Column_id
open csrColumns
fetch next from csrColumns into @ColName, @ColType
while @@fetch_status = 0
begin
– uncomment line below to see the numeric types
– print @ColName + ‘ ‘ + convert (varchar (10), @ColType)
set @ColList = @ColList + ‘ ‘ + @ColName
if @ColType in (173, 62, 60, 108, 59, 122)
set @ValList = @ValList + ‘~’
if @ColType in (165)
set @ValList = @ValList + ‘convert(varbinary,0)~’
– set @ValList = @ValList + ‘ ”+convert(varchar(200),’ + @ColName + ‘)+”’
– uid and string types
else if @ColType in (175, 239, 48, 52, 56, 36, 167)
set @ValList = @ValList + ‘0~’
else if @ColType in (106)
set @ValList = @ValList + ‘0.0~’
else if @ColType in (36, 61, 104, 231)
set @ValList = @ValList + ‘default~’
– set @ValList = @ValList + ‘ ”””+isnull(‘ + @ColName + ‘,”null”)+”””’
– dates (does not support nulls yet)
else if @ColType in (58)
set @ValList = @ValList + ‘~’
– set @ValList = @ValList + ‘ ”””+convert(varchar(200),’ + @ColName + ‘)+”””’
fetch next from csrColumns into @ColName, @ColType
end
close csrColumns
deallocate csrColumns
set @ValList =@ValList + ‘~’
set @ColList = replace(ltrim(@ColList), ‘ ‘, ‘, ‘)
set @ValList = replace(ltrim(@ValList), ‘~~’, ”)
set @ValList = replace(ltrim(@ValList), ‘~’, ‘, ‘)
if @IdentityInsert = 1
print ’set identity_insert ‘ + @TableName + ‘ on’
if @DebugMode = 1
Begin
set @NewList = @SQL1 + @ColList + @SQL2 + @ValList + @SQL3
set @NewList = rtrim(@NewList)
print @NewList
end
else
exec (@SQL1 + @ColList + @SQL2 + @ValList + @SQL3)
if @IdentityInsert = 1
print ’set identity_insert ‘ + @TableName + ‘ off’
print ‘cut before’