Finding a referenced table in multiple views using SQL - sql-server

I have a huge database with 50+ Views, Tables and Stored Procedures. I want to run a search to find I specific piece of text, i.e a table name, to see if it is being referenced anywhere.
I originally tried a C# route but I am suspecting this will be easier in SQL. The logic I am thinking about is possibly creating a query that loops through all Tables, Views and Stored Procedures and returns the data if it is available.
Any ideas?

In the below query, instead of the matchingstring replace your tablename, it will returns list of objects related to the search string
SELECT DISTINCT SO.[name]
FROM sysobjects SO
JOIN syscomments SC ON SC.Id = SO.Id
WHERE SC.[text] LIKE '%matchingstring%'

SELECT object_name(id) FROM sys.syscomments WHERE text LIKE'%yourtablename%'

Related

How to find all tables depends to stored procedure

I want to find all tables used in stored procedures but it only gives me the tables that are in the database that I'm just using. Is there a solution to find all tables used in stored procedures, which are in other databases (in the same server)?
I try this:
SELECT DISTINCT p.name AS proc_name, t.name AS table_name
FROM sys.sql_dependencies d
INNER JOIN sys.procedures p ON p.object_id = d.object_id
INNER JOIN sys.tables t ON t.object_id = d.referenced_major_id
WHERE p.name like '%sp_example%'
ORDER BY proc_name, table_name
The procedures I need to analyze contain tables from different databases, but the code above gives me only results from one database.
First of all, sys.sql_dependencies is deprecated. Instead, you should use sys.sql_expression_dependencies. In it, you will find 4-part names of referenced objects (this includes objects referenced via linked server, for example).
Second, any object_id in SQL Server only makes sense within its database. If you are looking for anything outside of your current DB, don't join tables by these identifiers - use object names instead.

Running the following query to view a list of all tables and views that are feeding into my Store Procedure, but only pulling from selected database

I'm fairly new to SQL, and I'm trying to automate my process of viewing all of the tables (from multiple databases) that feed into a specific proc. When I run the below query, I am only getting results from one database, but I know that this SP pulls from 3+ databases. Any suggestions or entirely new ways of going about this?
SELECT
NAME as 'List Of Tables'
FROM SYSOBJECTS
WHERE ID IN ( SELECT SD.DEPID
FROM SYSOBJECTS SO,
SYSDEPENDS SD
WHERE SO.NAME = 'X-Table-Name'
AND SD.ID = SO.ID
)

Using SQL Server - is there a way to query 'Create view' SQL

I want to know if a certain table dbo.person is being used in any of the database views in my database. There are a lot of views in this database.
I could right click each view "Script View as --> CREATE To" to see the SQL that was used to build this view but this will take a long time.
I am wondering if there is any way to query all of these "create view as" scripts to see if any of them mention my table dbo.person.
I hope this is clear.
You can query the system catalog for views (and use sql_modules to get the view definition) then use the system view sys.sql_expression_dependencies to find which of these views reference dbo.Person:
SELECT ViewName = QUOTENAME(OBJECT_SCHEMA_NAME(v.[object_id])) + '.' + QUOTENAME(v.Name),
m.[Definition]
FROM sys.views AS v
INNER JOIN sys.sql_modules AS m
ON m.[object_id] = v.[object_id]
WHERE EXISTS
( SELECT 1
FROM sys.sql_expression_dependencies AS d
WHERE d.Referenced_id = OBJECT_ID(N'dbo.Person', 'U')
AND v.[object_id] = d.referencing_id
)
ORDER BY ViewName;
There are some small issues with sys.sql_expression_dependencies, but I would still be more inclined to use this than to search for '%person%', since this could bring back 10s, or 100s of extra results, any time a table that contains person (e.g. dbo.PersonAddress) is referenced, or person is used as an alias (SELECT Forename + surname AS Person) etc. It really depends whether you are looking for something that is going to be accurate most of the time, but may, on occasion miss a reference, or whether you need a catch all solution that will bring back extra results.
You could try
select * from INFORMATION_SCHEMA.VIEWS
where VIEW_DEFINITION like '%Person%'
You might need to change the WHERE to dbo.person or [Person]
For a more detailed string search on SQL Server database object definitions please refer to SQL Object Where Used List script
You can simply call like
exec SearchInObjectsText 'name'
and it will return procedures, functions, views, etc where the text "name" is used
I know this doesn't answer the question directly, but it answers the need. I use a third party add-in called SQLSearch from redgate. It's free and I'm not an employee, so this isn't a "plug" of any kind.
I normally use this SQL to look in definitions. I didn't filter on VIEWS just in case it is used somewhere else.
SELECT
so.name, so.type, sm.Definition
FROM
sys.objects AS so INNER JOIN sys.sql_modules AS sm ON so.object_id = sm.object_id
WHERE
sm.Definition LIKE N'%dbo.person%'
ORDER BY so.name
Please query Information_Schema.View_Table system views
select * from INFORMATION_SCHEMA.VIEW_TABLE_USAGE where TABLE_NAME = 'Emp'
Try this
SELECT DISTINCT m.definition, 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 '%person%'
and type_desc = 'VIEW'

