简体   繁体   English

PDO sqlsrv不会在最后运行执行查询

[英]PDO sqlsrv don't run execute query to the end

i'm trying to use PDO and while loop statement in sql server query to insert to database but the while loop not run to the end although it work fine with simple query, i only use XAMPP to run server. 我正在尝试在SQL Server查询中使用PDO和while循环语句插入数据库,但是while循环没有运行到最后,尽管它可以与简单查询一起很好地工作,但我仅使用XAMPP来运行服务器。

here my script to insert all days from 2017-01-01 to 2018-01-01 (yyyy-MM-dd): 这是我的脚本,用于插入2017年1月1日至2018年1月1日之间的所有日期(yyyy-MM-dd):

try{
    $dsn = "sqlsrv:Server=MyServer,1433;Database=MyDB;";   
    $db = new PDO($dsn, "sa", "MyPassword"); 
    $db->setAttribute (PDO::SQLSRV_ATTR_QUERY_TIMEOUT, 60); 

    $query = "DELETE FROM TEST

              declare @TempDate as date
              declare @FDate as date = Cast('2017-01-01' as date)
              declare @TDate as date = Cast('2018-01-01' as date)
              declare @DayInMonthList as Table(AllDayDate varchar(8))

              BEGIN
                   SET @TempDate = @FDate
                   WHILE(@TempDate < @TDate)
                   BEGIN
                        INSERT INTO TEST(FromDate, ToDate, TempDate) VALUES(@FDate, @TDate, @TempDate)
                        SET @TempDate =DateAdd(day, 1, @TempDate) 
                   END
              END";

        $cmd = $this->db->prepare($query); 
        if(!$cmd->execute()){
            throw new PDOException ($cmd->errorInfo());
        } 
}catch(PDOException $e) {
    die("Error connecting to SQL Server: " . $e->getMessage());
} 

This script insert about 120 rows without any error, what is wrong with my code ? 该脚本插入了约120行,没有任何错误,我的代码有什么问题? please help me! 请帮我!

Try this 尝试这个

try{
    $dsn = "sqlsrv:Server=MyServer,1433;Database=MyDB;";   
    $db = new PDO($dsn, "sa", "MyPassword"); 
    $db->setAttribute (PDO::SQLSRV_ATTR_QUERY_TIMEOUT, 200); 
    $db->setAttribute ( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    $db->beginTransaction();
    $query = "DELETE FROM TEST

              declare @TempDate as date
              declare @FDate as date = Cast('2017-01-01' as date)
              declare @TDate as date = Cast('2018-01-01' as date)
              declare @DayInMonthList as Table(AllDayDate varchar(8))

              BEGIN
                   SET @TempDate = @FDate
                   WHILE(@TempDate < @TDate)
                   BEGIN
                        SET NOCOUNT ON;
                        INSERT INTO TEST(FromDate, ToDate, TempDate) VALUES(@FDate, @TDate, @TempDate)
                        SET @TempDate =DateAdd(day, 1, @TempDate) 
                   END
              END";

        $cmd = $db->prepare($query); 
        if(!$cmd->execute()){
            throw new PDOException ($cmd->errorInfo());
        }
        $db->commit();
}catch(PDOException $e) {
    die("Error connecting to SQL Server: " . $e->getMessage());
}

Always do beginTransaction and commit while doing PDO execution as shown below. 如下所示,在执行PDO时始终执行beginTransaction和commit。

$db->beginTransaction();
$db->commit();

Also you have to add the below code before the insert statement to stop break from loop while insertion. 另外,您还必须在insert语句之前添加以下代码,以在插入时停止循环中断。

SET NOCOUNT ON;

I hope this will help you. 我希望这能帮到您。 If you have any problems or doubts let me know. 如果您有任何问题或疑问,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM