List Database Scoped DM Views in SQL Server - 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;

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

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]

SQL Server: sys.master_files vs. sys.database_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.

How to make a select query for sql and access databases?

Using SQL server 2000 and Access 2003
Access Database Name - History.mdb
Access Table Name - Events
SQL Database Name - Star.mdf
SQL Table Name - Person
I want to take the field from person table, and include in Events table by using inner join
Tried Query
Select * from Events inner join person where events.id = person.id
So How to make a query for access and sql databases.
I want to make a Select query in access only. Not an sql Database.
Need Query Help?
While you can (possible, should -- why?) use a linked table, there are as ever more than one way to skin a cat. Here's another approach: put the connection details into the query test e.g. something like
SELECT *
FROM [ODBC;Driver={SQL Server};SERVER=MyServer;DATABASE=Star;UID=MyUsername;Pwd=MyPassword;].Person AS P1
INNER JOIN
[MS Access;DATABASE=C:\History;].[Events] AS E1
ON S1.seq = S2.seq
WHERE E1.id = P1.id;
You can set up a linked table in Access to your SQL Server, and the instructions on how to do so vary slightly in Access versions. Look in the help file for "Linked Table", or go here if you have Access 2007.
Once you have a linked table set up, you'll be able to access the SQL Server table in your query. Note that optimizing a linked table join takes some work.
You can create a linked table in Access, that points to the table in SQL. The rest you can achieve in the query designer.
You should add the msaccess db as a remote server.
then you can do that join

SQL 2005 Find Out Who Created a View

Does SQL store any information about who originally created a view, or who last modified it?
It's too late now, but if you were using 2008 you could create an audit that will track future changes.
EDIT: found it!
select p.name, v.*
from sys.all_views v, sys.database_principals p, sys.schemas s
where p.principal_id = s.principal_id
and v.schema_id = s.schema_id
and v.name = 'your_view_name'
This will produce a number of interesting details about the views in your database, including the column principal_id. Join with sys.database_principals on principal_id for the username!
I am not sure if there is a way to see who created the view but sp_help will get you some information on when it was created
sp_help viewMyView
sp_help works on any and all database objects BTW.
SQL Server does not store explicit information about who created or modified an object. There is information in the metadata catalog about who is the owner of a given object, or to what schema does the object belong to:
select * from sys.objects where object_id = object_id('<object name>');
Depending on the object type either the principal_id is populated with the database principal ID of the owner, or the schema_id is populated with the Id of the schema to which the object belongs. All schemas have an owner and which can be retrieved from the metadata catalog:
select * from sys.schemas
However note that these will only reveal the owner of the object. The owner does not necessarily means the user that created it or modified it. Ownership of objects can be changed during creation or after creation with the ALTER AUTHORIZATION statement, making identification by ownership unreliable at best. Also all members of sysadmin role map to the same database principal, dbo, in every database.
To properly identify the the user that created an object you should deploy auditing methods, but that require prior deployment of the audit. Some forensics can be done after the fact if audit was not deployed:
You can dig into the log file, Paul Randal has an example in his recent blog.
You can look into the default trace

Resources