[英]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.