1

我有一个返回 30 列的视图。视图是通过三个表的内部连接创建的。我希望找出使用查询从哪个表中取出哪一列,而不是逐行浏览视图。有没有办法在 sql server 2008 中做到这一点?提前致谢

4

2 回答 2

2

我希望找出使用查询从哪个表中取出哪一列,而不是逐行浏览视图

不,没有。

如果数据来自的表很重要,您可以在您的视图中使用列的别名来标识它们来自哪些表。

然而,这将否定首先拥有视图的原因。

于 2012-11-06T20:29:56.727 回答
0

您可以在执行计划 XML 中查询列名和表名。

使用此设置:

create table Table1
(
  Table1ID int identity primary key,
  Name varchar(10)
)

create table Table2
(
  Table2ID int identity primary key,
  Table1ID int not null references Table1(Table1ID),
  Name varchar(10)
)

go

create view Table_1_2 as
select T1.Table1ID,
       T1.Name as Table1Name,
       T2.Name as Table2Name
from Table1 as T1
  inner join Table2 as T2
    on T1.Table1ID = T2.Table1ID

在启用“包括实际执行计划”的情况下,从 SQL Server Management Studio 对视图执行查询。

select *
from Table_1_2
where 0 = 1

选择执行计划选项卡,右键单击并选择显示执行计划 XML。将 XML 复制到一个新的查询窗口,将其分配给一个 XML 变量,然后在 XML 中查询您需要的信息。

declare @XML xml = N'<?xml version="1.0" encoding="utf-16"?>
<ShowPlanXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Version="1.2" Build="11.0.2100.60" xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan">
  <BatchSequence>
    <Batch>
      <Statements>
        <StmtSimple StatementCompId="1" StatementEstRows="0" StatementId="1" StatementOptmLevel="TRIVIAL" StatementSubTreeCost="1.57E-07" StatementText="select *&#xD;&#xA;from Table_1_2&#xD;&#xA;where 0 = 1" StatementType="SELECT" QueryHash="0xEBAD405D39FDCB6A" QueryPlanHash="0xAE962A4D575B83FE" RetrievedFromCache="true">
          <StatementSetOptions ANSI_NULLS="true" ANSI_PADDING="true" ANSI_WARNINGS="true" ARITHABORT="true" CONCAT_NULL_YIELDS_NULL="true" NUMERIC_ROUNDABORT="false" QUOTED_IDENTIFIER="true" />
          <QueryPlan DegreeOfParallelism="1" CachedPlanSize="16" CompileTime="0" CompileCPU="0" CompileMemory="112">
            <MemoryGrantInfo SerialRequiredMemory="0" SerialDesiredMemory="0" />
            <OptimizerHardwareDependentProperties EstimatedAvailableMemoryGrant="25600" EstimatedPagesCached="25600" EstimatedAvailableDegreeOfParallelism="8" />
            <RelOp AvgRowSize="27" EstimateCPU="1.57E-07" EstimateIO="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row" EstimateRows="0" LogicalOp="Constant Scan" NodeId="0" Parallel="false" PhysicalOp="Constant Scan" EstimatedTotalSubtreeCost="1.57E-07">
              <OutputList>
                <ColumnReference Database="[xx]" Schema="[dbo]" Table="[Table1]" Alias="[T1]" Column="Table1ID" />
                <ColumnReference Database="[xx]" Schema="[dbo]" Table="[Table1]" Alias="[T1]" Column="Name" />
                <ColumnReference Database="[xx]" Schema="[dbo]" Table="[Table2]" Alias="[T2]" Column="Name" />
              </OutputList>
              <RunTimeInformation>
                <RunTimeCountersPerThread Thread="0" ActualRows="0" ActualEndOfScans="1" ActualExecutions="1" />
              </RunTimeInformation>
              <ConstantScan />
            </RelOp>
          </QueryPlan>
        </StmtSimple>
      </Statements>
    </Batch>
  </BatchSequence>
</ShowPlanXML>';

with xmlnamespaces(default 'http://schemas.microsoft.com/sqlserver/2004/07/showplan')
select T.N.value('@Table', 'sysname') as TableName,
       T.N.value('@Column', 'sysname') as ColumnName
from @XML.nodes('//ColumnReference') as T(N)

结果:

TableName            ColumnName           
-------------------- -------------------- 
[Table1]             Table1ID             
[Table1]             Name                 
[Table2]             Name                 
于 2012-11-07T07:03:33.563 回答