SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Search for a string in objects like sp,functions..
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

learntsql
Constraint Violating Yak Guru

India
460 Posts

Posted - 08/10/2012 :  05:34:47  Show Profile  Reply with Quote
Hi All,

I need to search for the string in all databse objects like procedures,Functions,views,etc...
In all these i want to search for parameters and variables if this string is used.
TIA

Transact Charlie
Flowing Fount of Yak Knowledge

United Kingdom
3442 Posts

Posted - 08/10/2012 :  05:44:47  Show Profile  Visit Transact Charlie's Homepage  Reply with Quote
Script them out and then use the tool of your choice. Agent Ransack is good (if you like regular expressions) but a simple windows find would do it.:

http://download.cnet.com/Agent-Ransack/3000-2072_4-10043846.html



Transact Charlie
Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
http://nosqlsolution.blogspot.co.uk/
Go to Top of Page

lionofdezert
Aged Yak Warrior

Pakistan
864 Posts

Posted - 08/10/2012 :  05:51:46  Show Profile  Visit lionofdezert's Homepage  Send lionofdezert a Yahoo! Message  Reply with Quote
SELECT sys.schemas.name + '.' + OBJECT_NAME(id) AS ObjectName
FROM syscomments
INNER JOIN sys.objects ON syscomments.id = sys.objects.object_id
INNER JOIN sys.schemas ON sys.objects.schema_id = sys.schemas.schema_id
WHERE [text] LIKE N'%MySearchTextHere%'
AND ( OBJECTPROPERTY(id, 'IsProcedure') = 1
OR OBJECTPROPERTY(id, 'IsScalarFunction') = 1
OR OBJECTPROPERTY(id, 'IsView') = 1
)
GROUP BY sys.schemas.name + '.' + OBJECT_NAME(id)

--------------------------
http://connectsql.blogspot.com/
Go to Top of Page

learntsql
Constraint Violating Yak Guru

India
460 Posts

Posted - 08/10/2012 :  08:48:00  Show Profile  Reply with Quote
ThankQ guys.
Go to Top of Page

Srinika
Flowing Fount of Yak Knowledge

Sri Lanka
1378 Posts

Posted - 08/10/2012 :  10:09:16  Show Profile  Reply with Quote
If u need to do it frequently, use a tool (free) like SQL Search from RedGate.

Srinika
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47188 Posts

Posted - 08/10/2012 :  10:52:35  Show Profile  Reply with Quote
quote:
Originally posted by learntsql

Hi All,

I need to search for the string in all databse objects like procedures,Functions,views,etc...
In all these i want to search for parameters and variables if this string is used.
TIA




select object_name(object_id)
from sys.sql_modules
where definition like '% <the string value> %'

?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Transact Charlie
Flowing Fount of Yak Knowledge

United Kingdom
3442 Posts

Posted - 08/10/2012 :  11:42:04  Show Profile  Visit Transact Charlie's Homepage  Reply with Quote
quote:

select object_name(object_id)
from sys.sql_modules
where definition like '% <the string value> %'


sys.sql_modules truncates text. Sure that's come up on this forum before.

Transact Charlie
Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
http://nosqlsolution.blogspot.co.uk/
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47188 Posts

Posted - 08/10/2012 :  11:54:38  Show Profile  Reply with Quote
quote:
Originally posted by Transact Charlie

quote:

select object_name(object_id)
from sys.sql_modules
where definition like '% <the string value> %'


sys.sql_modules truncates text. Sure that's come up on this forum before.

Transact Charlie
Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
http://nosqlsolution.blogspot.co.uk/


nope that was sys.syscomments as its text field was limited to nvarchar(4000)

sys.sql_modules has definition field as nvarchar(max) so i dont think it does any truncation

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Transact Charlie
Flowing Fount of Yak Knowledge

United Kingdom
3442 Posts

Posted - 08/13/2012 :  04:14:16  Show Profile  Visit Transact Charlie's Homepage  Reply with Quote
ah cool. thanks for the correction. You are absolutely correct: And this is troubling......

SELECT MAX(LEN([definition])) FROM sys.sql_modules

Results:

1185118

WTF?

Time to go hunting.........

Transact Charlie
Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
http://nosqlsolution.blogspot.co.uk/
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 08/13/2012 :  07:48:41  Show Profile  Reply with Quote
Just means you have been working very hard and writing some very long stored procedures

I thought I had a few long stored procs (1,500 lines of code) and my max length is only 47,031.

I know they give the same results, but I always end up using object_defintion(object_id) from sys.objects to do this type of search.
SELECT MAX(LEN(OBJECT_DEFINITION(OBJECT_ID))) FROM sys.objects
sys.sql_modules is built on top of sys.objects and the definition column in sys.sql_modules uses that anyway.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47188 Posts

Posted - 08/13/2012 :  10:05:20  Show Profile  Reply with Quote
quote:
Originally posted by sunitabeck

Just means you have been working very hard and writing some very long stored procedures

I thought I had a few long stored procs (1,500 lines of code) and my max length is only 47,031.

I know they give the same results, but I always end up using object_defintion(object_id) from sys.objects to do this type of search.
SELECT MAX(LEN(OBJECT_DEFINITION(OBJECT_ID))) FROM sys.objects
sys.sql_modules is built on top of sys.objects and the definition column in sys.sql_modules uses that anyway.


yep...thats true
i somehow seems to prefer sys.sql_modules always

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Transact Charlie
Flowing Fount of Yak Knowledge

United Kingdom
3442 Posts

Posted - 08/13/2012 :  15:50:51  Show Profile  Visit Transact Charlie's Homepage  Reply with Quote
quote:
Originally posted by sunitabeck

Just means you have been working very hard and writing some very long stored procedures


Kind of you.... but if I'd written one stored proc something with over 1 MILLION characters of code I'd have to take myself out back and shoot myself.

For my own good.

Turns out that this was an auto generated sp written back in the dawn of time to migrate servers. No idea why it was done this way.... It's been sitting in our svn repo and being deployed to every environment even though it's never, ever used....



Transact Charlie
Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
http://nosqlsolution.blogspot.co.uk/
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000