SQL Server: sys.master_files vs. sys.database_files - sql-server

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.

Related

How do I select current database name and all the schema for the database name in SYBASE ASE using a query

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

List Database Scoped DM Views in SQL Server

Server-scoped Dynamic Management View are stored only in the Master database.
SELECT name, type, type_desc
FROM sys.system_objects
WHERE name LIKE 'dm%'
ORDER BY name
How to list Database-Scoped DM Views and where it stored?
Can you advise?
Server-scoped Dynamic Management View are stored only in the Master
database.
Not exactly. DMVs are stored in the internal mssqlsystemresource database but are visible in all databases. You should get the same results if you run your query from any database, assuming you're not limited by permissions.
Database-scoped DMVs generally have prefix 'dm_db_' and can be listed with the query below.
SELECT name, type, type_desc
FROM sys.system_objects
WHERE name LIKE N'dm%[_]db[_]%'
ORDER BY name;

How to set default catalog with linked server in Mssql

Formerly, I had two databases in one physical server. ('People' and 'Work' Database)
So I used the below query when 'People' database information need at 'Work' database.
select * from People.dbo.information
But, It occurred to me something that one physical databases have to be seperatored with two physical server.
Therefore I made linked server connection at 'People' database server for refering Information at 'Work' database same as before name and Then I seted dafualt catalog the 'People' in linked server.
Although I already seted default catalog, I have to enter database's name and can't skip the name.
Phycal Databases was seperatored but I want to use previous query using linked server.
Example)
If i made linked server as called 'Peoplo', I have to use below query.
select * from People.People.dbo.information
--select * from [linked name].[db name].[dbo].[table Name]
I want to use below query.
select * from People.dbo.information
--select * from [linked name].[dbo].[table Name]
--Then, linked name is seted with default catalog as 'People'
If you have set the "default database" for the login on the linked server, you can use this (2 dots, skipping default catalog)
select * from People..dbo.information
Note that however you do it, linked servers can have performance issues when joining between servers.
Also note, the data has no referential integrity at all. The databases will not be synchromised or coherent in case of any downtime or restores etc

Tables created form model db MS SQL server

Long time I have not changed active database (like USE mydbname) and created bunch of tables into a master database I think. Ever since then when new databases are created these tables appears in it.
I think one of the four default databases (master, model, msdb, tempdb) works as model for new databases and therefore the "extra" tables must be stored somewhere. Based on this description, could you please advice me how to get rid of these tables in order to create new empty databases?
Do you want the get the user tables in system database?
you can try this:
EXEC sys.sp_MSforeachdb #command1 = N'use ?;select ''?'', * from sys.tables WHERE type=''U'' and is_ms_shipped=0'

tsql: select view from different database

Is it possible to select view defined in different database in MS SQL Server?
All my searching results point to defining view to use data from different database, but haven't found if it possible to select view from another database yet.
suppose you want to do a select on database DBOther than it would be :
select * from DBOther..TableName
Also check if the table or view is on the dbo schema, if not you should add the schema also : Please notice I use only one dot now after the database name
select * from DBOther.dbo.ViewName
Make sure the Database is in the Linked Server if they are not on the same server.
Then you can access the table or view on that database via:
SELECT * FROM [AnotherServerName].[DB].[dbo].[Table]
If on same server:
SELECT * FROM [DB].[dbo].[Table]

Resources