简体   繁体   中英

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 ?

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. 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 .

Press 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

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

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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM