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

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'

Related

Combine table and views in T-SQL

I am looking into extract table name and all related views and schemas in my SQL Server.
Until now I have used this SQL code to do that:
SELECT
t.VIEW_SCHEMA,
t.VIEW_NAME,
t.TABLE_SCHEMA,
t.TABLE_NAME
FROM
INFORMATION_SCHEMA.VIEW_TABLE_USAGE t
WHERE
t.TABLE_NAME = 'MYTABLE'
AND t.VIEW_NAME like '%_something%'
The INFORMATION_SCHEMA.VIEW_TABLE_USAGE seems only to be updated when views actually are used, so when I run my code on a table with have new view definitions or a views has never been used it returns nothing.
I have tried to combine INFORMATION_SCHEMA.VIEWS and INFORMATION_SCHEMA.TABLES but I can't find a key to join on.
I have also tried to look into sys.tables and sys.views but I still haven't found a way to join it.
I really need to get the columns specified in SQL.
Any ideas on how to do that? Maybe my approach is all wrong.
SQL Server offers a few possibilities via sys.sql_expression_dependencies and sys.dm_sql_referencing_entities()
sys.sql_expression_dependencies is easier to use, an example for your purpose would be
select
Schema_Name(v.schema_id) View_Schema, v.name View_Name,
Schema_Name(t.schema_id) Table_Schema, t.name Table_Name
from sys.tables t
join sys.sql_expression_dependencies d on d.referenced_id=t.object_id
join sys.objects v on v.object_id=d.referencing_id and v.type='V'
where
t.name='tablename'
and v.name like '%something%'

How to check a SQL Server view dependencies

I need to modify a SQL Server view in a large database and need to know what other objects in this database use this view. How can I do that?
I tried viewing dependencies in SQL Server Management Studio but that does not list the objects that depend on this view (I know of at least one stored procedure that depends on this view, and it is not listed as a dependency).
The other option that I considered is scripting the whole database and doing the text search in it, but I want to see if there is an automated way to do this.
Try to query syscomments table and find the usage.
select o.name,o.type from sys.syscomments c
inner join sys.objects o
on o. object_id = c.id
where c.text like '%your view name%'
Update #1
select o.name,o.type from sys.sql_modules m
inner join sys.objects o
on o.object_id = m.object_id
where m.definition like '%your view name%'

list of views referencing a table

Is there any way to know if a particular table is being referenced by any Views or not.I used the below code which gives only SP's and function names:
select * from sys.objects p inner join sys.sql_modules m
on p.object_id = m.object_id
where m.definition like '%abc%'
Please help!!!
select *
from INFORMATION_SCHEMA.VIEWS
where VIEW_DEFINITION like '%abc%'
First, your query gives views in the result set (I tried it on AdentureWorks2012 -> Production.Product table):
If you're using SQL Server 2008 or above, you can use the sys.sql_expression_dependencies catalog view. For example:
SELECT
referencing_object_name = o.name,
referencing_object_type_desc = o.type_desc,
referenced_object_name = referenced_entity_name,
referenced_object_type_desc = o1.type_desc
FROM sys.sql_expression_dependencies sed
INNER JOIN sys.objects o
ON sed.referencing_id = o.[object_id]
LEFT OUTER JOIN sys.objects o1
ON sed.referenced_id = o1.[object_id]
WHERE referenced_entity_name = 'YourTable'
It will give you nice look on each by-name dependency on a user-defined entity
For column level dependencies you can use the sys.dm_sql_referenced_entities function
Hope this helps
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 that doesn't fit your bill - you could also check out the sysdepends catalog view in SQL Server - it lists what objects depend on what (see details in the MSDN docs).
To find out what objects depend on a given table, you could use something like:
SELECT
id,
OBJECT_NAME(ID)
FROM sys.sysdepends
WHERE depid = OBJECT_ID('YourTable')
That should give you a list of all objects depending on that table (or view or whatever you're checking)

Query to get list of views that use a certain field in SQL Server

Does anybody know of a query from system tables or views to get a list of views that use a certain field in a SQL Server database?
SELECT *
FROM
sys.sql_modules m
JOIN
sys.views v ON m.object_id = v.object_id
WHERE
m.definition LIKE '%MyTable%' --or '%MyField%'
INFORMATION_SCHEMA views and legacy syscomments are unreliable for large view definition (or any definition) because they have nvarchar(4000) fields. sys.sql_modules uses nvarchar(max).
They should not be used
sys.sql_expression_dependencies may be an alternative but is more complex to use.

SQL Query to search schema of all tables

I am working on a SQL Server 2008 Db that has many tables in it (around 200). Many of these tables contain a field by the name "CreatedDate". I am trying to identify all the table schema with this particular field.
Is there a SQL query to do this?
I would query the information_schema - this has views that are much more readable than the underlying tables.
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%create%'
You can also try doing this using one of many third party tools that are available for this.
Queries are great for simple searches but if you need to do more manipulation with data, search for references and such this is where you can do a much better job with these.
Also, these come in very handy when some objects are encrypted and you need to search for
I’m using ApexSQL Search which is free but there are also many more (also free) on the market such as Red Gate or SSMS Tool Pack.
select object_name(c.object_id) as table_name
, schema_name(t.schema_id) as schema_name
from sys.columns c
join sys.tables t on c.object_id = t.object_id
where c.name=N'CreatedDate';
It gets a little more complicated if you want alsoother table properties, but you'll refer to the object catalog views like sys.tables, sys.columns etc.
My favorite...
SELECT objParent.name AS parent, obj.name, col.*
FROM sysobjects obj
LEFT JOIN syscolumns col
ON obj.id = col.id
LEFT JOIN sysobjects objParent
ON objParent.id = obj.parent_obj
WHERE col.name LIKE '%Comment%'
OR obj.name LIKE '%Comment%'
Above I'm searching for "Comment".
Drop the percent signs if you want a direct match.
This searches tables, fields and things like primary key names, constraints, views, etc.
And when you want to search in StoredProcs after monkeying with the tables (and need to make the procs match), use the following...
SELECT name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%Comment%'
Hope that helps, I find these two queries to be extremely useful.
For me I only have read access to run querys so I need to use this function often here is what I use:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
where TABLES.TABLE_NAME like '%your table name here%'
You can replace .TABLES with .COLUMNS then it would look like this:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE columns.COLUMN_NAME like '%your column name here%'
Use this query :
SELECT
t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name , *
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
Where
( c.name LIKE '%' + '<ColumnName>' + '%' )
AND
( t.type = 'U' ) -- Use This To Prevent Selecting System Tables
Same thing but in ANSI way
SELECT
*
FROM
INFORMATION_SCHEMA.TABLES
WHERE
TABLE_NAME IN (
SELECT
TABLE_NAME
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
COLUMN_NAME = 'CreateDate'
)
You do not need to type SQL Query for this in SQL Server 2008.
In SSMS Object Explorer choose Databases or Tables of the required database (if you need to search in one database), open menu View--> Object Explorer Details (alternatively press F7), type %CreatedDate% in Search textbox, ENTER, enjoy

Resources