Why do you want to do this? How is it going to help with your code - or the output? What would happen if you had to join to the same table - multiple times?
FROM t1
LEFT JOIN table2 t2 ON t2.key = t1.key
LEFT JOIN table2 t3 ON t3.key = t2.otherkey
Now you would be returning all the columns from 't3' as 't3_columnname'?
If this is to help with coding - then make sure you use the table alias when referencing the columns - always.
Thanks this looks very interesting but it will rename the columns in the actual table while I only want to rename the columns in the produced selection having only read-access.
create table Temp1 ( id int , name varchar(20) ,age int , address varchar(100))
insert into Temp1 select 1, 'Har',34,'Oak Street'
insert into Temp1 select 2, 'Car',23,'Ten Twelve'
create table Temp2 ( id int , name varchar(20) )
insert into Temp2 select 1, 'Har'
insert into Temp2 select 2, 'Car'
Query using sys.columns to get the columns for the table you want
SELECT
',b.'+c.[name]+' AS ['+'Temp1 -'+ c.[name] +']'
FROM
sys.columns c
WHERE
object_id = object_id('dbo.Temp1');
SELECT
a.*
,b.id AS [Temp1 -id]
,b.name AS [Temp1 -name]
,b.age AS [Temp1 -age]
,b.address AS [Temp1 -address]
FROM
Temp2 a
JOIN
Temp1 b
ON a.id = b.id