Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert this script to a stored procedure

Status
Not open for further replies.

divinyl

IS-IT--Management
Joined
Nov 2, 2001
Messages
163
Location
GB
hi all

i'm a SP newbie - can someone help me convert this script to a stored procedure? i've tried but i'm failing miserably. The stored procedure will exist in a user database - not the master - so i tried putting master.dbo before sysdatabases but it says it doesn't know what this item is...HELP

-------------
declare @IDENT INT, @sql varchar(1000), @DBNAME VARCHAR(200)

select @IDENT=min(DBID) from SYSDATABASES WHERE [DBID] > 4 AND NAME NOT IN

('pubs','northwind')
while @IDENT is not null
begin
SELECT @DBNAME = NAME FROM SYSDATABASES WHERE DBID = @IDENT
/*Change disk location here as required*/
SELECT @SQL = 'BACKUP DATABASE '+@DBNAME+' TO DISK =

''\\server1\upgrade_dumps\'+@DBNAME+'.BAK''WITH INIT'
PRINT @SQL
EXEC (@SQL)
select @IDENT=min(DBID) from SYSDATABASES WHERE [DBID] > 4 and DBID>@IDENT

AND NAME NOT IN ('pubs','clientmatter')
end

----------

I know you cannot use the "Use" statement in a stored proc -what's a way round this ??
 
Try this..

CREATE PROC sp_backup
as
set nocount On
declare @IDENT INT
declare @sql varchar(1000)
declare @DBNAME VARCHAR(200)

select @IDENT=min(DBID) from SYSDATABASES
WHERE [DBID] > 4
AND NAME NOT IN

('pubs','northwind')
while @IDENT is not null
begin
SELECT @DBNAME = NAME FROM SYSDATABASES WHERE DBID = @IDENT
/*Change disk location here as required*/
SELECT @SQL = 'BACKUP DATABASE '+@DBNAME+' TO DISK =

''\\server1\upgrade_dumps\'+@DBNAME+'.BAK''WITH INIT'
PRINT @SQL
EXEC (@SQL)
select @IDENT=min(DBID) from SYSDATABASES
WHERE [DBID] > 4
and DBID>@IDENT

AND NAME NOT IN ('pubs','clientmatter')
end
set nocount OFF

Dr.Sql
Good Luck.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top