Find Root disk Path with SQL query - sql-server

How do I find Server Root disk name with MS SQL query?
For Ex: c:\
Thanks!

SELECT DISTINCT DB_NAME(dovs.database_id) DBName,
mf.physical_name PhysicalFileLocation,
dovs.logical_volume_name AS LogicalName,
dovs.volume_mount_point AS Drive,
CONVERT(INT,dovs.available_bytes/1048576.0) AS FreeSpaceInMB
FROM sys.master_files mf
CROSS APPLY sys.dm_os_volume_stats(mf.database_id, mf.FILE_ID) dovs
ORDER BY FreeSpaceInMB ASC
GO
It will give you the DBname, Physical file location, Logicaldiskname,Drive,FreeSpace. I use this query.

Related

How to Find Whether SQL Server 2016 Db is set to be Memory Optimized or not?

I want to get information through SQL query for any database in SQL Server 2016 whether it is already set to be memory optimized or not. I know following query
SELECT DatabasePropertyEx('DATABASENAME', 'IsXTPSupported');
tells you that whether you can set (by setting the Memory Optimized Data file Group) the database to have in memory tables or not. But I want to know that whether db is already set to contain in memory table or not i.e. its Memory Optimized Data File Group have been already set or not?
How I can achieve this through SQL Query for any given database?
Googling the title of your question I found this: https://msdn.microsoft.com/en-CA/library/dn232522.aspx
SELECT
quotename(schema_name(o.schema_id)) + N'.' + quotename(o.name) AS [table],
s.name AS [statistics object],
1-s.no_recompute AS [auto-update enabled]
FROM sys.stats s JOIN sys.tables o ON s.object_id=o.object_id
WHERE o.is_memory_optimized=1
Try this:
SELECT distinct DB_NAME()
FROM sys.master_files sdf
INNER JOIN
sys.filegroups fg
ON sdf.data_space_id=fg.data_space_id and fg.type='FX'
Many ways... One more alternative than the above list:
SELECT type, name, memory_node_id, pages_kb/1024 AS pages_MB   
FROM sys.dm_os_memory_clerks WHERE type LIKE '%xtp%' 

SQL Server - get all databases and files

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

Comparing two Sybase databases

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.

Determine MDF and LDF file match

Is there a way to determine what MDF goes with what LDF file for SQL Server? We had a server crash and pull these files off and were only named with a random integer for the file name. So now we need to guess which MDF and LDF go together to get them up but what is the best way to do that?
You would find your current database's MDF and LDF with this:
sp_helpdb 'YourDBName'
Or you could see everything you have in your instance:
SELECT name, physical_name AS current_file_location FROM sys.master_files
In case of offline scenario try this:
SELECT DB.name, MF.name, MF.type_desc, MF.physical_name
FROM sys.databases DB
INNER JOIN sys.master_files MF ON db.database_id = mf.database_id
WHERE DB.state = 6
When DB.State= 6 means offline state.

Get list of databases from SQL Server

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

Resources