Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server SQL Server(2005+)查询以返回视图中每个列(字段)的基表和基列(字段)_Sql Server_Sql Server 2005_Database Design_Schema - Fatal编程技术网

Sql server SQL Server(2005+)查询以返回视图中每个列(字段)的基表和基列(字段)

Sql server SQL Server(2005+)查询以返回视图中每个列(字段)的基表和基列(字段),sql-server,sql-server-2005,database-design,schema,Sql Server,Sql Server 2005,Database Design,Schema,我需要一个查询,该查询将为视图中的每一列返回一行,并为视图本身返回一行 结果中应该有一个column basetable,为当前行中的列提供基表;结果中应该有一个column basefield,为重命名列提供基础查询中的列名称。如果任何计算也可以包含在basefield列中,那将是一个额外的收获 create table table1 (a int, b int) create table table2 (a int, c int) go create view view1 as select

我需要一个查询,该查询将为视图中的每一列返回一行,并为视图本身返回一行

结果中应该有一个column basetable,为当前行中的列提供基表;结果中应该有一个column basefield,为重命名列提供基础查询中的列名称。如果任何计算也可以包含在basefield列中,那将是一个额外的收获

create table table1 (a int, b int) create table table2 (a int, c int) go create view view1 as select table1.a, table1.b, table2.c from table1 left join table2 on table1.a = table2.a go select * from ( select 'View' objecttype,O.name viewname,'' fieldname,0 column_id,'' typename,'' max_length,'' [precision], '' scale, '' is_identity, 'what goes here' basetable, '' basefield from sys.objects O where O.type='V' and O.[schema_id] = 1 union all select 'Field' objecttype,object_name(C.[object_id]) viewname,C.name fieldname,C.column_id,T.name typename,C.max_length,C.precision,C.scale,C.is_identity, 'what goes here' basetable, 'what goes here' basefield from sys.columns C left join sys.types T on C.user_type_id=T.system_type_id where C.[object_id] in (select O.[object_id] from sys.objects O where O.type='V') ) I where viewname in ('view1') order by viewname, column_id drop view view1 drop table table1 drop table table2 我认为这是办不到的。我错了吗

在下面的示例中,此处的内容应在basetable列中适当地替换为table1或table2,在basefield列中适当地替换为a、b或c

create table table1 (a int, b int) create table table2 (a int, c int) go create view view1 as select table1.a, table1.b, table2.c from table1 left join table2 on table1.a = table2.a go select * from ( select 'View' objecttype,O.name viewname,'' fieldname,0 column_id,'' typename,'' max_length,'' [precision], '' scale, '' is_identity, 'what goes here' basetable, '' basefield from sys.objects O where O.type='V' and O.[schema_id] = 1 union all select 'Field' objecttype,object_name(C.[object_id]) viewname,C.name fieldname,C.column_id,T.name typename,C.max_length,C.precision,C.scale,C.is_identity, 'what goes here' basetable, 'what goes here' basefield from sys.columns C left join sys.types T on C.user_type_id=T.system_type_id where C.[object_id] in (select O.[object_id] from sys.objects O where O.type='V') ) I where viewname in ('view1') order by viewname, column_id drop view view1 drop table table1 drop table table2
你知道它可能不是1对1的映射吗?视图中的一列可能是多个或甚至没有的结果!来自不同表的列


也就是说,应该可以通过解析视图的源代码来实现。但是sql代码在本质上是过程性的/强制性的,一点也不琐碎。

信息模式中很少有表可以用来推断信息。提供大量数据的基本查询包括:

select * 
from INFORMATION_SCHEMA.VIEW_COLUMN_USAGE v
    inner join INFORMATION_SCHEMA.COLUMNS v1
        on  v.VIEW_NAME=v1.TABLE_NAME and v.COLUMN_NAME=v1.COLUMN_NAME
where v.VIEW_NAME='My_View'
信息架构中的表包括:

select * from INFORMATION_SCHEMA.VIEWS
select * from INFORMATION_SCHEMA.VIEW_TABLE_USAGE
select * from INFORMATION_SCHEMA.VIEW_COLUMN_USAGE
所以试着用这些


但是,只有当您直接使用基本选项卡中的列,而不使用任何公式ar派生时,它才有效。

是的,这是一个额外的好处:如果它可以返回给我计算计算字段的结果。只是想知道是否有一种基于SQL Server解析的方法已经存在——它可以运行查询这一事实意味着这是可能的。但是,考虑到视图是多个选项卡之间连接的直接查询,有什么方法可以获得它呢?