Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding number of columns returned by a query

Tags:

sql

sql-server

How can I get the number of columns returned by an SQL query using SQL Server?

For example, if I have a query like following:

SELECT  *
FROM    A1, A2

It should return the total number of columns in table A1 + total number of columns in table A2. But the query might be more complicated.

like image 987
Faruk Sahin Avatar asked Dec 07 '12 15:12

Faruk Sahin


People also ask

How do I count the number of columns in a SQL query?

In the Microsoft SQL server, the DESC command is not an SQL command, it is used in Oracle. SELECT count(*) as No_of_Column FROM information_schema. columns WHERE table_name ='geeksforgeeks'; Here, COUNT(*) counts the number of columns returned by the INFORMATION_SCHEMA .


1 Answers

Here is one method:

select top 0
into _MYLOCALTEMPTABLE
from (your query here) t

select count(*)
from Information_Schema.Columns c
where table_name = '_MYLOCALTEMPTABLE'

You can do something similar by creating a view.

like image 120
Gordon Linoff Avatar answered Oct 24 '22 00:10

Gordon Linoff