导航
  • 主页
  • 基础类
  • 应用实例
  • 新技术前沿

100分求教,多个表关联之后,如何取得返回结果的数据类型

Ki1381 上海大学 打杂  2007-12-21 01:08:27
例如,

TableA (id int,val01 varchar(50))
TableB (id int,val02 int)

假如执行的SQL语句是 select A.id,A.val01,B.val02 from TableA A,TableB B where A.id = B.id

有比较好的什么方法可以知道在返回的结果集中,id是int的,val01是varchar的,val02也是int的呢?

直接用SQL解决或用C#写程序解决都可以,但不想自己解析FROM语句得到哪些表或者视图然后去sp_columns。

谢谢!

...全文
33 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Ki1381 2007-12-21
牛!

不过sysproperties不存在。我使用SQL2005的

不过,思路清楚了!多谢!
回复
dawugui 2007-12-21
create table TableA (id int,val01 varchar(50)) 
create table TableB (id int,val02 int)

select top 1 A.id,A.val01,B.val02 into 表 from TableA A,TableB B where A.id = B.id

SELECT
表名 = case when a.colorder=1 then d.name else '' end,
表说明 = case when a.colorder=1 then isnull(f.value,'') else '' end,
字段序号 = a.colorder,
字段名 = a.name,
标识 = case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end,
主键 = case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in (
SELECT name FROM sysindexes WHERE indid in(
SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then '√' else '' end,
类型 = b.name,
占用字节数 = a.length,
长度 = COLUMNPROPERTY(a.id,a.name,'PRECISION'),
小数位数 = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
允许空 = case when a.isnullable=1 then '√'else '' end,
默认值 = isnull(e.text,''),
字段说明 = isnull(g.[value],'')
FROM
syscolumns a
left join
systypes b
on
a.xusertype=b.xusertype
inner join
sysobjects d
on
a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join
syscomments e
on
a.cdefault=e.id
left join
sysproperties g
on
a.id=g.id and a.colid=g.smallid
left join
sysproperties f
on
d.id=f.id and f.smallid=0
where
d.name='表' --如果只查询指定表,加上此条件
order by
a.id,a.colorder

drop table tableA,tableB,表

/*
表名 表说明 字段序号 字段名 标识 主键 类型 占用字节数 长度 小数位数 允许空 默认值 字段说明
-----------------------------------------------------------------------------------------------
表 1 id int 4 10 0 √
2 val01 varchar 50 50 0 √
3 val02 int 4 10 0 √

(所影响的行数为 3 行)
*/
回复
dawugui 2007-12-21
如果只查类型,不需要全部数据,top 1即可.
select top 1 A.id,A.val01,B.val02 into 表 from TableA A,TableB B where A.id = B.id 
回复
dawugui 2007-12-21
将结果插入一个表(最好不用临时表).然后使用下面这段代码,最后删除这个表.

SELECT 
表名 = case when a.colorder=1 then d.name else '' end,
表说明 = case when a.colorder=1 then isnull(f.value,'') else '' end,
字段序号 = a.colorder,
字段名 = a.name,
标识 = case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end,
主键 = case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in (
SELECT name FROM sysindexes WHERE indid in(
SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then '√' else '' end,
类型 = b.name,
占用字节数 = a.length,
长度 = COLUMNPROPERTY(a.id,a.name,'PRECISION'),
小数位数 = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
允许空 = case when a.isnullable=1 then '√'else '' end,
默认值 = isnull(e.text,''),
字段说明 = isnull(g.[value],'')
FROM
syscolumns a
left join
systypes b
on
a.xusertype=b.xusertype
inner join
sysobjects d
on
a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join
syscomments e
on
a.cdefault=e.id
left join
sysproperties g
on
a.id=g.id and a.colid=g.smallid
left join
sysproperties f
on
d.id=f.id and f.smallid=0
where
d.name='要查询的表' --如果只查询指定表,加上此条件
order by
a.id,a.colorder















SELECT (case when a.colorder=1 then d.name else '' end)表名,
a.colorder 字段序号,
a.name 字段名,
(case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) 标识,
(case when (SELECT count(*) FROM sysobjects WHERE (name in
(SELECT name FROM sysindexes WHERE (id = a.id) AND (indid in
(SELECT indid FROM sysindexkeys WHERE (id = a.id) AND (colid in
(SELECT colid FROM syscolumns WHERE (id = a.id) AND (name = a.name)))))))
AND (xtype = 'PK'))>0 then '√' else '' end) 主键,
b.name 类型,
a.length 占用字节数,
COLUMNPROPERTY(a.id,a.name,'PRECISION') as 长度,
isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as 小数位数,
(case when a.isnullable=1 then '√'else '' end) 允许空,
isnull(e.text,'') 默认值,
isnull(g.[value],'') AS 字段说明

FROM syscolumns a left join systypes b on a.xtype=b.xusertype
inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join syscomments e on a.cdefault=e.id
left join sysproperties g on a.id=g.id AND a.colid = g.smallid
where d.name = '?????????'

order by a.id,a.colorder


/*
表名 表说明 字段序号 字段名 标识 主键 类型 占用字节数 长度 小数位数 允许空 默认值 字段说明
------- ----- ------- -------- ---- ------- ------ ------- --------------- ------ ---------- ----------
authors 1 au_id √ id 11 11 0
2 au_lname varchar 40 40 0
3 au_fname varchar 20 20 0
4 phone char 12 12 0 ('UNKNOWN')
5 address varchar 40 40 0 √
6 city varchar 20 20 0 √
7 state char 2 2 0 √
8 zip char 5 5 0 √
9 contract bit 1 1 0
(所影响的行数为 9 行)
*/
回复
发动态
发帖子
MS-SQL Server
创建于2007-09-28

3.2w+

社区成员

MS-SQL Server相关内容讨论专区
申请成为版主
社区公告
暂无公告