Remove SQL Comments from Stored Procedure - SQL Server 2008 R2 - sql-server

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.

Related

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.

List of stored procedures inside a database

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).

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.

search all stored procs for a literal or string

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.

How do I get the content of a SQL Stored Procedure in Powershell?

I'm perverted in the sense that I hate exploring code and database structures in a tree view and I'd much prefer using something like the Powershell for that. Most of the stuff I need to do in SQL is exploring, i.e. looking at what columns does a table have or what does a particular stored procedure do.
Looking at table columns is as easy as ls'ing the columns directory of a table, but how would I get the contents of a stored procedure?
If you want to run this on Sql Server 2008 then here is a Cmdlet that will help you with it.
If you are using Sql Server 2005 then here is a page with a script to help you with this.
[EDIT]
You may use the SP sp_helptext to see the contents of the required stored procedure.
They text of a sproc lives in a data dictionary table sys.sql_modules. As an aside, this Stackoverflow post has a data dictionary reverse engineering script that (amongst other things) gets the text of view definitions from this table - reverse engineering sproc definitions works much the same.
A minimal script to retrieve the progam text of a stored procedure would look like:
select m.definition
from sys.objects o
join sys.sql_modules m
on o.object_id = m.object_id
join sys.schemas s
on s.schema_id = o.schema_id
where s.name = 'foo' -- Schema name
and o.name = 'bar' -- Sproc name

Resources