0

Based upon the thread: SQL Server View vs Database

I have created a schema named as Network in my SQL Server Database. The idea is to have logical group of sql views that will be part of this schema.

  1. How to associate the sql view to the schema?
  2. How to execute the view? Network.?
  3. How to view the list of views created in a particular sql server schema?

Example of SQL Server View:

Create view v_Customer
as
Select * from Customer
Rajiv
  • 675
  • 9
  • 21
  • 35

1 Answers1

2

Object(View) names in sql server has 4 part identifiers

ServerName.DatabaseName.schemaName.objectName

When you don't mention the servername and database name then the object will be created on database on which the connection is made.

When you don't mention the schema name while creating a object(view) it will be created on the default schema. Mostly it will be dbo

Just add schema name before the View name

Create view Network.v_Customer
as
Select * from Customer

To view the list of views created in a particular sql server schema

select * 
from sys.objects o
  JOIN sys.schemas s
         ON o.[schema_id] = s.[schema_id]
Where o.type = 'V'
and   s.name = 'schema_name'

The reason for not using SCHEMA_NAME() and joining sys.schemas is here

Bad habits : Using (certain) metadata "helper" functions

Pரதீப்
  • 91,748
  • 19
  • 131
  • 172