3

I have 100's of databases for which I need to do an Alter to a procedure, they all have the same procedure. How can I add a cursor that will allow me to do this alter?.

DECLARE @databasename varchar(100)
DECLARE @Command nvarchar(200)

DECLARE database_cursor CURSOR FOR
SELECT name
FROM MASTER.sys.sysdatabases

OPEN database_cursor

FETCH NEXT FROM database_cursor INTO @databasename

WHILE @@FETCH_STATUS = 0
BEGIN
     SELECT @Command = 'ALTER PROCEDURE ''' + @databasename + '''.[dbo].[DeleteAccountUpgrade]
               @Id INT
            AS
                DELETE FROM AccountUpgrades WHERE Id = @Id'
     EXEC sp_executesql @Command

     FETCH NEXT FROM database_cursor INTO @databasename
END

CLOSE database_cursor
DEALLOCATE database_cursor 

It says that i need to declare @Id but i dont understand why, I just want to run that alter on each database, i am not sending any data to execute the procedure, i just want to modify the existing procedure on all databases.

4

2 に答える 2

1

EXECではなく、単独で使用する必要がありますsp_executesql

EXEC sp_executesql @Command

次のように変更する必要があります。

EXEC(@Command)

このsp_executesqlプロシージャは、変数を使用してパラメータ化を行います。これがエラーの原因です。https://stackoverflow.com/a/16308447/1822514も参照してください。

于 2013-05-06T15:58:46.593 に答える