List of stored procedures inside a database - sql-server

I suffer the correct calling data to one of my databases (Parameter of stored procedure not found)
For further debugging it might be helpful to extract a list of available stored procedures inside my database and the params to call each procedure.
How to get this information from the database using Delphi code?
DELPHI XE 2
Database SQL Server 2008
ADO

The following query will list all user defined stored procs (including their parameters and parameter types) in your default database:
SELECT
sp.name,
p.name AS Parameter,
t.name AS [Type]
FROM
sys.procedures sp
LEFT JOIN sys.parameters p
ON sp.object_id = p.object_id
LEFT JOIN sys.types t
ON p.system_type_id = t.system_type_id
WHERE
is_ms_shipped = 0
ORDER BY
sp.name
Put that into an ADOQuery object and set it to Active. (Updated answer with LEFT JOINS so that it includes SPs without parameters).

Related

How to find all the functions and stored procedures called in a stored procedure in sql server

I need to know all the functions and stored procedures called in a stored procedure in sql server.
This shows all objects referenced by the given procedure:
select isnull(re.referenced_schema_name, 'dbo') referenced_schema_name,
referenced_entity_name as referenced_object_name,
o.type_desc referenced_object_type
from sys.dm_sql_referenced_entities('dbo.YourProcedureName', 'OBJECT') re
inner join sys.objects o on (o.object_id = re.referenced_id)
or (o.name = re.referenced_entity_name and schema_name(o.schema_id) = isnull(referenced_schema_name, 'dbo'))
where re.referenced_database_name is null
and o.type_desc in (
'SQL_TABLE_VALUED_FUNCTION',
'SQL_INLINE_TABLE_VALUED_FUNCTION',
'SQL_SCALAR_FUNCTION',
'SQL_STORED_PROCEDURE',
'SQL_TRIGGER',
'CLR_SCALAR_FUNCTION',
'CLR_STORED_PROCEDURE',
'CLR_TABLE_VALUED_FUNCTION',
'CLR_TRIGGER'
)
but it shows only procedure in the same database and same server and it does not show if a procedure or function is called inside a dynamic sql query.

Is there any query to know which parameter is used in the stored procedure at the time of execution in SQL Server?

The below query is used for getting last execution time and date but can I get the parameter used in it while execution in SQL Server?
SELECT
o.name,
s.last_execution_time,
s.type_desc,
s.execution_count
FROM
sys.dm_exec_procedure_stats s
INNER JOIN
sys.objects o ON s.object_id = o.object_id
WHERE
DB_NAME(s.database_ID) = 'XYZ' --Database Name
AND o.name LIKE ('%ABC%') --Object Name
Thanks for #HoneyBadger's comment, and this will help for you:
The article shows how to find compiled parameter values for SQL Server cached plans.

Logging of nested stored procedures in SQL Server

How do you individually log the information of nested stored procedures that are executed by the parent stored procedure?
Example:
CREATE PROCEDURE dbo.usp_ParentProcedure
AS
BEGIN
EXEC dbo.usp_ChildProcedure1
EXEC dbo.usp_ChildProcedure2
EXEC dbo.usp_ChildProcedure3
END
GO
Ideally I would like to log the transaction time, I/0, CPU usage of each nested stored procedure individually whilst only amending the parent procedure.
The reason being is because the nested procedures are frequently added and removed from the parent procedure by other users, and we all know getting everyone to follow a creation protocol is seriously hard work.
Aaron Bertrand has shared his solution for logging a single stored procedure here
http://www.mssqltips.com/sqlservertip/2003/simple-process-to-track-and-log-sql-server-stored-procedure-use/
I would appreciate if anyone can help me expand this to cover my scenario above.
Thanks
Neal
After spending some time having a look around the dm tables, I have found running this query after executing the parent stored procedure returns some useful information about the child stored procedures.
SELECT
DB_NAME (s.database_id),
s.[object_id],
p.name,
s.sql_handle,
s.plan_handle,
s.last_worker_time,
s.last_elapsed_time,
s.last_physical_reads,
s.last_logical_writes,
s.last_logical_reads,
p2.name,
p2.[object_id]
FROM sys.dm_exec_procedure_stats s
INNER JOIN sys.procedures p
ON s.[object_id] = p.[object_id]
LEFT JOIN sys.procedures p2 --join to identify parent procedure from results
ON p2.[object_id] = (
select top 1 s.[object_id]
from sys.dm_exec_procedure_stats s
inner join sys.procedures p
ON s.[object_id] = p.[object_id]
order by s.total_worker_time desc
)
WHERE s.[object_id] <> p2.[object_id]
AND s.[object_id] <> ##PROCID

Find the parameter names of a stored procedure

I am using Microsoft SQL Server 2008. I have a stored procedure. Is there a simple query I can execute that will give me the parameter names?
I have found this Link but it is not for Microsoft SQL Server 2008.
To get names only you can use this query:
SELECT name
FROM sys.parameters
WHERE object_id = OBJECT_ID('YourProcedureName')
To get more detailed info (name, type and length of parameter):
SELECT p.name AS ParameterName, t.name AS ParameterType, p.max_length AS ParameterLength
FROM sys.parameters AS p
JOIN sys.types AS t ON t.user_type_id = p.user_type_id
WHERE object_id = OBJECT_ID('YourProcedureName')
On top of what Marek stated, you can also retrieve them programatically using the DeriveParameters method in the .NET library: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommandbuilder.deriveparameters.aspx
Check out my blog on database files and objects. http://craftydba.com/?p=2901
I have a stored procedure called SP_STORE_PRIMES in my sample [MATH] database.
One way is to use the sys.parameters table. This can be optionally joined to types. Below is joined to sys.objects.
-- Parameters to SP & FN
select o.name, p.* from sys.parameters p join sys.objects o
on p.object_id = o.object_id where is_ms_shipped = 0
go
A older system stored procedure is sp_sproc_columns.
-- Older system stored proc - show all parameters to one
sp_sproc_columns #procedure_name = 'SP_STORE_PRIMES'
go
Both ways will get you where you want to go.

How Can I Extract parameters from Stored Procedure in SQL Server 2005?

Given a Stored procedure, I want to extract the parameter from it.
How can I do this in .net?
You can run the following SQL query in SQL Server 2005. You can of course call the same query using the SqlCommand class.
SELECT
p.name,
p.object_id,
pm.parameter_id,
pm.name AS parameter_name,
pm.system_type_id AS parameter_system_type_id,
pm.max_length AS parameter_max_length,
t.name AS type_name
FROM sys.procedures p
JOIN sys.parameters pm ON p.object_id = pm.object_id
JOIN sys.types t ON pm.system_type_id = t.system_type_id
WHERE p.name = 'sprocName'
Of course, the procedures, parameters and types system views contain other interesting stored procedure and parameter information as well. This query is just a selection.
You should use the SqlCommandBuilder.DeriveParameter, which is Shared (VB.NET) or Static (C#) to which you pass the SqlCommand: DeriveParameter on MSDN.
You just have to create a SqlCommand, setting the name of the stored procedure, call this method and look at the SqlCommand.Parameters property.

Resources