Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count of Columns in temp table in SQL Server

Tags:

sql

sql-server

Is there a way to count number of columns in a temp (#temptable) table in sql server ?

like image 484
shrekDeep Avatar asked Sep 12 '13 09:09

shrekDeep


People also ask

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

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 .

How do you find the number of columns in SQL?

Learn MySQL from scratch for Data Science and Analytics To find the number of columns in a MySQL table, use the count(*) function with information_schema. columns and the WHERE clause.


2 Answers

SELECT COUNT(*)
FROM tempdb.sys.columns
WHERE object_id = object_id('tempdb..#mytemptable')
like image 198
Gayathri L Avatar answered Sep 27 '22 22:09

Gayathri L


Query to get column counts for specified table

SELECT Count(*) as cnt into #TempTable FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'TableName'

Query to get column names for specified table

SELECT COLUMN_NAME into #TempTable FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'TableName'

Query to get column count for #TempTable

SELECT COUNT(*) as Cnt FROM tempdb.sys.columns WHERE object_id = object_id('tempdb..#TempTable')

DROP table #TempTable
like image 31
immayankmodi Avatar answered Sep 27 '22 23:09

immayankmodi