How to display user databases only in sqlserver? - sql-server

how to display user databases in sqlserver
i queried as select sys.databases it displays all including msdb temp and all.
i need only user cretaed databases.
is there any query like : select * from sys.databases where type='u' ??
Help me.
Thanks in Advance

Is there a reason it needs to be more advanced than just this?
SELECT * FROM sysdatabases WHERE name NOT IN('master', 'tempdb', 'model', 'msdb')

You should specify them with their owner name. In order to do that, you should join sys.databases with sys.server_principals.
Recent versions can have ReportServer databases as well with contains $ sign in name.
select
d.name
,d.database_id
from
sys.databases d
join
sys.server_principals p
on p.sid = d.owner_sid
where
p.name <> 'sa' and d.name not like '%$%';

Or per this thread:
select * from sys.sysdatabases where dbid>4 and [name] not like '$'

Related

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

Count the total number of databases

I am working in SQL server having a large number of databases. I want to count the number of databases. Is there any query to count?
SELECT * FROM sys.databases
OR
SELECT COUNT(*) FROM sys.databases
You can try this
SELECT Count(*) as DatabaseCount FROM master..sysdatabases
or
SELECT count(*) as DatabaseCount FROM master.sys.databases
try select COUNT(*) from sysdatabases or select COUNT(*) from sys.databases
edited from source: http://www.sqlservercentral.com/Forums/Topic401516-463-1.aspx#bm816566
SELECT COUNT(*) FROM sys.databases where database_id not in ( 1,2,3,4 ).. excluding system database
SELECT count(1) FROM sys.databases
this is what you can have for counting the number of database check this link for more info
If you only want to know the count, check this-
select COUNT(*) from sys.databases
check select * from sysdatabases for 2000 and 2005 server

SQL request to list all databases in an instance of Sql-Server?

Is there a way to list all of the existing databases in an instance of Sql Server via a SQL request ?
More generally can I use SQL to fully read the schema of the databases (tables, columns, ..) ?
Thank you
Jerome Wagner
Yes. See the Information Schema Views and sys.databases.
Here's a script for table definitions that may be useful. http://www.builderau.com.au/program/sqlserver/soa/Script-Table-definitions-using-TSQL/0,339028455,339293405,00.htm
You can get a lot of infos by the following queries:
SELECT * FROM sys.databases
use Northwind
select * from sys.objects where type_desc = 'USER_TABLE'
SELECT t1.name [table], t2.*
FROM sys.objects t1
inner join sys.columns t2 on t1.object_id = t2.object_id
where type_desc = 'USER_TABLE'
sp_help 'Customers' -- Customers = tablename
Yes, Sp_msforeachdb, there is also a sp_msforeachtable. You can use both to iteratively retrieve all tables in all DB's and get what you want.
https://web.archive.org/web/1/http://blogs.techrepublic%2ecom%2ecom/datacenter/?p=395.
Try using the stored procedure sp_databases to get the list of all db

SQL Query to search schema of all tables

I am working on a SQL Server 2008 Db that has many tables in it (around 200). Many of these tables contain a field by the name "CreatedDate". I am trying to identify all the table schema with this particular field.
Is there a SQL query to do this?
I would query the information_schema - this has views that are much more readable than the underlying tables.
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%create%'
You can also try doing this using one of many third party tools that are available for this.
Queries are great for simple searches but if you need to do more manipulation with data, search for references and such this is where you can do a much better job with these.
Also, these come in very handy when some objects are encrypted and you need to search for
I’m using ApexSQL Search which is free but there are also many more (also free) on the market such as Red Gate or SSMS Tool Pack.
select object_name(c.object_id) as table_name
, schema_name(t.schema_id) as schema_name
from sys.columns c
join sys.tables t on c.object_id = t.object_id
where c.name=N'CreatedDate';
It gets a little more complicated if you want alsoother table properties, but you'll refer to the object catalog views like sys.tables, sys.columns etc.
My favorite...
SELECT objParent.name AS parent, obj.name, col.*
FROM sysobjects obj
LEFT JOIN syscolumns col
ON obj.id = col.id
LEFT JOIN sysobjects objParent
ON objParent.id = obj.parent_obj
WHERE col.name LIKE '%Comment%'
OR obj.name LIKE '%Comment%'
Above I'm searching for "Comment".
Drop the percent signs if you want a direct match.
This searches tables, fields and things like primary key names, constraints, views, etc.
And when you want to search in StoredProcs after monkeying with the tables (and need to make the procs match), use the following...
SELECT name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%Comment%'
Hope that helps, I find these two queries to be extremely useful.
For me I only have read access to run querys so I need to use this function often here is what I use:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
where TABLES.TABLE_NAME like '%your table name here%'
You can replace .TABLES with .COLUMNS then it would look like this:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE columns.COLUMN_NAME like '%your column name here%'
Use this query :
SELECT
t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name , *
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
Where
( c.name LIKE '%' + '<ColumnName>' + '%' )
AND
( t.type = 'U' ) -- Use This To Prevent Selecting System Tables
Same thing but in ANSI way
SELECT
*
FROM
INFORMATION_SCHEMA.TABLES
WHERE
TABLE_NAME IN (
SELECT
TABLE_NAME
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
COLUMN_NAME = 'CreateDate'
)
You do not need to type SQL Query for this in SQL Server 2008.
In SSMS Object Explorer choose Databases or Tables of the required database (if you need to search in one database), open menu View--> Object Explorer Details (alternatively press F7), type %CreatedDate% in Search textbox, ENTER, enjoy

How do I get list of all tables in a database using TSQL?

