用Sql语句查询指定表格中指定字段的数据类型
一、语句通过systypes,syscolumns,sysobjects,三个系统表格联合检索获取结果。
systypes:数据库支持的数据类型属性。
syscolumns:数据库中各表格结构属性。
sysobjects:数据库中所有对象的属性。
二、检索语句演示
1、示例表格:base_zwb
2、语句如下:
xtype in (select xtype from syscolumns where name = 'zw_mc' and
id in (select ID from sysobjects where name = 'base_zwb'));
语句解析:
1):select ID from sysobjects where name = 'base_zwb'),从表格‘sysobjects ’中,获取表格'base_zwb'的对象ID
2):select xtype from syscolumns where name = 'zw_mc' and id in (select ID from sysobjects where name = 'base_zwb'),以字段名称‘zw_mc’和第1步中获取的‘ID’,从表格‘syscolumns ’中获取数据类型的编号'xtype'
3):以第2部获取的 'xtype'编号,从表格‘systypes ’中获取数据类型。
结果如图:
CREATE PROCEDURE gettableinfo
/@TableName varchar(32)/
AS
/创建临时表/
create table #TableFields(
tableName varchar(32),
fieldname varchar(32),
fieldtype varchar(32),
fieldlength varchar(32),
scale varchar(32),
des varchar(256),
defaultvalue varchar(32),
CanNULL varchar(32)
)
/ 声明游标/
declare table_cur scroll cursor
for select sysobjectsname from sysobjects where sysobjectsxtype = 'U'
for update of sysobjectsname
/声明临时表名/
declare @TName varchar(32)
/ 打开游标/
open table_cur
fetch next from table_cur into @TName
while @@fetch_status=0 begin
SELECT sysobjectsname AS tableName, syscolumnsname AS filedname,
systypesname AS fieldtype, syscolumnslength,
syscolumnsscale
into #FiledInfo_Master
FROM syscolumns INNER JOIN
systypes ON syscolumnsxtype = systypesxtype INNER JOIN
sysobjects ON syscolumnsid = sysobjectsid
WHERE (sysobjectsxtype = 'U') AND (systypesname <> 'sysname') and sysobjectsname=@TName
/得到字段描述/
SELECT objname as filedname ,value into #FiledInfo
FROM ::fn_listextendedproperty('MS_Description', 'user',
'dbo', 'table', @TName,
'column', DEFAULT)
/得到字段缺省值/
SELECT objname as filedname, value as defaultvalue
into #FiledInfo2
FROM ::fn_listextendedproperty('DefaultValue', 'user',
'dbo', 'table', @TName,
'column', DEFAULT)
/得到字段是否可为空/
SELECT objname as filedname, value as CanNULL
into #FiledInfo3
FROM ::fn_listextendedproperty('MS_AllowBlanks', 'user',
'dbo', 'table', @TName,
'column', DEFAULT)
/联结字段描述和属性/
insert into #TableFields
SELECT cast(#FiledInfo_MastertableName as varchar(32)) ,
cast(#FiledInfo_Masterfiledname as varchar(32)),
cast(#FiledInfo_Masterfieldtype as varchar(32)),
cast(#FiledInfo_Masterlength as varchar(32)),
cast(#FiledInfo_Masterscale as varchar(32)),
cast(#FiledInfo[value] as varchar(256)),
cast(#FiledInfo2defaultvalue as varchar(32)),
cast(#FiledInfo3CanNULL as varchar(32))
FROM dbo#FiledInfo_Master LEFT OUTER JOIN
dbo#FiledInfo ON dbo#FiledInfo_Masterfiledname = dbo#FiledInfoFiledName
LEFT OUTER JOIN dbo#FiledInfo2
ON dbo#FiledInfo_Masterfiledname = dbo#FiledInfo2FiledName
LEFT OUTER JOIN dbo#FiledInfo3
ON dbo#FiledInfo_Masterfiledname = dbo#FiledInfo3FiledName
where
#FiledInfo_Mastertablename=@TName
fetch next from table_cur into @TName
--if exists (select from dbosysobjects where id = object_id(N'#FiledInfo_Master') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table #FiledInfo_Master
--if exists (select from dbosysobjects where id = object_id(N'#FiledInfo') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table #FiledInfo
--if exists (select from dbosysobjects where id = object_id(N'#FiledInfo2') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table #FiledInfo2
drop table #FiledInfo3
end
select from #TableFields
deallocate table_cur
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)