alter procedure DisplayTable(
@tab varchar(30)
,@query varchar(max) output
)
as
BEGIN
Declare @execution varchar(max) = 'select * from ' +@tab
declare @tempStructure as table (
pk_id int identity
,ColumnName varchar(max)
,ColumnDataType varchar(max)
)
insert into
@tempStructure
select
COLUMN_NAME
,DATA_TYPE
from
INFORMATION_SCHEMA.columns
where TABLE_NAME= @tab
EXEC(@execution)
declare @ColumnCount int = (SELECT count(*) from @tempStructure)
declare @counter int = 1
while @counter <= @ColumnCount
BEGIN
IF @counter = 1
BEGIN
set @query = (SELECT ColumnName + ' ' + ColumnDataType FROM @tempStructure where pk_id= @counter)
END
IF @counter <> 1
BEGIN
set @query = @query + (SELECT ',' + ColumnName + ' ' + ColumnDataType FROM @tempStructure where @counter = pk_id)
END
set @counter = @counter + 1
END
END
When you execute the SP, you'll now get a return of the structure of the table you want.
This should hopefully get you moving.
If you want the table CONTENTS included, create yourself a loop for the entries, and append them to the @query parameter.
Remember to delimit the @query, else when you read it later on, you will not be able to restructure your table.