search all stored procs for a literal or string - sql-server

Is there a way to search for a string or literal in all stored procedures for a particular database in Microsoft SQL Server 2008?

Grab yourself a copy of the free Red-Gate SQL Search tool and start enjoying searching in SQL Server! :-)
It's a great and very useful tool, and YES! it's totally, absolutely FREE for any kind of use.

select quotename(s.name)+'.'+quotename(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
and o.type = 'P' /* stored procuedure */
inner join sys.schemas s
on o.schema_id = s.schema_id
where m.definition like '%YourSearchText%'

I think this will work for you
SELECT ROUTINE_NAME, ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE '%lq_Campaign%'
AND ROUTINE_TYPE='PROCEDURE'
Here is the a link I found on it.

Related

Is it possible Script out sql query to .sql file using ssis?

IS is possible to script out each stored procedure to .sql file using ssis ?
I have a query as
SELECT definition
FROM sys.sql_modules procs
INNER JOIN sys.objects o ON procs.object_id = o.object_id
INNER JOIN sys.schemas s ON o.schema_id = s.schema_id
WHERE o.[type] = 'P'
I am looking each row as each .sql file on folder
I have Image example as
Note : I dont have access to exec xp_cmdshell
Thanks,

Remove SQL Comments from Stored Procedure - SQL Server 2008 R2

We would like to remove developer comments from stored procedures for some specific reason.
Is there any mechanism available within SQL Server 2008 R2?
It may be possible to script alter statements based on meta data and strip the comments in the process.
As a starting point take a look at sys.sql_modules
SELECT OBJECT_SCHEMA_NAME(m.object_id) AS [SchemaName],
OBJECT_NAME(m.object_id) AS [ObjectName], o.type, m.definition
FROM sys.sql_modules m
inner join sys.all_objects o on o.object_id = m.object_id
where o.is_ms_shipped = 0
order by OBJECT_NAME(m.object_id) ;
But I don't know why you don't just manually script the sp's to a text file and strip it before running.

Find List Of Dynamic stored procedure in SQL Server 2008

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%')

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.

Find usage of a function in SQL server

Is there a way to find a usage of a function in SQL server 2008?
Use in code:
SELECT * FROM sys.sql_modules WHERE definition LIKE '%MyFunc%'
UNION
SELECT * FROM sys.computed_columns WHERE definition LIKE '%MyFunc%'
UNION
SELECT * FROM sys.check_constraints WHERE definition LIKE '%MyFunc%'
UNION
SELECT * FROM sys.default_constraints WHERE definition LIKE '%MyFunc%'
I think I've covered all bases...
You can't use sys.comments because the type is nvarchar(4000)
The answer for SQL Server 2012:
SELECT DISTINCT sc.id,
so.name
FROM syscomments sc
INNER JOIN sysobjects so
ON so.id = sc.id
WHERE sc.TEXT LIKE '%functionname%'
ORDER BY 2
With SQL2012, use sp_depends in your database will list all usage inside.
ex.
EXEC sp_depends 'FunctionNameToSearch'
EXEC sp_depends 'TableNameToSearch'
EXEC sp_depends 'StoredProcNameToSearch'
Assuming that by "usage" you mean "usage statistics", then maybe SQL Server Profiler is useful for you.

Resources