How do I query schema bound views? - sql-server

I can list all views in SQL Server 2008 by using
SELECT * FROM sys.views
What I want to do is to list only the views that are schema bound. How can I do this?

SELECT *
FROM sys.views
WHERE OBJECTPROPERTY(object_id, 'IsSchemaBound') = 1

I needed to find the schema bound views for a specific table. This worked for me in SQL Server 2019.
select distinct
o.type, SCHEMA_NAME(o.schema_id), o.name
--, *
from sys.sql_dependencies d
inner join sys.objects o
on d.object_id = o.object_id
where d.class = 1 -- OBJECT_OR_COLUMN_REFERENCE_SCHEMA_BOUND
and d.referenced_major_id = OBJECT_ID(N'dbo.MyTable')
order by 1, 2, 3
;

Related

How to list all database objects with no dependencies?

I would like to list all database objects in a Microsoft SQL database that have no dependencies - e.g. there are no other database objects that depend on them.
I could find this for each table in SQL Server Management Studio, but this is very time consuming (right click on a table, "Check Dependencies"):
(example of a table with no dependencies)
I'm looking how to do this programatically for all database objects in a database - in T-SQL.
You can use the following script to get all objects with no dependendcies.
SELECT
schema_name = s.name,
o.name,
FROM sys.objects o
JOIN sys.schemas s ON s.schema_id = o.schema_id
WHERE NOT EXISTS (SELECT 1
FROM sys.objects o2
WHERE o2.parent_object_id = o.object_id
)
AND NOT EXISTS (SELECT 1
FROM sys.sql_expression_dependencies sed
WHERE sed.referenced_id = o.object_id
);
Note that the first NOT EXISTS will exclude all objects with keys, indexes or defaults. If you only want to look at procedures, functions and views, then remove that.
SELECT
schema_name = s.name,
o.name,
FROM sys.objects o
JOIN sys.schemas s ON s.schema_id = o.schema_id
WHERE NOT EXISTS (SELECT 1
FROM sys.sql_expression_dependencies sed
WHERE sed.referenced_id = o.object_id
);
Note also that dependency checking is not perfect. In particular, dynamic SQL obviously doesn't work, and it won't check application code.
But it's also not reliable for cases when the schema is not specified in the reference.
Charlieface, thanks again for your answer!
I just needed this again and I revisited your answer. I have a small update to your solution / query.
The query returns system tables too, which is something one probably doesn't want to have in the results list.
Updated query:
SELECT
schema_name = s.name,
o.name,
o.type_desc
FROM
sys.objects o
INNER JOIN
sys.schemas s ON s.schema_id = o.schema_id
WHERE
NOT EXISTS (SELECT 1 FROM sys.objects o2 WHERE o2.parent_object_id = o.object_id)
AND NOT EXISTS (SELECT 1 FROM sys.sql_expression_dependencies sed WHERE sed.referenced_id = o.object_id)
AND o.type_desc<>'SYSTEM_TABLE'

How can I search for stored procedures executing other stored procedures?

I have a list of about 350 stored procedures like this:
usp_SP1,
usp_SP2
...
I want to search through each one looking to see if any of them call other stored procedures or other databases?
I guess I would look for a line like 'exec something' in each one or a specific name of a database. ex. some_other_database
How would I do this to give me a list of the stored procedures that call other stored procedures or contain some specific string? ex. "some other database name"
I can run this below but it finds just the text. is there any way I can ensure it's a exec call and not just text?
USE [Your_DB];
GO
SELECT
ROUTINE_NAME, ROUTINE_DEFINITION
FROM
INFORMATION_SCHEMA.ROUTINES
WHERE
ROUTINE_DEFINITION LIKE '%exec %'
AND ROUTINE_TYPE = 'PROCEDURE'
AND ROUTINE_NAME IN ('usp_SP1', 'usp_SP2')
GO
You can query the sys.sql_dependencies view, like this:
SELECT o1.name AS CallerSP, o2.name AS CalledSP
FROM sys.sql_dependencies sd
INNER JOIN sys.objects o1 ON o1.object_id = sd.object_id
INNER JOIN sys.objects o2 ON o2.object_id = sd.referenced_major_id
WHERE o1.type='P' AND o2.type='P'
You may need to call sp_refreshsqlmodule for all objects before executing this query, if the called SP was created after the caller.
Other options could be to query the sys.sql_expression_dependencies view or the sys.dm_sql_referenced_entities function.
Not to compete, Razvan Socol is correct. Adding the ways to do it in earlier version back to 2000. All the old tables and views are still query-able even if not visible in ssms.
select distinct /* I believe the sys.sys views were added in 2012 or so, still works in 2017 */
od.name caller_procedure_name
,o.name called_procedure_name
from sys.sysdepends d
inner join sys.sysobjects o on o.id = d.depid and o.type = 'P'
inner join sys.sysobjects od on od.id = d.id and od.type = 'P'
select distinct /* should work all the way back to sql 2000, still works in 2017 */
od.name caller_procedure_name
,o.name called_procedure_name
from dbo.sysdepends d
inner join dbo.sysobjects o on o.id = d.depid and o.type = 'P'
inner join dbo.sysobjects od on od.id = d.id and od.type = 'P'

List down the column names in a Temporary Table in SQL Server

