System Views

When we create a new Database then SQL Server automatically creates System Views.

The following are a few System Views:

  1. sys.databases
  2. sys.tables
  3. sys.triggers
  4. sys.views
  5. sys.procedures



database

systemViews

sys.databases view

The sys.databases view:

SELECT name[DBName],create_date[CreatedDate],user_access_desc[UserAccessDescription] FROM sys.databases

system database

sys.tables view

The sys.tables view returns all the tables information (like table name, type of table, created date and modified date and so on) in a database:

  1. CREATE DATABASE DB_SystemViews
  2. GO
  3. CREATE TABLE tbl_defaultValues
  4. (
  5. Id INT IDENTITY(1,1) not null
  6. )
  7. GO
  8. CREATE TABLE tbl_parent
  9. (
  10. Id INT IDENTITY(1,1) CONSTRAINT PK_tbl_parent_Id PRIMARY KEY,
  11. Name VARCHAR(50)
  12. )
  13. GO
  14. CREATE TABLE tbl_child
  15. (
  16. Id INT IDENTITY(1,1) PRIMARY KEY,
  17. Name VARCHAR(50),
  18. ParentId INT CONSTRAINT FK_tbl_parent_tbl_child_ParentId FOREIGN KEY REFERENCES tbl_parent(Id)
  19. )
After creating the table if we want to see the created tables in the database DB_SystemViews.
  1. USE DB_SystemViews
  2. GO
  3. SELECT
  4. name[TableName],
  5. type_desc[Description],
  6. create_date[TableCreatedDateWithTime],
  7. modify_date[TableModifeidDateWithTime]
  8. FROM sys.tables
  9. GO

The preceding query returns DB_SystemViews database tables.

table

sys.triggers view

The sys.triggers view returns all the triggers information (like trigger name, type of trigger, created date and modified date and so on) in the database tables.

Create simple triggers for two different tables (tbl_parent and tbl_child).

  1. USE DB_SystemViews
  2. GO
  3. CREATE TRIGGER trg_parentTrigger ON tbl_parent FOR INSERT
  4. AS
  5. INSERT INTO dbo.tbl_defaultValues default values
  6. GO
  7. CREATE TRIGGER trg_childTrigger ON tbl_child FOR INSERT
  8. AS
  9. INSERT INTO dbo.tbl_defaultValues default values
  10. GO
If we want to see the triggers in a database DB_SystemViews.
  1. USE DB_SystemViews
  2. GO
  3. SELECT
  4. name[TriggerName],
  5. type_desc[TriggerType],
  6. create_date[TriggerCreatedDate],
  7. modify_date[TriggerModifiedDate]
  8. FROM sys.triggers

triggers

  1. USE DB_SystemViews
  2. GO
  3. --disable the trigger
  4. DISABLE TRIGGER trg_childTrigger ON dbo.tbl_child
  5. GO
  6. SELECT
  7. name[TriggerName],
  8. OBJECT_NAME(parent_id)[TableName],
  9. is_disabled[Status]
  10. FROM sys.triggers
  11. GO

disable the trigger

sys.views view

The sys.views view returns all the views information (like view name, type of view, created date and modified date and so on) in a database.

The following shows how to create a simple view:

  1. USE DB_SystemViews
  2. GO
  3. CREATE VIEW vParent
  4. AS
  5. select * from tbl_parent
  6. GO
If we want to see the views in a database DB_SystemViews.
  1. USE DB_SystemViews
  2. GO
  3. SELECT
  4. name[ViewName],
  5. type_desc[Description],
  6. create_date[ViewCreatedDateWithTime],
  7. modify_date[ViewModifeidDateWithTime]
  8. FROM sys.views
  9. GO

query

sys.procedures view

The sys.procedures view returns all the procedures information (like procedure name, type of procedure, created date and modified date and so on) in a database.

The following shows how to create a simple proc:

  1. USE DB_SystemViews
  2. GO
  3. CREATE PROC sp_parent
  4. AS
  5. SELECT * FROM tbl_parent
  6. GO
If we want to see the procedures in a database DB_SystemViews.
  1. USE DB_SystemViews
  2. GO
  3. SELECT
  4. name[ProcName],
  5. type_desc[Description],
  6. create_date[ProcCreatedDateWithTime],
  7. modify_date[ProcModifeidDateWithTime]
  8. FROM sys.procedures

SystemViews image