Finding all views that are using linked server - sql-server

I am updating url for linked servers. Before make the changes, I would like to know all views that have reference to this linked servers. Is there any programmatic way (TSQL) to perform this task?
Thanks for your help.
I am using SQL Server 2005, 2008 and 2012. The database servers that referencing linked servers are mostly SQL Server 2005

While it may return false positives, and won't capture any cases where a four-part name is constructed using dynamic SQL, this is probably the simplest approach:
SELECT name FROM sys.views
WHERE LOWER(OBJECT_DEFINITION([object_id])) LIKE LOWER('%LinkedServerName%');

This will find the views:
SELECT t2.name, OBJECT_DEFINITION(t1.[object_id]) view_definition
FROM sys.views t1 join sys.servers t2 on
OBJECT_DEFINITION(t1.[object_id]) like '%['+ t2.name + '].%' ESCAPE '['
It can fail if a table, view, schema or database has same name as a linked server.
In case some views have eluded the first check you can add this line this part is not checking for the square brackets surrounding the linked server name. But be aware that this part is more likely to include
extra unwanted views
or OBJECT_DEFINITION(t1.[object_id]) like '% '+ t2.name + '.%'
EDIT: Changed sys.sysservers to sys.servers. Thanks Aaron Bertrand

If you need to find database objects (e.g. tables, columns, triggers) by name - have a look at the FREE Red-Gate tool called SQL Search which does this - it searches your entire database for any kind of string(s).
It's a great must-have tool for any DBA or database developer - did I already mention it's absolutely FREE to use for any kind of use??

Try This:
SELECT name, OBJECT_DEFINITION([object_id]) FROM sys.views
where OBJECT_DEFINITION([object_id]) like '%.%.dbo.%'

Related

Use Cases of sys.databases and master.dbo.sysdatabases

I'm trying to learn mssql with Rocky Linux on RHEL 8, however I've found examples of people pulling database names using
SELECT name FROM master.dbo.sysdatabases
I don't understand how this command was created, I'm not finding anything that points to using master.dbo.
I used this to obtain the same results, in my mind this is more straight forward and makes more sense. Maybe I'm missing something crucial - but that's why I'm asking a question.
SELECT name FROM sys.databases
Maybe in my use-case it doesn't make a difference, but is there a preferred method between these options?
I feel like I'm pulling hairs, but I'm just curious.
master is the name of the primary database, it is where many DMVs and catalog views are sourced, though several of those are also accessible from other databases (and even when referenced from other schemas).
master.dbo.sysdatabases
^ Specifically, this is a 3-part name, in the form [database].[schema].[object]. But it references a backward compatibility view that is only still there to avoid breaking code written before SQL Server 2005. You can see plenty of warnings about this here:
sys.sysdatabases
Note that it works with both dbo.sysdatabases and sys.sysdatabases because the schema part of the name is essentially ignored.
Your instinct is right that the catalog view is the better way to query the list of databases, both because it isn't deprecated and because it includes a much more complete set of properties about a database:
SELECT * FROM sys.databases;
But you could also say:
SELECT * FROM mydatabase.sys.databases;
SELECT * FROM master.sys.databases;
But there are other catalog views where this is not true. For example, these will give very different, database-specific results:
SELECT * FROM mydatabase.sys.tables;
SELECT * FROM master.sys.tables;
Some other background:
System Catalog Views
Querying the SQL Server System Catalog FAQ
System Databases

How can I query all my view designs looking for a particular string in SQL Server?

There are views created based on another system views in our SQL Server database. Since that another system is going to get replaced, I need to find the list of views that are built based on another system.
When I open the design of an existing view that is built on another system view, I can see another system server name.
So, I am looking for a query that queries all my view designs looking for a particular string 'Server Name', in this case.
Appreciate your support!
Okay, based on what I understand you are looking for some string in the definition of your view, if that's the case you can do the following:
SELECT
QUOTENAME(OBJECT_SCHEMA_NAME(o.object_id)) + '.' + QUOTENAME(o.name) AS [ObjectName],
o.type_desc AS [ObjectType]
FROM sys.objects AS o -- Will check all objects, inlcuding Views
WHERE OBJECT_DEFINITION(o.object_id) LIKE '%cfs%' -- string in the definition
Note that I am checking against sys.objects instead of sys.views to look for the same keyword in other objects like procedures, tables, etc. you can change sys.objects to sys.views if you are only interested on views.

