简体   繁体   English

使用 php 批量加载到 azure

[英]bulk load to azure using php

I am trying to load CSV to an Azure webserver database using PHP.我正在尝试使用 PHP 将 CSV 加载到 Azure 网络服务器数据库。 The CSV's are 2000 lines each.每个 CSV 文件有 2000 行。 I have it working but when I run the file on the web page the database is only updated with 479 entries and I am not sure why.我让它工作了,但是当我在 web 页面上运行文件时,数据库只更新了 479 个条目,我不知道为什么。 The webpage takes a long time to process then it will show the page saying its completed.该网页需要很长时间才能处理,然后它会显示页面说它已完成。 Here's what I have for the insert statement.这是我对插入语句的内容。

try {
    $connection = new PDO("sqlsrv:server = tcp:".$SrvName.",1433; Database = ".$DBName, $SQL_USER, $SQL_PASS);
    if (($handle = fopen("personal.csv", "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $num = count($data);
            $sql = "INSERT INTO personal (clientId, lastname, ssn)
                    VALUES ($data[0], $data[1],$data[2])";
            $statement = $connection->prepare($sql);
            $statement->execute();
        }
        fclose($handle);
    }

} catch(PDOException $error) {
    echo $sql . "<br>" . $error->getMessage();
}

Any help is greatly apreciated!非常感谢任何帮助!

I think we have two solutions:我认为我们有两个解决方案:

Solution1解决方案1
How to: Send Data as a Stream : The Microsoft Drivers for PHP for SQL Server takes advantage of PHP streams for sending large objects to the server. How to: Send Data as a Stream : The Microsoft Drivers for PHP for SQL Server takes advantage of PHP streams for sending large objects to the server.

Solution2解决方案2
Design a stored procedure, pass the file name as a parameter.设计一个存储过程,将文件名作为参数传递。
Example code:示例代码:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[spBulkInsert]  @FileName varchar(255)
AS
declare @sql varchar(max)
BEGIN Try
set @sql =  
'INSERT into  ext.customer_Staging with (TABLOCK) (customer, age, gender)
   SELECT * FROM OPENROWSET(
   BULK '+ @FileName + ',
   DATA_SOURCE = ''MyDataSource'',
   FORMAT = ''CSV'',
   FORMATFILE_DATA_SOURCE = ''MyDataSource''
   ) AS DataFile;
   go'

 exec(@sql)
END TRY 

BEGIN CATCH
    PRINT ERROR_MESSAGE()
END CATCH

GO

Then use php to call this sqlserver stored procedure instance.然后使用php调用这个sqlserver存储过程实例。


But don't BULK INSERT into your real tables directly.但不要直接将BULK INSERT 插入到您的真实表中。

  • I would always insert into a staging table ext.customer_Staging (without the IDENTITY column) from the CSV file我总是会从 CSV 文件中插入临时表 ext.customer_Staging(没有 IDENTITY 列)
  • possibly edit / clean up / manipulate your imported data可能编辑/清理/操作您导入的数据
  • and then copy the data across to the real table with a T-SQL statement like:然后使用以下 T-SQL 语句将数据复制到真实表中:
INSERT into  ext.customer_Staging with (TABLOCK) (customer, age, gender)
   SELECT * FROM OPENROWSET(
   BULK 'bs140513_032310-demo.csv',
   DATA_SOURCE = 'MyDataSource',
   FORMAT = 'CSV',
   FORMATFILE_DATA_SOURCE = 'MyDataSource'
   ) AS DataFile;
   go

INSERT INTO ext.customer(Name, Address) 
   SELECT customer, age, gender
   FROM ext.customer_Staging

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

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