This tip shows How to execute Command in All Database in Server using Stored Procedure .
The below code snippet we can use to create Stored Procedure
- Create proc CustomExec(@strs nvarchar(max)
- )
- as begin
- DECLARE @ID varchar(50)
- DECLARE c CURSOR READ_ONLY FAST_FORWARD FOR
- SELECT name
- FROM master..sysdatabases where name like 'Test%'
- -- Open the cursor
- OPEN c
- FETCH NEXT FROM c INTO @id
- WHILE (@@FETCH_STATUS = 0)
- BEGIN
- DECLARE @command varchar(max)
- SELECT @command = 'USE '+ @id +''
- SET @command=@command+'
- Go'
- Set @command =@command +@strs
- SET @command=@command+'
- GO
- '
- print @command
- FETCH NEXT FROM c INTO @id
- END
- -- Close and deallocate the cursor
- CLOSE c
- DEALLOCATE c
- end
No comments :
Post a Comment