SQL Server : reverse engineering view and stored procedure

I need some help from your side.
For example I have a SQL Server view MyView.
Adb.vs.MyView
with columns:
ID
Name
Address
Email
Phone
Logic behind the view is next
SELECT
Ac.AccountID AS ID,
Ac.AccountName AS Name,
Ad.Main_Adress AS Address,
Em.Main_Email AS Email,
Concat(Ph.Phone_Area_Code,Ph.Mobile_Phone_Number) AS Phone
FROM
Bdb.dbo.Account AS Ac
INNER JOIN
Cdb.dbo.Address AS Ad ON Ac.AccountID = Ad.AccountID
INNER JOIN
Cdb.dbo.Emails AS Em ON Ac.AccountID = Em.AccountID
INNER JOIN
Cdb.dbo.PhoneBook AS Ph ON Ac.AccountID = Ph.AccountID
NOTE:
No KEY relations build between all this tables.
My target to reverse engineer this view to get next kind of result:
Please suggest any kind of tool/tools or scripts to perform this.
Also, if somebody know similar solution but for Rev.En. stored procedures which was used to populating data into tables I will be really appreciated
bec. I will need to reverse tons of such kind views and stored procedures in nearest future.
Thanks in advance for any kind of support !
Take a look at the system views. Most of what you want is probably available in INFORMATION_SCHEMA.VIEW_COLUMN_USAGE. For example:
USE Adb
GO
Select * from INFORMATION_SCHEMA.VIEW_COLUMN_USAGE
where VIEW_NAME='MyView'
GO
Views are just stored SQL scripts that SQL Server adds into your query as a sub select. Consequently the fields used are not actually saved within SQL Server in the same way that table definitions are. Your best bet is to script out all your views using SQL Server Management Studio and plug the files into a tool such as the General SQL Parser which can output the columns and tables that are used in that script.
It isn't perfect but should get you a long way towards what you are trying to achieve. You can try it for free here.

Determine Which Objects Reference a Table in SQL Server

I need to rename a table that has many columns and stored procedures that process against that table. How can one get all Items in database that have a relation to a table in such a scenario?
Using sys.dm_sql_referencing_entities:
SELECT
referencing_schema_name, referencing_entity_name, referencing_id,
referencing_class_desc, is_caller_dependent
FROM
sys.dm_sql_referencing_entities ('mySchemaName.myTableName', 'OBJECT');
GO
where 'mySchemaName.myTableName' is your schema.table, for example 'dbo.MyTable'
If you need to find database objects (e.g. tables, columns, triggers) by name - have a look at the FREE Red-Gate tool called SQL Search which does this - it searches your entire database for any kind of string(s).
It's a great must-have tool for any DBA or database developer - did I already mention it's absolutely FREE to use for any kind of use??
if you want of ref of DB item like table, column, procedure, etc..
You can use visual-expert tools, we can analyse the code SqlServer code
more info: https://www.visual-expert.com/EN/visual-expert-documentation/code-cross-references/

In SQL Server Management Studio, does this type of query run faster?

My coworker showed me this query to look for a certain column:
select *
from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME
like '%id%'
Does this help it run faster, to use the .COLUMNS notation?
thanks a lot?
I agree with Martin. I much prefer this query:
SELECT OBJECT_SCHEMA_NAME(object_id), OBJECT_NAME(object_id), name
FROM sys.columns
WHERE name LIKE '%id%';
The reason I prefer sys. over INFORMATION_SCHEMA.? Mostly because - while the INFORMATION_SCHEMA views are standard - they're already incomplete, and to add insult to injury they don't pick up new features in newer versions of SQL Server. I blogged about it here.
But your question seems to be missing some context in any case. What other methods of "looking for a certain column" are you comparing this to?

Resources