Reputation: 702
I have a select in SQL Server like this:
Select name, email, username, tel
from ViewName
I want to get a list of column names from that select.
Note :
1. I don't have table name
2. Select query is dynamic
Select query is in view
Upvotes: 2
Views: 1841
Reputation: 4630
Try This:
I hope this will work dynamically, it'll select all the
columns
depending on theTable_View_Name
you passed in theparameter
.
declare @tab varchar(max)='Your_Dynamic_Table_View_Name'
declare @txt varchar(max)='SELECT COLUMN_NAME FROM information_schema.columns WHERE table_name ='+@tab
declare @columns varchar(max)=(SELECT
STUFF((
SELECT ', ' + COLUMN_NAME
FROM information_schema.columns where table_name=@tab
FOR XML PATH(''),TYPE).value('(./text())[1]','VARCHAR(MAX)')
,1,2,'') AS NameValues)
exec('select '+@columns+' from '+@tab)
Upvotes: 1
Reputation: 9606
Try these..
sp_help 'viewName'
or
select c.* from syscolumns c inner join sysobjects o on o.id=c.id where o.name='viewName'
Upvotes: 0