Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I have a scenario where i want to get the number of columns returned by a query.

For e.g.

SQL
select * from
(select 1 as col1, 2 as col2, 3 as col3, 4 as col4) tab1

This has 4 columns so I want output as 4

select * from
(select 1 as col1, 2 as col2) tab1


This has 2 columns so I want output as 2

Is there a way to achieve this ...

Any help would be much appreciated.

Regards,
Gopal
Posted
Comments
Om Prakash Pant 13-Sep-11 9:24am    
you can get number of columns for a table, for a select query i don't think you will get columns from sql server.

1 solution

Instead of doing it in your SQL, do it in your application code by checking the DataTable.Columns.Count property.
 
Share this answer
 
v2
Comments
gopalgupta 13-Sep-11 9:19am    
Thnx but I have to do this in the back end ....
Mehdi Gholam 13-Sep-11 10:06am    
You can do the above on your backend also.
Mehdi Gholam 13-Sep-11 10:06am    
My 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900