I know about getting the database names by doing the following:
select *
from master..sysdatabases
order by Name
What I want to be able to do is return all the files related to each database on the server. To get the list of files per database I have to do something similar to the following:
use [database]
select *
from sysfiles
Unfortunately I need the use prefix as sysfiles only pulls from the database I'm using.
Ideally I want to be able to wrap this into a view that can be called and hooked into zabbix via an odbc database monitoring item.
Let's say I have 3 database:
DB1
DB2
DB3
I want a view that will show
Database Name FileName
--------------------------------
DB1 c:\mnt\db1.mdf
DB1 c:\mnt\db1.ldf
DB2 c:\mnt\db2.mdf
DB2 c:\mnt\db2.ldf
DB3 d:\mnt\db3.mdf
DB3 d:\mnt\db3.ldf
You can query it using the sys.databases and sys.master_files views:
SELECT
db.name,
FileName = mf.name,
PhysicalFileName = mf.physical_name,
Type = mf.type_desc
FROM sys.databases db
INNER JOIN sys.master_files mf
ON mf.database_id = db.database_id
WHERE db.name NOT IN('msdb', 'master', 'tempdb', 'model')
ORDER BY db.name
Related
How do I select current database name and all the schema for the database name in SYBASE ASE using a query
tried Select db_name
gives invalid column name
Current database:
select db_name()
By 'schema' I'm assuming you mean a user (in the database) who owns at least one object:
select distinct(user_name(uid)) from sysobjects
NOTE: I'm not sitting in front of an ASE instance at the moment so can't recall, or verify, if the 2nd query could generate a NULL ... should be easy enough to test and add an optional where clause to filter out as needed
Sybase server of my product is going to be upgraded from version 12 to 15.
I am looking for a script to take a snapshot of the server before upgrade and then after upgrade, with an aim to quickly compare and assure myself that all the tables / indices / views / stored procs / users and permissions are there.
I am a dev (and not a DBA) and I will have only a command line access to production server (which is a Solaris box).
Thanks for help.
A really low tech answer, but this should work. Get the following information from both the Sybase servers and check that they match. A quick CSV compare, maybe?
-- Compare name of all tables, views and triggers.
SELECT ob.name, ob.type FROM sysobjects ob WHERE ob.type in ('U', 'V', 'TR') ORDER BY ob.name
-- Compare name of stored procs
SELECT ob.name FROM sysobjects ob WHERE ob.type ="P" ORDER BY ob.name
-- Compare list of all columns of all table and views.
select ob.name, c.name, c.type, c.length, c.prec, c.scale
from sysobjects ob, syscolumns c
where ob.type in ('U','V') and ob.id=c.id
order by ob.name
Create a view on sysobjects like this: CREATE VIEW myview as SELECT name, user_name(uid) as objectowner, type from sysobjects order by 1,2,3
Then do a BCP-out from the view:
bcp yourdb..myview out myfile.txt -Usa -Pyourpaswd -SYOURSERVER -c
Do this before and after the upgrade and do a 'diff' between the files.
What is the difference of the sys.master_files and sys.database_files? I have about 20 databases in my instance but when I query the sys.master_files I do not receive any rows. Why? When I query sys.database_files I get the information about the database files concerning the current database.
sys.master_files :
Contains a row per file of a database
as stored in the master database. This
is a single, system-wide view.
sys.database_files :
Contains a row per file of a database as stored in the database itself. This is a per-database view.
So, SELECT * FROM sys.master_files should list the files for each database in the instance whereas SELECT * FROM sys.database_files should list the files for the specific database context.
Testing this here (SQL 2K8), it works as per the above?
Update:
If you're not seeing rows from sys.master_files, it could be a permissions issue as BOL states:
The minimum permissions that are
required to see the corresponding row
are CREATE DATABASE, ALTER ANY
DATABASE, or VIEW ANY DEFINITION.
Whereas for sys.database_files just requires membership in the public role.
I want to get a list of all of the user databases from an mssql server instance. What's the best way to do this?
I know I can select from sys.databases, but I don't see any way to filter out system databases besides hardcoding a list of names to exclude.
I need the script to work on 2000/2005 and 2008.
If the approach I listed above is the only way to go, what are list of names I should exclude? I don't know if 2005 or 2008 added any new system databases off the top of my head.
Was looking in to this again today and decided to profile what Management Studio was doing to populate the Object Explorer details.
Turns out the solution Microsoft have implemented is pretty simplistic and boils down to the following:
SELECT *
FROM master.sys.databases
WHERE Cast(CASE WHEN name IN ('master', 'model', 'msdb', 'tempdb') THEN 1 ELSE is_distributor END As bit) = 0
Please note that this was performed using SSMS 2008R2 (10.50.4033.0).
The first query will return a table with data regarding all of the databases on the instance:
Select *
From sys.databases
From this table you'll notice you can narrow down the scope of data you're looking for by using the WHERE clause. For example, the following queries will essentially return the same result table (the one you're most likely looking for):
Select *
From sys.databases
Where database_id > 5
Select *
From sys.databases
Where len(owner_sid)>1
These queries will work in SQL Server 2008 and 2012.
On SQL Server 2008 R2 Express, looks like I cannot reliably use any of the above methods. INFORMATION_SCHEMA.SCHEMATA only shows me information in the current database, db_id (database_id) #5 is my first user database, and owner_sid on two of my user databases on one of my mirrored databases (running on SQL Server 2008 R2 Standard) shows owner_sid = 1 for my two most recently created databases. (PablolnNZ's comment above is correct: I did not set an explicit owner for those two databases so it still shows as having an owner of 'sa'.)
The only reliable means I was able to use was the following:
SELECT name FROM sys.databases
WHERE name NOT IN ('master', 'model', 'tempdb', 'msdb', 'Resource')
This works in 2005, not 100% sure about the other versions but I think it will fly. It's a bit of a hack but might get you what you need:
select * from sys.databases where len(owner_sid)>1
As nasty as it sounds to hardcode things. The names and number of system databases has been fairly consistent for several versions of SQL. However, if that is too unpleasant you could semi-hardcode them into a table and then plug that into your query.
Not sure if you can offhand. One note -- on 2k you'll have to use master.dbo.sysdatabases and not master.sys.databases (which doesn't exist in 2k).
Starting with SQL Server 2008 you have access to a view called sys.databases which when joined with sys.server_principals can eliminate the databases owned by sa, which you can (most often) safely discern are the "system databases". Thus, allowing you to filter these items out.
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';
How can I get the list of available databases on a SQL Server instance? I'm planning to make a list of them in a combo box in VB.NET.
Execute:
SELECT name FROM master.sys.databases
This the preferred approach now, rather than dbo.sysdatabases, which has been deprecated for some time.
Execute this query:
SELECT name FROM master.dbo.sysdatabases
or if you prefer
EXEC sp_databases
in light of the ambiguity as to the number of non-user databases, you should probably add:
WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb');
and add the names of the reporting services databases
To exclude system databases:
SELECT [name]
FROM master.dbo.sysdatabases
WHERE dbid > 6
Edited : 2:36 PM 2/5/2013
Updated with accurate database_id, It should be greater than 4, to skip listing
system databases which are having database id between 1 and 4.
SELECT *
FROM sys.databases d
WHERE d.database_id > 4
SELECT [name]
FROM master.dbo.sysdatabases
WHERE dbid > 4
Works on our SQL Server 2008
Use the query below to get all the databases:
select * from sys.databases
If you need only the user-defined databases;
select * from sys.databases WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb');
Some of the system database names are (resource,distribution,reportservice,reportservicetempdb) just insert it into the query if you have the above db's in your machine as default.
Since you are using .NET you can use the SQL Server Management Objects
Dim server As New Microsoft.SqlServer.Management.Smo.Server("localhost")
For Each db As Database In server.Databases
Console.WriteLine(db.Name)
Next
SELECT [name]
FROM master.dbo.sysdatabases
WHERE dbid > 4 and [name] <> 'ReportServer' and [name] <> 'ReportServerTempDB'
This will work for both condition, Whether reporting is enabled or not
I use the following SQL Server Management Objects code to get a list of databases that aren't system databases and aren't snapshots.
using Microsoft.SqlServer.Management.Smo;
public static string[] GetDatabaseNames( string serverName )
{
var server = new Server( serverName );
return ( from Database database in server.Databases
where !database.IsSystemObject && !database.IsDatabaseSnapshot
select database.Name
).ToArray();
}
If you want to omit system databases and ReportServer tables (if installed)
select DATABASE_NAME = db_name(s_mf.database_id)
from sys.master_files s_mf
where
s_mf.state = 0 -- ONLINE
and has_dbaccess(db_name(s_mf.database_id)) = 1
and db_name(s_mf.database_id) NOT IN ('master', 'tempdb', 'model', 'msdb')
and db_name(s_mf.database_id) not like 'ReportServer%'
group by s_mf.database_id
order by 1;
This works on SQL Server 2008/2012/2014. Most of query comes from "sp_databases" system stored procedure. I only removed unneeded column and added where conditions.
Not sure if this will omit the Report server databases since I am not running one, but from what I have seen, I can omit system user owned databases with this SQL:
SELECT db.[name] as dbname
FROM [master].[sys].[databases] db
LEFT OUTER JOIN [master].[sys].[sysusers] su on su.sid = db.owner_sid
WHERE su.sid is null
order by db.[name]
In SQL Server 7, dbid 1 thru 4 are the system dbs.
perhaps I'm a dodo!
show databases; worked for me.
If you are looking for a command to list databases in MYSQL, then just use the below command. After login to sql server,
show databases;
To exclude system databases :
SELECT name FROM master.dbo.sysdatabases where sid <>0x01