Monday, March 19, 2012

Getting result of select query with tablename.

Hi all,
I have one select query which is fired on 2 tables each table contains
more than 20 columns.
So what i want is when i fire query in query analyzer i should get
column name along with table name.
Like say suppose i have field name in table say table1 so query
analyzer should display me result as table1.name..
Is it possible or do i need to include each field explicitly.
thanks in advance.You can use an alias, e.g.
SELECT
[table1.name] = t1.name,
[table2.price] = t2.price
FROM
table1 t1
INNER JOIN table2 t2
ON
t1.primaryKey = t2.foreignKey;
<trialproduct2004@.yahoo.com> wrote in message
news:1162302880.711103.326880@.f16g2000cwb.googlegroups.com...
> Hi all,
> I have one select query which is fired on 2 tables each table contains
> more than 20 columns.
> So what i want is when i fire query in query analyzer i should get
> column name along with table name.
> Like say suppose i have field name in table say table1 so query
> analyzer should display me result as table1.name..
> Is it possible or do i need to include each field explicitly.
> thanks in advance.
>|||Hi
I'm not sure what are you after? Do the columns repeate in the both tables
? I mean , 2 tables have the column called 'name'?
<trialproduct2004@.yahoo.com> wrote in message
news:1162302880.711103.326880@.f16g2000cwb.googlegroups.com...
> Hi all,
> I have one select query which is fired on 2 tables each table contains
> more than 20 columns.
> So what i want is when i fire query in query analyzer i should get
> column name along with table name.
> Like say suppose i have field name in table say table1 so query
> analyzer should display me result as table1.name..
> Is it possible or do i need to include each field explicitly.
> thanks in advance.
>|||There is no such support at the TSQL level. A client tool (such as Query Analyzer) can theoretically
construct such a "table header", but there is no such support in either QA or SSMS (to the best of
my knowledge).
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
<trialproduct2004@.yahoo.com> wrote in message
news:1162302880.711103.326880@.f16g2000cwb.googlegroups.com...
> Hi all,
> I have one select query which is fired on 2 tables each table contains
> more than 20 columns.
> So what i want is when i fire query in query analyzer i should get
> column name along with table name.
> Like say suppose i have field name in table say table1 so query
> analyzer should display me result as table1.name..
> Is it possible or do i need to include each field explicitly.
> thanks in advance.
>|||trialproduct2004@.yahoo.com wrote:
> Hi all,
> I have one select query which is fired on 2 tables each table contains
> more than 20 columns.
> So what i want is when i fire query in query analyzer i should get
> column name along with table name.
> Like say suppose i have field name in table say table1 so query
> analyzer should display me result as table1.name..
> Is it possible or do i need to include each field explicitly.
> thanks in advance.
>
Use aliases in the field list of your select statement, you can have the
columns returned with whatever name you choose:
SELECT
table1.col1 AS [Table1.Column1],
table2.col1 AS [Table2.Column1]
FROM ...
Tracy McKibben
MCDBA
http://www.realsqlguy.com|||Hi,
thanks for your reply.
yes i have 2 tables with many column name repeated.
thanks in advance.|||> yes i have 2 tables with many column name repeated.
Do they contain the same data? If so, then why return both columns? If
not, then why do they have the same names?
A|||hi,
thanks for your reply.
they have same data for foreign key. But one column name is same that
is dateadded in both column. I want to distinguish dateadded of 2
tables.
Thanks|||Hi
You need to give alias as tablename.column name then necessarry joind
condition as usual for the select statement.
i.e.
select Table1.[id] 'Table1.[id]', Table1.col1 'Table1.col1',
Table1.col2 'Table1.col2', Table1.col3 'Table1.col2' from Table1
Let me know if you need more details on same
Thanks
Kailash
trialproduct2004@.yahoo.com wrote:
> Hi all,
> I have one select query which is fired on 2 tables each table contains
> more than 20 columns.
> So what i want is when i fire query in query analyzer i should get
> column name along with table name.
> Like say suppose i have field name in table say table1 so query
> analyzer should display me result as table1.name..
> Is it possible or do i need to include each field explicitly.
> thanks in advance.

No comments:

Post a Comment