I need to search for certain databases with a particular prefix. Once these databases have been located, I need to check to see if a certain schema exists and if it does not exist - take the database offline. I have been trying various suggestions I found on Google but nothing works. I have one error that I cannot get past. The error is 'XYZ' is not a recognized option. If I separate out the code it runs fine but when I add sp_MSforeachdb, the error returns.
EXEC sp_msforeachdb 'IF ''?'' LIKE ''abc_%''
BEGIN
IF (NOT EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'XYZ'
BEGIN
ALTER DATABASE [abc_xxxxxxxxx] SET OFFLINE WITH
ROLLBACK IMMEDIATE
END'
This code works fine:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'XYZ'
Here's a method that doesn't rely on the undocumented, unsupported and buggy system procedure
sp_msforeachdb
, and also doesn't rely on similarly unreliableINFORMATION_SCHEMA
views.Your actual error is because you have string delimiters inside a string. You can't do this:
Error:
You need to do this:
However this gets convoluted quite quickly when you're nesting commands inside a command you're sending to an undocumented, unsupported and buggy system procedure. I know I'm repeating myself here. I hope it's clear that you should not be using
sp_msforeachdb
if you want reliable results.