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.
Related
How do I retrieve all record from every table (Ex: table1, table2, table3, ... tableN ) where id = 1 from single database (EX: database1) in SQL Server 2008 R2?
Let's suppose I have 1 database and in that I have infinite tables (EX. table1,table2, ....,tableN). Is this possible to get all the record from entire database where id=1 on each table? I think it is possible with SQL information_schema.table or information_schema.column, but I don't know how to use this.
Any help appreciated
Thanks in advance!
You can use the undocumented sp_msforeachtable
sp_msforeachtable
#command1 = 'SELECT * FROM ? WHERE id=1',
#whereand = ' And Object_id In (Select Object_id From sys.columns Where name=''id'')'
#command1 is your query. The question mark is a placeholder the stored procedure uses to insert the table name.
#whereand limits the search to just tables that have the column named id
I don't have much idea of MySQL, Use this in db2
Select C.NAME,C.ROLLNUMBER from TABLE1 C,TABLE2 A where C.ROLLNUMBER=A.ROLLNUMBER and id = 1 order by C.CIRCLENAME
hope this will work too..
If you want to mention the tables manually try this.
SELECT COL1,COL2,COL3....COLN FROM TABLE1 WHERE ID=1
UNION ALL
SELECT COL1,COL2,COL3....COLN FROM TABLE2 WHERE ID=1
UNION ALL
SELECT COL1,COL2,COL3....COLN FROM TABLE2 WHERE ID=1
:
:
:
UNION ALL
SELECT COL1,COL2,COL3....COLN FROM TABLEN WHERE ID=1
Note: The col1,col2,col3...coln Should Be Same Datatype in All The Mentioned Tables
This for dynamic building of all the tables with id =1
SELECT STUFF((SELECT '
UNION ALL
SELECT COL1,COL2,COL3..COLN FROM '+TABLE_NAME + ' WHERE ID=1 '
FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' FOR XML PATH(''),type).value('.', 'NVARCHAR(MAX)'),1,11,'')
Note:Your tables must contain the common column i.e ID .change the column names as per you need but all the mentioned columns in select statement should be contain in all of your tables.
The best possible way would be to generate a dynamic query and executed it to get the required information.
To generate the query you can use the schema information related system tables and feed the data in a fixed format table. i.e. make a fix format tables having a defined column structure. That will help to feed the data.
For example:
CREATE TABLE AllTableData
(
TableId int,
TableName nvarchar(250),
TableData NVARCHAR(max),
SelectedId int
)
Where TableId is the id of table from system table and TableData will contain the concatenated value string of all columns of a table with some separator identifier.
;WITH T AS
(
SELECT
T1.*
FROM
INFORMATION_SCHEMA.TABLES T1
INNER JOIN INFORMATION_SCHEMA.COLUMNS T2 ON T2.TABLE_NAME = T2.TABLE_NAME
WHERE
T2.COLUMN_NAME = 'Id'
AND T1.TABLE_TYPE='BASE TABLE'
),
DynamicQuery AS (
SELECT
1 AS Id,
CONCAT(
'SELECT ', QUOTENAME(T.TABLE_NAME,''''),' AS [TableName],',
CONCAT(' CONCAT(',STUFF((SELECT CONCAT(', [' , C.COLUMN_NAME,']' )
FROM INFORMATION_SCHEMA.COLUMNS C
WHERE C.TABLE_NAME = T.TABLE_NAME
FOR XML PATH('')), 1, 1, ''),') AS [TableData]'
)
,', 1 AS [SelectedId] FROM ', T.TABLE_NAME,' WHERE Id = 1'
) [FinalString]
FROM T
)
SELECT DISTINCT
STUFF((SELECT ' UNION ALL ' + DQ2.FinalString
FROM DynamicQuery DQ2
WHERE DQ2.Id = DQ1.Id
FOR XML PATH('')), 1, 10, '') [FinalString]
FROM DynamicQuery DQ1
GROUP BY DQ1.Id, DQ1.FinalString
I think, this is what you are searching for.
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
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')
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
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';