To get total number of columns in a table in sql - sql-server

I need a query in sql to get total columns in a table.Can anybody help?

SELECT COUNT(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG = 'database' AND TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'table'

This query gets the columns name
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'YourTableName'
And this one gets the count
SELECT Count(*) FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'YourTableName'

In MS-SQL Server 7+:
SELECT count(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'mytable'

The below query will display all the tables and corresponding column count in a database schema
SELECT Table_Name, count(*) as [No.of Columns]
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'dbo' -- schema name
group by table_name

Select Table_Name, Count(*) As ColumnCount
From Information_Schema.Columns
Group By Table_Name
Order By Table_Name
This code show a list of tables with a number of columns present in that table for a database.
If you want to know the number of column for a particular table in a database
then simply use where clause e.g. where Table_Name='name_your_table'

You can try below query:
select
count(*)
from
all_tab_columns
where
table_name = 'your_table'

It can be done using:-
SELECT COUNT(COLUMN_NAME) 'NO OF COLUMN' FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Address'

Correction to top query above, to allow to run from any database
SELECT COUNT(COLUMN_NAME) FROM [*database*].INFORMATION_SCHEMA.COLUMNS WHERE
TABLE_CATALOG = 'database' AND TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'table'

In my situation, I was comparing table schema column count for 2 identical tables in 2 databases; one is the main database and the other is the archival database. I did this (SQL 2012+):
DECLARE #colCount1 INT;
DECLARE #colCount2 INT;
SELECT #colCount1 = COUNT(COLUMN_NAME) FROM MainDB.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'SomeTable';
SELECT #colCount2 = COUNT(COLUMN_NAME) FROM ArchiveDB.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'SomeTable';
IF (#colCount1 != #colCount2) THROW 5000, 'Number of columns in both tables are not equal. The archive schema may need to be updated.', 16;
The important thing to notice here is qualifying the database name before INFORMATION_SCHEMA (which is a schema, like dbo). This will allow the code to break, in case columns were added to the main database and not to the archival database, in which if the procedure were allowed to run, data loss would almost certainly occur.

To get the list of all columns of the SQL table
select column_name from information_schema.columns where table_name=[dbo].[your_table_name]
To get the list of number of columns of the SQL table
select count(column_name) from information_schema.columns where table_name=[dbo].[your_table_name]

To get the total number of columns in table.
SELECT COUNT(COLUMN_NAME) FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'database_name' AND TABLE_NAME = 'table_name';

Related

Retreive tables having identity column in sql server

I would like to retreive in sql server all table name having identity column. Is there any way to do it via a query without looping over all the columns in sys.columns?
thank you
Yep. You can query the sys.identity_columns catalog:
select t.name TableName
from sys.tables t
where exists (select 1 from sys.identity_columns where [object_id] = t.[object_id])
INFORMATION_SCHEMA.COLUMNS has a lot of columns of information, like length of column, data type, etc.
In this case, we're using it to pull information about identity columns.
select TABLE_NAME + '.' + COLUMN_NAME, TABLE_NAME
from INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA = 'dbo'
and COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') =
1
order by TABLE_NAME
Another helpful one is INFORMATION_SCHEMA.TABLES to provide a list of tables.

Deleting specific rows from all tables

I have a database with a lot of tables.
In each table there is a column named "LicenseID" (bigint). How can I delete all rows, from all tables, that contain the value "2" in the column "LicenseID" ?
Best regards!
You didn't mentioned which database are you working with.
I assume it is SQL Server
First check your result with following query
SELECT * FROM table WHERE LicenseID LIKE '%2%'
or
SELECT * FROM table WHERE Contains(LicenseID, 2) > 0
Then Delete all those rows with above condition.
DELETE FROM table WHERE LicenseID LIKE '%2%'
or
DELETE FROM table WHERE Contains(LicenseID, 2) > 0
I have not tested it and don't know what type of database are you using. Answer can be little different in different databases.
You can use INFORMATION_SCHEMA.COLUMNS to generate the delete statements dynamically.
Something like this should do the trick:
DECLARE #Sql varchar(max) = ''
SELECT #Sql = #Sql + 'DELETE FROM '+ TABLE_NAME +' WHERE LicenseID = 2; '
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'LicenseID'
EXEC(#Sql)
The reason I've used INFORMATION_SCHEMA.COLUMNS and not INFORMATION_SCHEMA.TABLES is to prevent an error in case there is a table that doesn't contain the LicenseID column.

list all tables from a database in syBase

In sql server 2012 i'm using
USE myDatabase;
GO
SELECT *
FROM sys.objects
WHERE type = 'U';
Is it possible to do the same in syBase ?
In order to get a list of all tables in the current database, you can filter the sysobjects table by type = ā€˜Uā€™ e.g.:
select convert(varchar(30),o.name) AS table_name
from sysobjects o
where type = 'U'
order by table_name
Further Reference
Here is an example of getting all table names in MSSQL or SQL Server database:
USE test; //SELECT DATABASE
SELECT table_name FROM information_schema.tables WHERE table_type = 'base table'
or you can use sys.tables to get all table names from selected database as shown in following SQL query
USE test; //SELECT DATABASE
SELECT * FROM sys.tables
That's all on how to find all table names from database in SQL Server.
For SQL Anywhere 16, this query works fine.
select * from sys.SYSTABLE
It gives a list of the table_names along with the information like table id, table page count, etc.
In order to list all the tables and rows count, the following query can be used.
select convert(varchar(30),o.name) AS table_name,
row_count(db_id(), o.id) AS row_count
from sysobjects o
where type = 'U'
order by table_name
In order get a list of tables which has the table name like, use the following query.
Here table_name has to be replaced with your desired name.
select convert(varchar(30),o.name) AS table_name,
row_count(db_id(), o.id) AS row_count
from sysobjects o
where type = 'U' and o.name like '%table_name%'
order by table_name

How can I get the list of columns from a temporary table?

I'd like to get the list of columns in a temporary table, similar to the INFORMATION_SCHEMA.columns view. However, this code:
select *
from tempdb.INFORMATION_SCHEMA.columns
where TABLE_CATALOG = 'tempdb'
and TABLE_NAME like '#myTemporaryTable%'
returns one row per column and per session. Is it safe to do this:
select distinct column_name,data_type
from tempdb.INFORMATION_SCHEMA.columns
where TABLE_CATALOG = 'tempdb'
and TABLE_NAME like '#myTemporaryTable%'
I have a feeling it isn't, even if you tighten up the like clause so it won't match myTemporaryTable and myTemporaryTable2.
If you really need query tempdb, I would use object_id
SELECT *
FROM tempdb.sys.columns
WHERE object_id = OBJECT_ID('tempdb..#myTemporaryTable')

How to get the list of tables and their columns in a database which are having a specific keyword in their name

I have to make a list of tables and their columns that are having keyword 'EMP' in their column name. The database is having around 160 tables. Is there any way of finding this quick ?
Give this a go
SELECT TABLE_NAME,
COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME LIKE '%YoutValue%'
ORDER BY TABLE_NAME,
ORDINAL_POSITION
EDIT
You could use
SELECT TABLE_NAME,
COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME LIKE '%YoutValue%'
OR COLUMN_NAME LIKE '%YoutValue%'
ORDER BY TABLE_NAME,
ORDINAL_POSITION

Resources