0

First i written select query with join for retrieving required columns. After that using Top 0 i got the required columns. Which will come like this, Which will come like this

i dont want any records. but i need the result of only the column names like this,

enter image description here

sample code:

select Top 0 *tablename*.* from my *tablename*
(some joins and where conditions)

tablename => contains dynamic columns (After doing joins)

8
  • are you perhaps looking for a PIVOT? it's hard to say from your question. Commented Jun 1, 2014 at 10:20
  • @MitchWheat no, im not expecting pivot Commented Jun 1, 2014 at 10:24
  • 1
    The suspense is killing me..... Commented Jun 1, 2014 at 10:24
  • Far from clear, but what if you got the column names from querying say syscolumns, Wouldn't need the dynamic sql either, just join to systables. Commented Jun 1, 2014 at 10:41
  • @TonyHopkinson its not a sys columns columns and table names are dynamic... Commented Jun 1, 2014 at 10:59

2 Answers 2

1

Inspired on @Tony Hopkinson suggestion

SELECT c.name FROM sys.tables t 
 INNER JOIN sys.columns c
    on c.object_id = t.object_id
    WHERE t.name = @table
Sign up to request clarification or add additional context in comments.

Comments

0

If you can use a cursour (i.e. you can use Transact-SQL), then the sp_describe_cursor_columns (http://technet.microsoft.com/en-us/library/ms182755(v=sql.105).aspx) may help you.

If you want to do this from outside SQL Server, all database access technologies provide some kind of API to access metadata for a result set.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.