oops. here you go. I have excluded xml and timestamp columns because the target data column is nvarchar(max). You will have to sort that out.
use sqlteam
go
declare @id int ;
create table #bootleg(id int identity(1,1),
tablename varchar(max),
columnname varchar(max),
dada nvarchar(max))
DECLARE @tablename VARCHAR(MAX),
@columnname VARCHAR(MAX),
@query VARCHAR(MAX)
DECLARE cursor_zoro CURSOR
FOR
select t.name as tablename,
c.name columnname,
'select top 100 ' + c.name + ' from [' + t.name + ']' as query
from sys.tables t
join sys.columns c
on t.object_id = c.object_id
join sys.types tt on tt.user_type_id = c.user_type_id
where tt.name not in ('timestamp', 'xml')
create table #data(dada nvarchar(max))
OPEN cursor_zoro;
FETCH NEXT FROM cursor_zoro INTO
@tablename,
@columnname,
@query;
WHILE @@FETCH_STATUS = 0
BEGIN
insert into #data(dada)
exec(@query)
;with src
as
(
select dada, @tablename as tablename, @columnname as columnname
from #data
)
insert into #bootleg(tablename, columnname, dada)
select tablename, columnname, dada
from src
truncate table #data;
FETCH NEXT FROM cursor_zoro INTO
@tablename,
@columnname,
@query;
END;
CLOSE cursor_zoro;
DEALLOCATE cursor_zoro;
select * From #bootleg
drop table #bootleg