Check view dependencies from snapshot database - sql-server

I want to find all objects in which view from snapshot database is called in the database.
For objects created in the database, that is possible using View Dependencies option.
Is there any way to find that out for views created in snapshot database?

Sure. I'm assuming that you want to answer the question "what objects does the view reference?". You can find that with the following query:
select referenced_schema_name, referenced_entity_name
from sys.sql_expression_dependencies
where referencing_id = object_id('HumanResources.vEmployee');
Obviously replace HumanResources.vEmployee with your view (I used the venerable AdventureWorks2019 database as a test).

Related

Why INFORMATION_SCHEMA.VIEWS does not list up materialized view in Snowflake?

I could not able to see materialized view while executing below query in Snowflake even though current role in session is owner role which has all the privileges on an object (here materialized view)
Select * from MYDB.INFORMATION_SCHEMA.VIEWS
Snowflake documentation mention following
This Information Schema view displays a row for each view in the specified (or current) database, including the INFORMATION_SCHEMA views for the database.
Although while executing below command , I am able to see all the views including materialized views.
Show Views in DATABASE MYDB
Currently, there is a problem with the information_schema.views not listing the MV details. Instead of information_schema.views , the details are present in information_schema.tables (table_type column).
There is an improvement request already filed with the snowflake engineering team to correct this issue and include the MV details in the information_schema.views view. The improvement request has been approved however there is no ETA at this time. Please use the information_schema.tables view at this time to gather the required information.

Checking views and their default dependencies

Is it the default for a View to reference base tables in its own database in SQL Server Management Studio if it doesn't explicitly point to a specific database? I have tables with the same names across several databases on the same server and I'm not sure how to check which tables it's using. I used this query to see the Table_Catalog:
SELECT view_name, Table_Name,* FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
However I saw a warning that querying the sys.objects is the only way to really confirm the dependencies but I don't have a good query to see database information.
Appreciate any help. Thank you.
A view same as any query will reference tables from it's own database unless database name is explicitly mentioned with the table name like databaseOne.dbo.TableName

MSSQL - Find and Replace text in ALL stored Procedures

I recently renamed a table in my database. As the result, I need to find and replace the table name in all of the stored procedures in my database.
SELECT DISTINCT
o.name AS Object_Name,
o.type_desc
FROM sys.sql_modules m
INNER JOIN
sys.objects o
ON m.object_id = o.object_id
WHERE m.definition Like '%myoldtable%'
order by OBJECT_NAME desc
When I run the script above, I get 230 matches. There are 230 stored procedures in my database that somehow reference that table name. I want to do a massive find and replace from myoldtable to mynewtable. Is that possible?
I'd suggest using an SSDT database project in Visual Studio (or another database management tool) - it's fairly simple to import the database objects and refactor the table name, then create a change script to apply to your database(s). You can then use this project to version control changes to your DB and even integrate with CI/CD. Here are some links to get you started:
https://learn.microsoft.com/en-us/sql/ssdt/import-into-a-database-project?view=sql-server-2017
https://learn.microsoft.com/en-us/sql/ssdt/how-to-use-rename-and-refactoring-to-make-changes-to-your-database-objects?view=sql-server-2017
Here is one of third party tool, i personally liked it much as it's simple and easy to use, it basically used for search text within actual data of a database.
However, along with that search feature, there is an add-on feature Safe Rename which would be available (after the installation) every right-click menu of the object.

How to create schema bound, cross-database view in SQL Server

I am attempting to create an indexed view on SQL Server 2008. I have a master database in which I cannot make any changes to (in terms of adding tables, views, etc.). However, I need to create some different views for various reasons that need to work with live data.
I have created a new database along side my master database so I can create views there. I am able to create views just fine, but I want to index some of the larger views. However, when I try to create a schema bound view cross-database, I receive the following error:
Cannot schema bind view 'dbo.Divisions' because name
'master.dbo.hbs_fsdv' is invalid for schema binding. Names must be in
two-part format and an object cannot reference itself.
Since I am going cross-database with the views, I have to reference the name in three-part format.
My creation statement for the view:
CREATE VIEW dbo.Divisions WITH SCHEMABINDING AS
SELECT master.dbo.hbs_fsdv.seq_ AS DivisionID,
master.dbo.hbs_fsdv.fs_division_desc_ AS Description
FROM master.dbo.hbs_fsdv
How can I create an indexed cross-database view in SQL Server?
Plain and simple. You can't. From the MSDN page:
The view must reference only base tables that are in the same database as the view.
https://msdn.microsoft.com/en-us/library/ms191432.aspx
Although (per the docs) it cannot be done directly with a simple SQL statement, this use case is very common and has a solution.
The architecture would have to involve caching the remote tables into your centralized database, and building the indexed view on top of them.
Some good notes on this can be found here:
What is the best way to cache a table from a (SQL) linked server view?
and
https://thomaslarock.com/2013/05/top-3-performance-killers-for-linked-server-queries/

SQLDeveloper copy database

I'm trying to copy a database for use in testing/developing, in SQLDeveloper I can only see the user views, the data objects are not accessible for me.
Is there anyway to copy the views only and get a dll that creates some sort of phantom structure for the data objects that are not reachable but referenced in the sql queries for those views? Problem is there are over a thousand such references,
In the example below I cannot reach the header object due too permissions,
Example:
CREATE OR REPLACE FORCE VIEW "TRADE"."EXCHANGE" ("MSGQUE", "MSGQUE2") AS
select msgque, msgque2
from head.msgqueues;
I have tryed to export the views in SQL developer but when I import it in my Oracle test database the views contain error and are unusable because the data object did not get exported in the export.sql file,
Thanks in advance
I recommend using the expdp utility to perform this. You can explicitly say to grab views and tables.
Example parfile:
SCHEMAS=SCOTT
INCLUDE=TABLE:"IN ('DEPT')"
INCLUDE=VIEW
DIRECTORY=datapump
DUMPFILE=dept.dmp
LOGFILE=dept.log
Then you can impdp that parfile into the DB you wish and you will have the TABLE and the VIEW that goes in the schema. You can modify the IN clause to grab whatever naming scheme you would need.

Resources