正在作一个关于SQL SERVER数据库导入Excel文件的程序,要读取数据库中的列的信息,从网上找了很多资料,终于总结出来比较理想的sql语句,执行后返回的列分别是:表名、列名、列类型、列长度、列描述、是否主键,语句如下:
1 Select Sysobjects.Name As Tb_name, Syscolumns.Name As Col_name, Systypes.Name As Col_type, Syscolumns.Length As Col_len, Isnull(Sysproperties.Value,Syscolumns.Name) As Col_memo, 2 Case When Syscolumns.Name In 3 (Select 主键=A.Name 4 From Syscolumns A 5 Inner Join Sysobjects B On A.Id=B.Id And B.Xtype=''U'' And B.Name<>''Dtproperties'' 6 Where Exists(Select 1 From Sysobjects Where Xtype=''Pk'' And Name In ( 7 Select Name From Sysindexes Where Indid In( 8 Select Indid From Sysindexkeys Where Id = A.Id And Colid=A.Colid 9 ))) 10 And B.Name=Sysobjects.Name 11 ) 12 Then 1 Else 0 End As Is_key 13 14 From Sysobjects,Systypes,Syscolumns 15 Left Join Sysproperties On (Syscolumns.Id = Sysproperties.Id And 16 Syscolumns.Colid = Sysproperties.Smallid) 17 18 Where (Sysobjects.Xtype =''U'' Or Sysobjects.Xtype =''V'') 19 And Sysobjects.Id = Syscolumns.Id And Systypes.Xtype = Syscolumns.Xtype 20 And Systypes.Name <> ''Sysname'' And Sysobjects.Name Like ''%'' Order By Sysobjects.Name, Syscolumns.Colid
结果如图:
关注此文的读者还看过:
- 2010-4-1 18:12:45 预防非法表D99_Tmp,kill_kk的建立
- 2008-4-23 15:53:39 SQL Server数据库超级管理员账号防护知识
- 2008-4-11 21:37:35 SQL Server 2005数据库镜像配置脚本示例
- 2008-4-4 9:12:54 讲解如何实现互联网上数据库的安全
- 2008-4-1 11:07:24 SQL 2005数据库转到SQL 2000的步骤讲解
- 2008-3-31 12:39:45 SQL Server自动生成日期加数字的序列号
- 2008-3-31 12:39:07 SQL Server 2000的数据库容量究竟是多大
- 2008-3-18 18:01:49 SQL Server 2005数据库的同义词Bug 讲解
|