Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Administration (2000)
 How to extract schema inside the SP

Author  Topic 

rubs_65
Posting Yak Master

144 Posts

Posted - 2004-11-03 : 15:10:25
Hi,

Inside the SP We need to store the schema of table in our own schema table having column old_schema nvarchar(4000). how can we extract the schema of a table..so far i use EM or object browser to extract schema ...do we have it stored as a metadata that we can extract and store the sql in a column....i am looking into alternative other than query information_schema.columns and do some string manipulation..etc.

Thanks
--Harvinder

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2004-11-03 : 15:22:30
Why would you have to get the table metadata every time ?
Do it once and store it any way you want.

rockmoose
Go to Top of Page

rubs_65
Posting Yak Master

144 Posts

Posted - 2004-11-03 : 15:24:41
Schema of this table changes 1-2 every month so we need to store the historical information for the schema

Thanks
--Harvinder
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2004-11-03 : 16:22:36
I would probably just use INFORMATION_SCHEMA, this makes use of "undocumented" sprocs_


-- GENERATES CREATE TABLE DDL for @mytable
-- does not generate defaults or any constraints
declare @mytable sysname
set @mytable = '....'

declare @obj_id int, @colid int, @md varchar(8000), @n sysname, @t nvarchar(4000)
select @obj_id = object_id(@mytable), @colid = 1

exec sp_MSget_qualified_name @obj_id, @md output

set @md = 'CREATE TABLE ' + @md + '('
while @colid is not null
begin
exec sp_MSget_type @obj_id, @colid, @n output, @t output
set @md = @md + ' ' + @n + ' ' + @t + ' '
select @md = @md + case isnullable when 0 then 'not null' else 'null' end from syscolumns where id = @obj_id and colid = @colid
set @colid = ( select min(colid) from syscolumns where id = @obj_id and colid > @colid )
set @md = @md + case when @colid is null then ')' else ',' end
end

select coalesce( @md, 'table ' + @mytable + ' does not exist' )


rockmoose
Go to Top of Page
   

- Advertisement -