Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want enum user database in SQL server, e.g.

select name from sys.databases

It returns below content:

master
tempdb
model
msdb
ReportServer$SQLEXPRESS
ReportServer$SQLEXPRESSTempDB
xsxk
MyDatabase2

The first 6 databases are system database, they are not my wanted.
I just need the last 2 databases. The last 2 databases are user created.

Please provide source code in C# or SQL, to filter system database.
The user database name has no rules.

What I have tried:

Using SMO:

Microsoft.SqlServer.Management.Smo.Server so = new Microsoft.SqlServer.Management.Smo.Server();
Microsoft.SqlServer.Management.Smo.DatabaseCollection dc = so.Databases;

It also return all databases.
Posted
Updated 22-May-17 23:44pm
Comments
PIEBALDconsult 18-May-17 1:08am    
See also:
https://www.codeproject.com/Questions/694915/How-to-determine-which-databases-are-and-are-not-s

Unfortunately if you base your query on a date then tempdb will (usually) still appear in your list (depending on how the instance is being managed).

If you use the standard approach
select [name] from sys.databases where database_id > 4 
then (in your case) you will still get entries for ReportServer$SQLEXPRESS and ReportServer$SQLEXPRESSTempDB. You can't just change the WHERE clause to
SQL
where database_id > 6
because if you don't have Reporting services installed then you'll wrongly omit some genuine user created databases.

It may look as if you could use
SQL
select * from sys.databases where owner_sid = 0x01
i.e. list all databases created by the admin account. BUT what if your DBA used that account to create databases on behalf of a user? Again, you could wrongly omit some genuine databases of interest.

Probably the safest route is
SQL
select * from sys.databases where database_id > 5 and [name] NOT LIKE 'ReportServer%'
 
Share this answer
 
In master db hit this query

(SQL 2012)
select * from sysdatabases order by crdate


Check date of first database created.
Make configuration for date with value equal to first user created DB
Now use below query to fetch your database


select * from sysdatabases where crdate > @date order by crdate


@date value should be fetched from configuration

Dont hard code @date in SP because you DEV,QC,UAT,Prod environment db created date will be different
 
Share this answer
 
v2
Comments
pradiprenushe 18-May-17 6:30am    
Any reason for downvote
CHill60 19-May-17 4:49am    
I should imagine it's because it doesn't quite work - check out database msdb - It's *that* date that you need. On all my SQL instances model and master are dated 2003-04-09. msdb is dated when I installed the instance on the computer. I suspect the ReportServer entries will have the same issue.
By the way, if you use sys.databases rather than sysdatabases then you can use the technique on earlier versions of SQL Server.
pradiprenushe 23-May-17 1:41am    
date is same for system database and it will same when as installation date. But you will get different date for custom DB and that date you can use for logic. I have tested this on my side
CHill60 23-May-17 4:50am    
You can test it as much as you want - the classic response from "developers" has always been "it works on my machine". I have given you an example of msdb having a different date to the installation date of the SQL Server instance. ReportServer* could have a completely different date again and the date on tempdb depends on how SQL Server is managed - mine for example is dated last month yet I installed SQL Server on this machine 3 years ago.
It wasn't me that downvoted you by the way, I was just responding to your question "Any reason for downvote"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900