How to get properties of a field of table in sybase

Here is details
In sybase, I have a table "abc" having 5 fields(name, roll, address, desc,path). If i would use 'sp_help tablename' then i can see all properties of all fields how i can get properties of a particular field i.e. roll or any one field and their properties using sql or pl/sql.
As we know that we can not use any parameters in sp_help so is there any way to to get the properties of a field, (pl/sql or sql)?
Thanks in advance
sp_help tablename
This depends on what specific properties you are looking for. Assuming you are looking for Table, Column, Datatype, Datatype Length - you have to join sysobjects, syscolumns, and systypes
use YOURDB
go
select o.name, c.name, t.usertype, c.length
from
sysobject o,
syscolumns c,
systypes t
where o.id = c.id
and c.usertype = t.usertype
and o.name like "YOUR_TABLE"
and c.name like "YOUR_COLUMN"
go
If you want more than that, you'll have to bring in other columns and/or tables into the query like sysdepends, sysdefaults or sysconstraints
More information about the system tables can be found here:
Sybase ASE Reference Manual: Tables > System Tables
And the system table diagram, which shows the column mappings can be found here:
http://infocenter.sybase.com/help/topic/com.sybase.infocenter.dc70204.1550/pdf/a155pst.pdf
Also, FYI - Sybase uses T-SQL (like SQL Server), not pl/sql.

sql server: looking for table usage through out database

How would I figure out what type of sql code such as procs, functions, views etc. are interacting with my table called TABLE1 through out a given database. Sample code would be very helpful for me.
thanks
select so.name, so.xtype
from sysobjects so (nolock)
inner join syscomments sc (nolock) on sc.id = so.id
where sc.text like '%tablename%'
This code will search all SQL Server objects for a reference to your table. You have to run this query for each database.
If a stored procedure uses your table it will appear in this query. The same is true of functions, views, and triggers.
xtype tells you the type of object.
Here are the possible xtype values:
D = Field names
F = Foreign Key
FN = Function
P = Stored Procedures
PK = Primary Key
S = System Tables
U = User tables
V = Hidden tables
Not enough info in your question, but one thing you can do is use SQL Profiler to profile where INSERTs, UPDATEs, and DELETEs are coming from.
I assume you are talking about how an app is interacting with data and what name (of say a sproc) is doing the insert / update / delete.
Look at SQL Profiler, it comes with your client tools install. Filter it to only show connections to your database (either db name or ID).
If you've been good and created your SPs/views/functions after your table was created, sp_depends will tell you evertyhing referencing the table. Exept for dynamic sql that is.

Resources