• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 445
  • Last Modified:

SQL Query - show table name into coloumn/field

Hi There,

Hows it going Experts!

I have a rather simple question is it possible to insert table name into a query?

For example I have a table called: TableA, I want this name (TableA) to appear in my query as a field is this possible?


Please let me know if you want me to clarify

Cheers

J3di
0
J3D1-KN1G1-1t
Asked:
J3D1-KN1G1-1t
1 Solution
 
IrogSintaCommented:
I'm not sure I understand.
Wouldn't your query just be something like:
SELECT "TABLEA" AS NameOfTable, field1, field2, FROM TABLEA
0
 
HuaMinChenBusiness AnalystCommented:
Try
select 'TabA' as Table1, ...
from TabA
where ...;
0
 
sarabhaiCommented:
this is sample for  spt_monitor table in master u can try for other also...

select OBJECT_NAME ( object_id('spt_monitor')) ,* from spt_monitor

for e.g.
select OBJECT_NAME ( object_id('TableName')) ,* from TableName
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
keyuCommented:
yah you can store it as a field....

but you can eaily get it from below query so no need to store it ..

select name from sys.objects where type='U'

if you want to store t in some table.....

select name into my_table from sys.objects where type='U'

than you can use Pivot functionality to show it as field
0
 
keyuCommented:
DEclare @tablelist as varchar(max)
DEclare @tablsql as nvarchar(max)

select @tablelist =coalesce(@tablelist +',','')+ name from sys.objects where type='U' group by name

set @tablsql='select '+@tablelist+' from (select * from sys.objects where type=''U'') t pivot (avg(object_id) for name in ('+@tablelist+') ) as mypivot'
EXEC Sp_ExecuteSql @tablsql,N'@tablsql as nvarchar(max) output',@tablsql output
0
 
aplusexpertCommented:
Try this query,

select 'TableA' as [Table] ,* from TableA

Open in new window


Thanks.
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now