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.
Related
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.
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.
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).
I want to find list of stored procedures having dynamic queries in them.
Is there any way to that?
Here's a start. You can execute a dynamic SQL using EXEC and sp_executesql, so you want to search for stored procedures containing those commands:
SELECT
SP_NAME = o.name
FROM sys.sql_modules m
INNER JOIN sys.objects o
ON m.object_id = o.object_id
WHERE
m.definition LIKE '%EXEC%'
OR m.definition LIKE '%SP_EXECUTESQL%'
As suggested by Coder of Code, instead of sys.objects, you could JOIN it to sys.procedures.
Here are other method's according to my google search:
Using sys.procedures:
SELECT
SP_NAME = name
FROM sys.procedures
WHERE
OBJECT_DEFINITION(object_id) LIKE '%EXEC%'
OR OBJECT_DEFINITION(object_id)LIKE '%SP_EXECUTESQL%'
Using sys.sql_modules:
SELECT
SP_NAME = OBJECT_NAME(OBJECT_ID)
FROM sys.sql_modules
WHERE
OBJECTPROPERTY(OBJECT_ID, 'IsProcedure') = 1
AND (
definition LIKE '%EXEC%'
OR definition LIKE '%SP_EXECUTESQL%'
)
may be this will help
select * from sys.syscomments where text like '%exec%' AND 'SOME OTHER CONDITIONS'
Try this.
select b.name as Sp_Names from sys.syscomments a,sys.procedures b
where a.id=b.object_id and (a.text like '%exec%' or a.text like '%sp_executesql%')
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.