What is the best way to get the names of all of the tables in a specific database on SQL Server?
SQL Server 2000, 2005, 2008, 2012, 2014, 2016, 2017 or 2019:
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'
To show only tables from a particular database
SELECT TABLE_NAME
FROM [<DATABASE_NAME>].INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
Or,
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
AND TABLE_CATALOG='dbName' --(for MySql, use: TABLE_SCHEMA='dbName' )
PS: For SQL Server 2000:
SELECT * FROM sysobjects WHERE xtype='U'
SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U'
Here is a list of other object types you can search for as well:
AF: Aggregate function (CLR)
C: CHECK constraint
D: Default or DEFAULT constraint
F: FOREIGN KEY constraint
L: Log
FN: Scalar function
FS: Assembly (CLR) scalar-function
FT: Assembly (CLR) table-valued function
IF: In-lined table-function
IT: Internal table
P: Stored procedure
PC: Assembly (CLR) stored-procedure
PK: PRIMARY KEY constraint (type is K)
RF: Replication filter stored procedure
S: System table
SN: Synonym
SQ: Service queue
TA: Assembly (CLR) DML trigger
TF: Table function
TR: SQL DML Trigger
TT: Table type
U: User table
UQ: UNIQUE constraint (type is K)
V: View
X: Extended stored procedure
SELECT * FROM INFORMATION_SCHEMA.TABLES
OR
SELECT * FROM Sys.Tables
USE YourDBName
GO
SELECT *
FROM sys.Tables
GO
OR
USE YourDBName
GO
SELECT * FROM INFORMATION_SCHEMA.TABLES
GO
SELECT * FROM information_schema.tables
where TABLE_TYPE = 'BASE TABLE'
SQL Server 2012
select * from sysobjects where xtype='U'
exec sp_msforeachtable 'print ''?'''
SELECT name
FROM sysobjects
WHERE xtype='U'
ORDER BY name;
(SQL Server 2000 standard; still supported in SQL Server 2005.)
The downside of INFORMATION_SCHEMA.TABLES is that it also includes system tables such as dtproperties and the MSpeer_... tables, with no way to tell them apart from your own tables.
I would recommend using sys.objects (the new version of the deprecated sysobjects view), which does support excluding the system tables:
select *
from sys.objects
where type = 'U' -- User tables
and is_ms_shipped = 0 -- Exclude system tables
SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U'
Well you can use sys.objects to get all database objects.
GO
select * from sys.objects where type_desc='USER_TABLE' order by name
GO
OR
-- For all tables
select * from INFORMATION_SCHEMA.TABLES
GO
--- For user defined tables
select * from INFORMATION_SCHEMA.TABLES where TABLE_TYPE='BASE TABLE'
GO
--- For Views
select * from INFORMATION_SCHEMA.TABLES where TABLE_TYPE='VIEW'
GO
In SSMS, to get all fully qualified table names in a specific database (E.g., "MyDatabase"):
SELECT [TABLE_CATALOG] + '.' + [TABLE_SCHEMA] + '.' + [TABLE_NAME]
FROM MyDatabase.INFORMATION_SCHEMA.Tables
WHERE [TABLE_TYPE] = 'BASE TABLE' and [TABLE_NAME] <> 'sysdiagrams'
ORDER BY [TABLE_SCHEMA], [TABLE_NAME]
Results:
MyDatabase.dbo.MyTable1
MyDatabase.dbo.MyTable2
MyDatabase.MySchema.MyTable3
MyDatabase.MySchema.MyTable4
etc.
Any of the T-SQL code below will work in SQL Server 2019:
-- here, you need to prefix the database name in INFORMATION_SCHEMA.TABLES
SELECT TABLE_NAME FROM [MSSQL-TEST].INFORMATION_SCHEMA.TABLES;
-- The next 2 ways will require you to point
-- to the specific database you want to list the tables
USE [MSSQL-TEST];
-- (1) Using sys.tables
SELECT * FROM sys.tables;
-- (2) Using sysobjects
SELECT * FROM sysobjects
WHERE type='U';
Here’s a working example using [Skyvia] using sys.tables.
[Skyvia] should be the link to https://skyvia.com/connectors/sql-server
[1]: https://i.stack.imgur.com/o3qo9.png
Your SQL GUI tool should also have a way to list down all the tables in a database like the one above.
So, whatever suits your need and taste, there’s a code or GUI tool for that.
--for oracle
select tablespace_name, table_name from all_tables;
This link can provide much more information on this
topic
Please use this. You will get table names along with schema names:
SELECT SYSSCHEMA.NAME, SYSTABLE.NAME
FROM SYS.tables SYSTABLE
INNER JOIN SYS.SCHEMAS SYSSCHEMA
ON SYSTABLE.SCHEMA_ID = SYSSCHEMA.SCHEMA_ID
UPDATE 2022:
You can list/show the tables that you created with this simple query in Microsoft SQL SERVER.
select * from SYS.TABLES;
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE='BASE TABLE'
ORDER BY TABLE_NAME
Thanks to Ray Vega, whose response gives all user tables in a database...
exec sp_msforeachtable 'print ''?'''
sp_helptext shows the underlying query, which summarises to...
select * from dbo.sysobjects o
join sys.all_objects syso on o.id = syso.object_id
where OBJECTPROPERTY(o.id, 'IsUserTable') = 1
and o.category & 2 = 0
Using SELECT * FROM INFORMATION_SCHEMA.COLUMNS also shows you all tables and related columns.
To remove tables added by replication and any other table Microsoft adds run this:
SELECT s.NAME SchemaName, t.NAME TableName
FROM [dbname].SYS.tables t
INNER JOIN [dbname].SYS.SCHEMAS s
ON t.SCHEMA_ID = s.SCHEMA_ID
WHERE t.is_ms_shipped=0 and type_desc = 'USER_TABLE'
ORDER BY s.NAME, t.NAME

Resources