科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网网络频道在SQL Server中显示表结构的脚本片段

在SQL Server中显示表结构的脚本片段

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

本文将为大家介绍如何在SQL Server中显示表结构的脚本片段。

作者:赛迪网 小乔 来源:天新网 2008年3月22日

关键字: 数据库 Mssql SQL SQL Server

  • 评论
  • 分享微博
  • 分享邮件

在SQL Server中显示表结构的脚本片段:

比如现实表TEST1的结构就run sp_showtable "TEST1"
IF OBJECT_ID("dbo.sp_showtable") IS NOT NULL
BEGIN
DROP PROCEDURE dbo.sp_showtable
IF OBJECT_ID("dbo.sp_showtable") IS NOT NULL
PRINT "<<< FAILED DROPPING PROCEDURE dbo.sp_showtable >>>"
ELSE
PRINT "<<< DROPPED PROCEDURE dbo.sp_showtable >>>"
END
go
SET ANSI_NULLS ON
go
SET QUOTED_IDENTIFIER ON
go

create procedure [dbo].[sp_showtable] @tablename varchar(50)
as 
begin
select "**************************************"
print @tablename +" Structure is "
select b.name as ColumnName, case when c.name in 
( "nvarchar","char","nchar","varchar") then c.name+"
("+convert(varchar(4),b.prec)+")"
when c.name in ("decimal","numeric","float")then 
c.name+"("+convert(varchar(4),b.prec)+","
+convert(varchar(4),b.scale)+")"
when c.name in ("text","tinyint","image",
"int","smalldatetime","datetime",
"bigint","timestamp","money") then c.name
else "?????????"
end as Type,
case b.isnullable when 0 then "not 
null" else "null" end as "Null" 
from sysobjects a ,syscolumns b, systypes c
where a.name=@tablename 
and a.id=b.id
and b.usertype=c.usertype
and b.xusertype=c.xusertype
order by b.colorder 

end

go
SET ANSI_NULLS OFF
go
SET QUOTED_IDENTIFIER OFF
go
IF OBJECT_ID("dbo.sp_showtable") IS NOT NULL
PRINT "<<< CREATED PROCEDURE dbo.sp_showtable >>>"
ELSE
PRINT "<<< FAILED CREATING PROCEDURE dbo.sp_showtable >>>"
go

IF OBJECT_ID("dbo.sp_showtable_insert") IS NOT NULL
BEGIN
DROP PROCEDURE dbo.sp_showtable_insert
IF OBJECT_ID("dbo.sp_showtable_insert") IS NOT NULL
PRINT "<<< FAILED DROPPING PROCEDURE dbo.sp_showtable_insert >>>"
ELSE
PRINT "<<< DROPPED PROCEDURE dbo.sp_showtable_insert >>>"
END
go
SET ANSI_NULLS ON
go
SET QUOTED_IDENTIFIER ON
go
    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章