—-Option 1
SELECT DISTINCT so.name
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE LOWER(sc.TEXT) LIKE ‘%name%’
—-Option 2
SELECT DISTINCT o.name, o.xtype
FROM syscomments sc
INNER JOIN sysobjects o ON sc.id=o.id
WHERE LOWER(sc.TEXT) LIKE ‘%name%’
Tag: SQL Server
SQL: Setting DateTime Value to Zero
–using DATEDIFF can set the time portion of your DateTime value to zeros –Handy!
DECLARE @Day DATETIME
SET @Day = DATEADD(DAY, -7, DATEDIFF(Day, 0, GETDATE()))
Shhh! Secret Procedures!
I don’t have to tell you that the folks at Microsoft can be quite sneaky so, in case you don’t already know, there are a number of unofficially documented SQL stored procedures that can make your life not only better, but often cursor-free.
The one that is most helpful to me is entitled “sp_msforeachtable.” Using this stored procedure you can accomplish the same tasks on each table.
You can Google it for more information but a short and simple article can be found at DatabaseJournal.com:
sp_MSforeachtable @command1=”print ‘?’ dbcc checktable (‘?’)”
For more handy secrets, do a search on “undocument sql server stored procedures” or http://lmgtfy.com/?q=undocumented+sql+server+stored+procedures .
Disabling and Enabling Table Constraints
Back in the ages of pre-Sql Server 2005, we had to drop and re-create constraints to modify certain data. I guess it was habit when I found myself beginning to do this today. Fortunately, I remembered the newer-improved way (of course, I DID have to look it up). Instead of dropping and re-creating constraint,s they can simply be disabled and enabled:
ALTER TABLE testTable NOCHECK CONSTRAINT FK_testTable_Task
–do your data deletes, etc here
ALTER TABLE testTable CHECK CONSTRAINT FK_testTable_Task
Voila, no risky dropping involved. Of course, be careful when manipulating your data in such a manner. This should not be normal practice.
Searching the Text of Stored Procedures in SQL Server
–How To Search The Text of Stored Procedures in SQL Server:
SELECT OBJECT_NAME(id)
FROM syscomments
WHERE [text] LIKE ‘%dog%’
AND OBJECTPROPERTY(id, ‘IsProcedure’) = 1
GROUP BY OBJECT_NAME(id)