Results 1 to 5 of 5

Thread: Scheduling a Stored Procedure

  1. #1
    Join Date
    Jan 2009
    Posts
    22

    Question Scheduling a Stored Procedure

    How do I schedule a stored procedure as a job?
    I have a created a Maint plan but it won't run correctly... here is the stored procedure I am trying to schedule to run daily to alert me when a database file is running out of space

    CREATE PROCEDURE [dbo].[DBsizeNotification]

    as

    declare @dbname as varchar(100)
    declare @checksize as varchar(1000)
    DECLARE CRS_db CURSOR LOCAL FAST_FORWARD
    FOR
    SELECT name
    FROM sys.databases

    OPEN CRS_db
    FETCH NEXT FROM CRS_db INTO @dbname
    CREATE TABLE DBSize (DB sysname, SizeMB int)
    WHILE @@FETCH_STATUS = 0
    BEGIN

    Set @checksize = 'use ' + @dbname +
    ' insert DBSize select db_name(), round(sum(([size]/128.0)),2) from sysfiles'


    exec (@checksize)

    FETCH NEXT FROM CRS_db INTO @dbname
    END
    EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'SQLDBA',
    @recipients = 'sqldba@ibsa.com',
    @query = 'SELECT * FROM DBSize WHERE SizeMB > 40' ,
    @subject = '**************************Large Database Alert',
    @attach_query_result_as_file = 1 ;

    CLOSE CRS_db
    DEALLOCATE CRS_db
    DROP TABLE DBSize

  2. #2
    Join Date
    Sep 2002
    Posts
    5,938
    Create sql job in ssms and put sp name and parameters in job step, need choose t-sql as command type and specify correct db in the step.

  3. #3
    Join Date
    Jan 2009
    Posts
    22
    so what you are saying is that I need to create a new job for each database that I want monitored?
    I currently have the job with 'master' as the database selection...Is that only going to check 'master'? I'm confused.

  4. #4
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    No, since you have the cursor populating the table with sizes, it will do what you want. You need to pick one database in the job step window, it could be master or anything else as long as you have fully qualified the stored procedure name

    exec dbname.dbo.procname

  5. #5
    Join Date
    Sep 2002
    Posts
    5,938
    I believe @checksize should be 'use ' + @dbname +
    ' insert master..DBSize select db_name(), round(sum(([size]/128.0)),2) from sysfiles' if you run it under master db.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •