2014-05-22 45 views
0

嗨我写了一个存储过程来检索有关空间不足的服务器磁盘驱动器的信息。现在我只是试图编写另一个存储过程发送电子邮件通知来通知客户端服务器磁盘空间不足。以下是我有:SQL存储过程中的语法错误发送电子邮件?

--Creating alert notification 
CREATE PROC dbo.sp_drivespacelow1_alerts 
     @from varchar(100), 
     @to varchar(200), 
     @subject varchar(100), 
     @threshold int -- number of MB under which to launch an alert 
AS 

     SET NOCOUNT ON 

     DECLARE @list nvarchar(2000) = ''; 

     WITH core AS ( 
       SELECT DISTINCT 
         s.volume_mount_point [Drive], 
         CAST(s.available_bytes/1048576 as decimal(12,2))  [AvailableMBs] 
       FROM 
        sys.master_files f 
         CROSS APPLY sys.dm_os_volume_stats(f.database_id, f.[file_id]) s 
    ) 

     SELECT @list = @list + ' ' + Drive + ', ' 
     FROM core 
     WHERE AvailableMBs < @threshold 

     IF LEN(@list) > 3 BEGIN 
       DECLARE @msg varchar(500) = 'Low Disk Space Notification. The following  drives are currently reporting less than ' 
       + CAST(@threshold as varchar(12)) + ' MB free: ' + @list 

       EXEC msdb.dbo.sp_send_dbmail @profile_name = 'xxxxx', 
       @recipients = @to, 
       @subject = @subject, 
       @body = @msg 

     END 

     RETURN 0 
GO --Check done every 5 mins 

--To Run: 
EXEC master.dbo.sp_drivespace_alerts 
     @threshold = 5120 --5 GB 
     @from = '[email protected]' 
     @subject = 'Low diskspace alert -ServerName Here' 
     @to = '[email protected]' 

下面是我用什么来检查磁盘空间:

--Returns informations on drives with less than 5 GBs of disk space 
CREATE TABLE #drives (
     drive char, 
     [free] int 
) 

INSERT INTO #drives 
EXEC master..xp_fixeddrives 

SELECT drive, [free] 
FROM #drives 
WHERE [free] < 5 * 102 

drop table #drives --must get rid of temporary tables in order to run again 

眼下存储过程检查磁盘空间是很好,但一到发送电子邮件通知不断给我回这个错误: 消息2714,级别16,状态3,过程sp_drivespacelow_alerts,行37 数据库中已有一个名为'sp_drivespacelow_alerts'的对象。 消息102,级别15,状态1,行6 附近有语法错误@from',“@主题”

如果我注释掉这些代码两位并运行此错误: 消息102,15级,状态1,行7 “@to”附近的语法不正确。

请帮助谢谢!

+0

重新标记'SQL-server'为'mysql'是obvisously不是使用 –

回答

-1

如果您运行的是:

--To Run: 
EXEC master.dbo.sp_drivespace_alerts 
     @threshold = 5120 --5 GB 
     @from = '[email protected]' 
     @subject = 'Low diskspace alert -ServerName Here' 
     @to = '[email protected]' 

我想你想这样做,是这样的:

--To Run: 
CALL master.dbo.sp_drivespace_alerts (
     5120, --5 GB 
     '[email protected]', 
     'Low diskspace alert -ServerName Here', 
     '[email protected]'); 
+0

嗨,谢谢你我试过你的建议,但我现在得到这个错误:Msg 102,Level 15,State 1,Line 3 'master'附近语法不正确。 – user3657381

+0

我的错误。我把括号放在了错误的地方。现在试试。 – Tom

+0

最初的'exec'几乎是正确的。只是缺少逗号。它不应该在SQL Server中有括号或“调用”(调用只能在ODBC转义序列中理解) –