【问题标题】:pass Database Mail parameters to stored prcedure using php and sql server 2008使用 php 和 sql server 2008 将数据库邮件参数传递给存储过程
【发布时间】:2011-11-12 07:54:35
【问题描述】:

我想为我的应用程序使用数据库邮件,这意味着我需要将参数传递给存储过程sp_send_dbmail 我有以下代码仅用于测试目的。但是,我想知道如何使用 ssql server 2008 和 php 将参数传递给存储过程。 仅供参考,我使用的是 Microsoft 的 sqlsrv 驱动程序

<?php require_once ('../Connection/connmail.php')?> 
<?php
 $sql = "{CALL sp_send_dbmail[[@profile_name='gmailsmtp']]}";//my stored procedure

    $stmt = sqlsrv_query($conn,$sql)or die(print_r(sqlsrv_errors(),true));    


 ?> 

上面的代码产生错误

Array ( [0] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 102 [code] => 102 [2] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Incorrect syntax near '{'. [message] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Incorrect syntax near '{'. ) [1] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 105 [code] => 105 [2] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Unclosed quotation mark after the character string '[@profile_name='gmailsmtp']}'. [message] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Unclosed quotation mark after the character string '[@profile_name='gmailsmtp']}'. ) ) 

任何帮助将不胜感激

【问题讨论】:

    标签: php sql-server-2008 stored-procedures parameters


    【解决方案1】:

    我看到这个问题有一段时间没有得到解决,所以这里是解决方案。在 msdb 系统数据库中让你的用户成为databasemailuserrole

    $callmailproc = "EXEC msdb.dbo.sp_send_dbmail @profile_name = ?, @recipients=?, @subject=?, @body=?";
    $profilename = 'DatabaseMail';
    $recipients = 'recipient@domain.com';
    $subject='Test Message';
    $body = 'This is the body';
    $params = array( 
                     array($profilename, SQLSRV_PARAM_IN),
                     array($recipients, SQLSRV_PARAM_IN),
                     array($subject, SQLSRV_PARAM_IN),
                     array($body, SQLSRV_PARAM_IN),
                     );
    
    /* Execute the query. */
    $stmt3 = sqlsrv_query( $conn, $callmailproc, $params);
    if( $stmt3 === false )
    {
         echo "Error in executing statement 3.\n";
         die( print_r( sqlsrv_errors(), true));
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      • 2013-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多