I am a beginner to SQL Server Database. I have a basic question.
How to retrieve the column names of a temporary table in SQL Server?
I have tried querying the sys.objects, but the table is not listing there.
Temp tables are stored in the tempdb database so you have to query the views from the tempdb database. Something like this:
SELECT c.name
FROM tempdb.sys.objects o
INNER JOIN tempdb.sys.columns c ON c.object_id = o.object_id
WHERE o.name LIKE '#TempTableName%';
SELECT Obj.NAME
,Col.NAME
FROM tempdb.sys.objects Obj
INNER JOIN tempdb.sys.columns Col ON Obj.object_id = Col.object_id
WHERE Obj.NAME LIKE '#tmp%'
But please note that the local temporary table names will not be unique. We can have the same names from different sessions. So be careful with the query.
Here's how to get the temp table for just the current session.
SELECT c.name
FROM tempdb.sys.objects o
INNER JOIN tempdb.sys.columns c ON c.object_id = o.object_id
WHERE o.object_id = object_id('tempdb..#TempTableName)
`SELECT STUFF(
`(SELECT ',' + LTRIM(RTRIM(ISNULL(name,'')))
`FROM tempdb.sys.columns
`WHERE Object_ID = Object_ID(N'tempdb..#TEMP')
`FOR XML PATH(''), TYPE).value('.','varchar(max)'), 1, 1, '')
`AS comma_separated_list

How do I get the list of all stored procedures and their parameters starting with a certain prefix?

Is there a way to query the database and retrieve a list of all stored procedures and their parameters?
I am using SQL Server 2000.
To get information on the stored procedures:
SELECT * FROM INFORMATION_SCHEMA.ROUTINES
To find the sprocs starting with a certain prefix (e.g. "usp"):
SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME LIKE 'usp%'
To find all the parameters for a stored procedure:
SELECT * FROM INFORMATION_SCHEMA.PARAMETERS WHERE SPECIFIC_NAME='YourSprocName'
To find all the parameters for all stored procedures starting with a certain prefix:
SELECT * FROM INFORMATION_SCHEMA.PARAMETERS WHERE SPECIFIC_NAME LIKE 'usp%'
try this one :
select o.name,p.name from sys.all_parameters p inner join sys.all_objects o on p.object_id = o.object_id
where o.type = 'P'
To show a list of all procedures and their parameters, it would be in this way:
SELECT o.name AS [Procedure name], p.name as [Parameter name]
FROM sys.parameters p INNER JOIN sysobjects o ON p.object_id = o.id
WHERE o.name LIKE 'prefix%' AND o.xtype = 'P'
It works in SQL Server 2016 but I guess it works in older versions too.
the following query returns the procedures, functions and filters by a prefix.
I am not sure though, if it would work on sql server 2000.
I leave it here the reference anyway, because it is a good useful query.
SELECT SCHEMA_NAME(SCHEMA_ID) AS [Schema],
SO.name AS [ObjectName],
SO.Type_Desc AS [ObjectType (UDF/SP)],
COALESCE(P.parameter_id,0) AS [ParameterID],
COALESCE(P.name, 'NO PARAMETER') AS [ParameterName],
COALESCE(TYPE_NAME(P.user_type_id),'') AS [ParameterDataType],
COALESCE(P.max_length,0) AS [ParameterMaxBytes],
COALESCE(P.is_output,0) AS [IsOutPutParameter]
FROM sys.objects AS SO
LEFT OUTER JOIN sys.parameters AS P
ON SO.OBJECT_ID = P.OBJECT_ID
WHERE SO.OBJECT_ID IN ( SELECT OBJECT_ID
FROM sys.objects
WHERE TYPE IN ('P','FN'))
AND SO.NAME LIKE 'U%' --starting with a certain prefix
ORDER BY [Schema], SO.name, P.parameter_id
GO

How do I get a list of tables affected by a set of stored procedures?

I have a huge database with some 100 tables and some 250 stored procedures. I want to know the list of tables affected by a subset of stored procedures. For example, I have a list of 50 stored procedures, out of 250, and I want to know the list of tables that will be affected by these 50 stored procedures. Is there any easy way for doing this, other than reading all the stored procedures and finding the list of tables manually?
PS: I am using SQL Server 2000 and SQL Server 2005 clients for this.
This would be your SQL Server query:
SELECT
[NAME]
FROM
sysobjects
WHERE
xType = 'U' AND --specifies a user table object
id in
(
SELECT
sd.depid
FROM
sysobjects so,
sysdepends sd
WHERE
so.name = 'NameOfStoredProcedure' AND
sd.id = so.id
)
Hope this helps someone.
sp_depends 'StoredProcName' will return the object name and object type that the stored proc depends on.
EDIT: I like #KG's answer better. More flexible IMHO.
I'd do it this way in SQL 2005 (uncomment the "AND" line if you only want it for a particular proc):
SELECT
[Proc] = SCHEMA_NAME(p.schema_id) + '.' + p.name,
[Table] = SCHEMA_NAME(t.schema_id) + '.' + t.name,
[Column] = c.name,
d.is_selected,
d.is_updated
FROM sys.procedures p
INNER JOIN sys.sql_dependencies d
ON d.object_id = p.object_id
AND d.class IN (0,1)
INNER JOIN sys.tables t
ON t.object_id = d.referenced_major_id
INNER JOIN sys.columns c
ON c.object_id = t.object_id
AND c.column_id = d.referenced_minor_id
WHERE p.type IN ('P')
-- AND p.object_id = OBJECT_ID('MyProc')
ORDER BY
1, 2, 3
One very invasive option would be to get a duplicate database and set a trigger on every table that logs that something happened. Then run all the SP's. If you can't do lots of mods to the DB that wont work
Also, be sure to add the logging to existing triggers rather than replace them with logging if you also want tables that the SP's effect via triggers.

Resources