Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jeroena
Yak Posting Veteran
76 Posts |
Posted - 2002-10-18 : 09:59:39
|
| HiI created a script for dropping all the user defined stored procedures in my database.DECLARE @csp_name VARCHAR(255)DECLARE c_procs CURSORFORSELECT nameFROM sysobjectsWHERE name LIKE 'csp_%'OPEN c_procsFETCH NEXT FROM c_procsINTO @csp_nameWHILE @@FETCH_STATUS = 0BEGIN EXEC ('DROP PROCEDURE ' + @csp_name) PRINT ('Procedure "' + @csp_name + '" verwijderd.') FETCH NEXT FROM c_procs INTO @csp_nameENDCLOSE c_procsDEALLOCATE c_procsGOAll my procedures start with "csp_". The question: Does anyone have such a dropscript for indexes? My indexes are names "<tablename>_ix001", so this works:select * from sysindexes where name like '%_ix%'Thnx!Spike. |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2002-10-18 : 10:48:09
|
| http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=20452Jay White{0} |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2002-10-18 : 11:01:39
|
| Two additional things:A. Procedures can be created as part of, and dropped as, a group. Since you are prefixing the procedures you want to group together, you might want to look at using procedure grouping if you intend to drop and recreate them on a regular basis. Books Online details this.B. You could also:DECLARE @sp varchar(8000)SET @sp=''SELECT @sp=@sp + name + ',' FROM sysobjects WHERE name LIKE 'csp[_]%'EXECUTE ('DROP PROCEDURE ' + Left(@sp, Len(@sp)-1))This code grabs all of the procedure names and builds a string of them, which is included in the DROP PROCEDURE call to drop all of them. The same technique can be used to drop all of the indexes as well:DECLARE @idx varchar(8000)SET @idx=''SELECT @idx=@idx + name + ',' FROM sysindexes WHERE name LIKE '%[_]ix%'EXECUTE ('DROP INDEX ' + Left(@idx, Len(@idx)-1))Also, you need to enclose a literal underscore in square brackets like this [_] in order for the LIKE operator to treat it as a literal, and not a pattern match character.Edited by - robvolk on 10/18/2002 11:18:38 |
 |
|
|
jeroena
Yak Posting Veteran
76 Posts |
Posted - 2002-10-21 : 05:04:35
|
| Hmm... seems more effecient...doesn't seem to work here (SQL Server 2000) though.Incorrect syntax near the keyword 'Left'.And the DROP INDEX statement needs a tablename as well.I should be able to get it to work though. Thnx.Jeroen. |
 |
|
|
jeroena
Yak Posting Veteran
76 Posts |
Posted - 2002-10-21 : 05:09:12
|
| This seems to work:DECLARE @idx_name VARCHAR(255), @tab_name VARCHAR(255)DECLARE c_indexes CURSORFORSELECT sob.name AS TableName, six.name AS IndexNameFROM sysobjects sob, sysindexes sixWHERE six.name LIKE sob.name + '%' and six.name LIKE '%_ix%'OPEN c_indexesFETCH NEXT FROM c_indexesINTO @tab_name, @idx_nameWHILE @@FETCH_STATUS = 0BEGIN EXEC ('DROP INDEX ' + @tab_name + '.' + @idx_name) PRINT ('Index "' + @idx_name + '" verwijderd.') FETCH NEXT FROM c_indexes INTO @tab_name, @idx_nameENDCLOSE c_indexesDEALLOCATE c_indexesGO |
 |
|
|
|
|
|
|
|