James
My wife has a request for a copy of a database where each table only has 10 rows in it.
I suggested she loop through the tables, something like this:
declare @table varchar(255),
@stgtable varchar(255),
@columns varchar(max),
@sql nvarchar(max)
declare c cursor for
select table_name from INFORMATION_SCHEMA.tables
open c
fetch from c into @table
while @@FETCH_STATUS = 0
begin
set @sql = 'alter table dbo.' + @table + ' nocheck constraint all'
print @sql
--exec sp_executesql @sql
select @columns = stuff( (
select ', ' + Column_Name from information_schema.columns
where Table_Name = @table
for xml path ('')),1,2,'')
set @sql = 'use sampledb; select top 10 ' + @columns + ' into ' + @table + ' from
reportDB.dbo.' + @table
print @sql
--exec sp_executesql @sql
fetch next from c into @table
end
close c
deallocate c
but this seems kind of loopy, if you know what I mean.
**QUESTION** is there a more straightforward way?