博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sql查找某个指定表或视图的所有字段
阅读量:3954 次
发布时间:2019-05-24

本文共 1590 字,大约阅读时间需要 5 分钟。

查找某个表或视图的所有字段

– 查找某个表或视图的所有字段最终语句,xhxtest为要查找的表名或视图名,U表示查找表,如果查找视图把U改为V

– 如果想要用其他符号分割,就将,替换为其他符号

select fieldName = STUFF((select ','+B.name from  SYSOBJECTS A ,SYSCOLUMNS B where A.xtype='U' and A.id = B.id and A.NAME='xhxtest'  ORDER BY b.colid for xml path('')),1,1,'')

– 以下为分析过程

– SYSOBJECTS 表存着所有表名

select * from  SYSOBJECTS where xtype='U'

– SYSCOLUMNS 表存着所有列名,SYSOBJECTS和SYSCOLUMNS通过id关联

select * from  SYSCOLUMNS

– 获取表和表对应的字段,通过id相同

select A.name,B.name,b.colid from  SYSOBJECTS A ,SYSCOLUMNS B where A.xtype='U' and A.id = B.id and A.NAME='xhxtest'  ORDER BY b.colid

– 获取表的所有字段,用逗号拼接,用到了for xml path函数

select ','+B.name from  SYSOBJECTS A ,SYSCOLUMNS B where A.xtype='U' and A.id = B.id and A.NAME='xhxtest'  ORDER BY b.colid for xml path('')

– 上面的结果最前面会多个逗号,去掉逗号,用STUFF函数

select STUFF((select ','+B.name from  SYSOBJECTS A ,SYSCOLUMNS B where A.xtype='U' and A.id = B.id and A.NAME='xhxtest'  ORDER BY b.colid for xml path('')),1,1,'')

– 可以这么写查询

select name = 'xhx'select 'xhx'

– 可以对刚刚的结果再加个字段名fieldName

select fieldName = STUFF((select ','+B.name from  SYSOBJECTS A ,SYSCOLUMNS B where A.xtype='U' and A.id = B.id and A.NAME='xhxtest'  ORDER BY b.colid for xml path('')),1,1,'')

– 如果查询视图就把U改为V

select fieldName = STUFF((select ','+B.name from  SYSOBJECTS A ,SYSCOLUMNS B where A.xtype='V' and A.id = B.id and A.NAME='test3'  ORDER BY b.colid for xml path('')),1,1,'')

– stuff 函数用法 删除指定长度的字符,并在指定的起点处插入另一组字符。

– character_expression为原始和要替换的参数,start为从哪儿位置开始删除,length为删除的长度
STUFF ( character_expression , start , length ,character_expression )

select stuff((select '123456'),1,1,'f')

转载地址:http://hduzi.baihongyu.com/

你可能感兴趣的文章
【Redis】Centos7下搭建Redis集群——哨兵模式
查看>>
【Linux】本地ping不同VM虚拟机
查看>>
【SpringCloud】Hystrix
查看>>
快速阅读——《认知篇》
查看>>
【Asp.net】基本概念
查看>>
【Asp.net】Web服务器控件
查看>>
【Asp.net】内置对象
查看>>
C语言数据类型笔记 by STP
查看>>
C语言指针笔记 by STP
查看>>
CoreLocation笔记 by STP
查看>>
Application Transport Security has blocked a cleartext HTTP (http://) 解决方案
查看>>
The identity used to sign the executable is no longer valid.解决方案
查看>>
Xcode增加pch文件
查看>>
CocoaPods安装和使用笔记 by STP
查看>>
Could not find developer disk image-解决方案
查看>>
升级Xcode之后VVDocumenter-Xcode不能用的解决办法
查看>>
iOS开发常见报错及解决方案 by STP
查看>>
SVN(Cornerstone)屏蔽/忽略不需要版本控制的UserInterfaceState.xcuserstate
查看>>
IOS 8 以上版本 设置applicationIconBadgeNumber和消息推送
查看>>
git常用命令
查看>>