SQL 通过syscolumns.xtype动态查找指定数据类型字段所包含的数据
表中太多列,只想查找某些比如,數(shù)據(jù)類型為varchar的字段的數(shù)據(jù)。
思路:1、先獲取列名:
select * from syscolumns where id=(select max(id) from sysobjects where xtype='u' and name='test_A')
2、查找指定數(shù)據(jù)類型,xtype就是數(shù)據(jù)類型,參考如下
syscolumns表內(nèi)的xtype
查了一下,這些東西都是存于每一個(gè)數(shù)據(jù)庫(kù)的syscolumns表里面得,name就是列名,xtype就是數(shù)據(jù)類型,但是這個(gè)xtype是數(shù)字的,下面是數(shù)字和數(shù)據(jù)類型對(duì)應(yīng)的關(guān)系;
xtype=34 'image'?
?xtype= 35 'text'?
?xtype=36 'uniqueidentifier'?
?xtype=48 'tinyint'?
?xtype=52 'smallint'?
?xtype=56 'int'?
?xtype=58 'smalldatetime'?
?xtype=59 'real'?
?xtype=60 'money'?
?xtype=61 'datetime'?
?xtype=62 'float'?
?xtype=98 'sql_variant'?
?xtype=99 'ntext'?
?xtype=104 'bit'?
?xtype=106 'decimal'?
?xtype=108 'numeric'?
?xtype=122 'smallmoney'?
?xtype=127 'bigint'?
?xtype=165 'varbinary'?
?xtype=167 'varchar'
?xtype=173 'binary'?
?xtype=175 'char'?
?xtype=189 'timestamp'?
?xtype=231 'nvarchar'
?xtype=239 'nchar'?
?xtype=241 'xml'?
?xtype=231 'sysname'
3、構(gòu)造最終的動(dòng)態(tài)SQL語(yǔ)句:
DECLARE @sql VARCHAR(max) DECLARE @col VARCHAR(1000)SELECT @col = STUFF((SELECT ','+name FROM syscolumns where id=(select max(id) from sysobjects where xtype='u' and name='test_A') AND sys.syscolumns.xtype= 167 for xml path('')),1,1,'')SET @sql='select '+@col SET @sql=@sql+' from test_A' EXEC(@sql)?
轉(zhuǎn)載于:https://www.cnblogs.com/EminemJK/p/5825063.html
總結(jié)
以上是生活随笔為你收集整理的SQL 通过syscolumns.xtype动态查找指定数据类型字段所包含的数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【普通の随笔】6.30
- 下一篇: 手机客户端接口开发文档模板整理