简体   繁体   English

如何在SQL Server 2008查询的结果标题中显示表名

[英]How can I display tablenames in resultheader of SQL Server 2008 query

I am analysing a large query with lots of joins on different tables that all get aliases in the joins. 我正在分析一个大型查询,该查询在不同的表上有很多联接,所有联接都在联接中得到别名。 Some tables are joined twice with different aliases. 有些表用不同的别名连接了两次。

For example the code from this answer : 例如,此答案中的代码:

SELECT *
FROM Blank b
INNER JOIN Ticket t ON b.BlankCode = t.Blank_BlankCode
INNER JOIN MCO_Blank mb ON b.BlankCode = mb.Blank_BlankCode
INNER JOIN Purchase p1 ON  t.PurchaseID = p1.PurchaseID
INNER JOIN Purchase p2 ON mb.PurchaseID = p2.PurchaseID
INNER JOIN Payment pa1 ON t.PurchaseID = pa1.PurchaseID
INNER JOIN Payment pa2 ON mc.PurchaseID = pa2.PurchaseID
WHERE pa1.Status = "Paid";

I would like my results grid to display the table aliases in the columns. 我希望结果网格在列中显示表别名。 Is that in any way possible other than using an alias for each column in the select ? 除了对select每一列使用别名之外,还有其他方法吗?

So my columnheader should look like this: 因此,我的列标题应如下所示:

b.BlankTypeCode|b.BlankCode|pa1.Amount|pa1.Type|p1.PurchaseDate| pa2.DatePaid

instead of 代替

BlankTypeCode|BlankCode|Amount|Type|PurchaseDate|DatePaid

use This 用这个

SELECT b.BlankTypeCode AS [b.BlankTypeCode]
, b.BlankCode AS [b.BlankCode]
, pa1.Amount AS [pa1.Amount]
, pa1.Type AS [pa1.Type]
, p1.PurchaseDate AS [p1.PurchaseDate]
, pa2.DatePaid AS [pa2.DatePaid]

FROM Blank b
INNER JOIN Ticket t
ON b.BlankCode = t.Blank_BlankCode
INNER JOIN MCO_Blank mb
ON b.BlankCode = mb.Blank_BlankCode
INNER JOIN Purchase p1
ON  t.PurchaseID = p1.PurchaseID
INNER JOIN Purchase p2
ON mb.PurchaseID = p2.PurchaseID
INNER JOIN Payment pa1
ON t.PurchaseID = pa1.PurchaseID
INNER JOIN Payment pa2
ON mc.PurchaseID = pa2.PurchaseID
WHERE pa1.Status = "Paid";

You will have to alias each column in the select query, but there is a slightly quicker way to do it. 您将不得不为select查询中的每一列加上别名,但是有一种更快的方法。 Place your mouse cursor before b.BlankCode , and then, holding down the Alt key , click and drag to select down to a few spaces after pa2.DatePaid . 将鼠标光标b.BlankCode之前,然后在按住Alt键的同时 ,单击并拖动以在pa2.DatePaid之后向下选择几个空格。

Press Ctrl + C . Ctrl + C

Now enter a few spaces after b.BlankCode (to ensure you are past the vertical column of the most stuck out bit, and press Ctrl + V 现在,在b.BlankCode之后输入几个空格(以确保您超出了最突出的位的垂直列,然后按Ctrl + V

This is a quick way to create aliases for your columns, and you can type into multiple columns this way as well. 这是为列创建别名的快速方法,您也可以通过这种方式键入多个列。

It is not recommended using blank spaces or Dot(.) or any other special charecters is column names 不建议使用空格或Dot(。)或任何其他特殊字符作为列名

but if you need so then you can change it like this : 但是,如果需要,您可以像这样更改它:

change the code like this : 像这样更改代码:

    SELECT 
b.BlankTypeCode as "b.BlankTypeCode"
    , b.BlankCode as "b.BlankCode "
    , pa1.Amount as "pa1.Amount"
    , pa1.Type as "pa1.Type"
    , p1.PurchaseDate as "p1.PurchaseDate"
    , pa2.DatePaid as "pa2.DatePaid"

    FROM Blank b
    INNER JOIN Ticket t
    ON b.BlankCode = t.Blank_BlankCode
    INNER JOIN MCO_Blank mb
    ON b.BlankCode = mb.Blank_BlankCode
    INNER JOIN Purchase p1
    ON  t.PurchaseID = p1.PurchaseID
    INNER JOIN Purchase p2
    ON mb.PurchaseID = p2.PurchaseID
    INNER JOIN Payment pa1
    ON t.PurchaseID = pa1.PurchaseID
    INNER JOIN Payment pa2
    ON mc.PurchaseID = pa2.PurchaseID
    WHERE pa1.Status = "Paid";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在SQL Server 2008中查询XML列中的值 - How can I query a value in a XML column in SQL Server 2008 如何在SQL Server 2008中将表作为查询文本查看 - How can I view tables as query text in SQL Server 2008 SQL Server 2008如何执行此查询的子查询 - SQL Server 2008 how can I do a subquery of this query 如何在SQL Server 2008查询中指定此SQL索引提示? - How can I specify this SQL Index Hint on my SQL Server 2008 query? 如何在SQL Server 2008中的单个查询中进行两个分组 - How can i do two groupings in single query in sql server 2008 如何使用STDistance在SQL Server 2008中提高地理更新查询的性能? - How can I improve performance my geography update query in SQL Server 2008 using STDistance? 我需要SQL Server 2008的SQL查询 - i need sql query for sql server 2008 在SQL Server 2008中,如何查询具有对特定表列的外键引用的表? - In SQL Server 2008 how can I query for tables with foreign key references to a specific table column? 如何在MS SQL Server 2008中获得过滤的查询字符串? - How can I get filtered query string in MS SQL Server 2008? 在SQL Server 2008中,如何编写查询以按组明智地获取前10条记录,而我想在单行列(如数据透视表)中显示 - In SQL Server 2008, how to write query to get top 10 records in group wise and I want to display that in single row column like pivot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM