Heim  >  Artikel  >  Datenbank  >  sql语句实现表的字段名查询

sql语句实现表的字段名查询

WBOY
WBOYOriginal
2016-06-07 16:22:571762Durchsuche

下面为您介绍的是查询表的字段名的sql语句写法,sql语句可以实现许多的功能,希望可以您在学习sql语句使用方面获得启示。 select name from syscolumns where id = (select id from sysobjects where type = 'u' and name = '相应表名') 或者 select name fr

下面为您介绍的是查询表的字段名的sql语句写法,sql语句可以实现许多的功能,希望可以您在学习sql语句使用方面获得启示。

select name from syscolumns where id = (select id from sysobjects where type = 'u' and name = '相应表名')   或者   select name from syscolumns where id = object_id('相应表名')  用以上sql语句输入相应表名就可以查到表的字段名,对应好数据库 查询是否存在该表语句

而判断表名在数据库中是否存在的方法是

if not object_id('相应表名') is null   print '存在'  这次查询表中的字段名的目标是在写程序的时候需要写一点sql语句,但是表的字段太多了,,如果一个一个去复制的话太慢了,而且有可能会复制漏了某个字段,所以利用自己数据库的知识,写了个sql语句直接生成字段名字符串,例如下面我要写一个select语句,需要生成表所有的字段:

declare @s varchar(1000)   select @s = isnull(@s+',', '') + [name] from syscolumns where id = object_id('相应表名')   select @s  获取字段名已经字段类型,类型长度

SELECT a.colid as ID,a.name as ColumnName,b.name as DataType,a.length   as Length FROM syscolumns a,systypes b WHERE a.id=  object_id('相应的表名') anda.xtype=b.xtype   and b.name 'sysname' order by a.colid 

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn