728x90
ALTER PROC USP_TAB_INFO (
@DBNAME VARCHAR(100)=''
, @TABLENAME VARCHAR(100) = ''
)
AS
BEGIN
SET NOCOUNT ON
DECLARE @SQL VARCHAR(4000)
IF @TABLENAME=''
set @sql='select b.name ColumnName, c.name TypeName, b.length ColumnLength
, Case b.IsNullable when 1 then '''' else ''Not Null'' end Nullable
, Case When g.name is null Then '''' Else ''PK'' End PKwithCidx
, IsNull(d.value,'''') Caption
from '+@DbName+'..sysobjects a
JOIN '+@DbName+'..syscolumns b ON (a.id = b.id and a.type=''U'' and a.status > 0)
JOIN '+@DbName+'..systypes c ON (b.xtype = c.xtype and c.name<>''sysname'')
LEFT JOIN '+@DbName+'..sysproperties d ON (b.id=d.id and b.colid = d.smallid)
LEFT JOIN (select name,e.id,colid from '+@DbName+'..sysindexes e join '+@DbName+'..sysindexkeys f on e.id=f.id
where e.id > 10000000 and e.indid=1 and f.indid=1) g ON (g.id=b.id and g.colid=b.colid)
order by a.name, colorder'
ELSE
set @sql='select b.name ColumnName, c.name TypeName, b.length ColumnLength
, Case b.IsNullable when 1 then '''' else ''Not Null'' end Nullable
, Case When g.name is null Then '''' Else ''PK'' End PKwithCidx
, IsNull(d.value,'''') Caption
from '+@DbName+'..sysobjects a
JOIN '+@DbName+'..syscolumns b ON (a.id = b.id and a.type=''U'' and a.status > 0 and a.name='''+@TableName+''')
JOIN '+@DbName+'..systypes c ON (b.xtype = c.xtype and c.name<>''sysname'')
LEFT JOIN '+@DbName+'..sysproperties d ON (b.id=d.id and b.colid = d.smallid)
LEFT JOIN (select name,e.id,colid from '+@DbName+'..sysindexes e join '+@DbName+'..sysindexkeys f on e.id=f.id
where e.id > 10000000 and e.indid=1 and f.indid=1) g ON (g.id=b.id and g.colid=b.colid)
order by a.name, colorder'
EXEC(@SQL)
SET NOCOUNT OFF
END
--USE INSIDER
--GO
--EXEC USP_TAB_INFO 'INSIDER', 'INSIDER_1'
@DBNAME VARCHAR(100)=''
, @TABLENAME VARCHAR(100) = ''
)
AS
BEGIN
SET NOCOUNT ON
DECLARE @SQL VARCHAR(4000)
IF @TABLENAME=''
set @sql='select b.name ColumnName, c.name TypeName, b.length ColumnLength
, Case b.IsNullable when 1 then '''' else ''Not Null'' end Nullable
, Case When g.name is null Then '''' Else ''PK'' End PKwithCidx
, IsNull(d.value,'''') Caption
from '+@DbName+'..sysobjects a
JOIN '+@DbName+'..syscolumns b ON (a.id = b.id and a.type=''U'' and a.status > 0)
JOIN '+@DbName+'..systypes c ON (b.xtype = c.xtype and c.name<>''sysname'')
LEFT JOIN '+@DbName+'..sysproperties d ON (b.id=d.id and b.colid = d.smallid)
LEFT JOIN (select name,e.id,colid from '+@DbName+'..sysindexes e join '+@DbName+'..sysindexkeys f on e.id=f.id
where e.id > 10000000 and e.indid=1 and f.indid=1) g ON (g.id=b.id and g.colid=b.colid)
order by a.name, colorder'
ELSE
set @sql='select b.name ColumnName, c.name TypeName, b.length ColumnLength
, Case b.IsNullable when 1 then '''' else ''Not Null'' end Nullable
, Case When g.name is null Then '''' Else ''PK'' End PKwithCidx
, IsNull(d.value,'''') Caption
from '+@DbName+'..sysobjects a
JOIN '+@DbName+'..syscolumns b ON (a.id = b.id and a.type=''U'' and a.status > 0 and a.name='''+@TableName+''')
JOIN '+@DbName+'..systypes c ON (b.xtype = c.xtype and c.name<>''sysname'')
LEFT JOIN '+@DbName+'..sysproperties d ON (b.id=d.id and b.colid = d.smallid)
LEFT JOIN (select name,e.id,colid from '+@DbName+'..sysindexes e join '+@DbName+'..sysindexkeys f on e.id=f.id
where e.id > 10000000 and e.indid=1 and f.indid=1) g ON (g.id=b.id and g.colid=b.colid)
order by a.name, colorder'
EXEC(@SQL)
SET NOCOUNT OFF
END
--USE INSIDER
--GO
--EXEC USP_TAB_INFO 'INSIDER', 'INSIDER_1'
[출처] [MsSql] table schema 출력하는 procedure |작성자 필립박
'데이터베이스 > SQL Server' 카테고리의 다른 글
페이징 처리 sp (0) | 2008.04.28 |
---|---|
[SQL injection 해킹 보안] 웹관리자를 위한 응급처리법 (0) | 2008.04.28 |
MSSQL 링크드서버를 이용하여 오라클 데이타 이용 (0) | 2008.04.28 |
DB상의 모든 인덱스 정보를 보이는 저장프로시져 (0) | 2008.04.28 |
Bulk Insert 옵션에 따른 성능 비교 (0) | 2008.04.28 |