简体   繁体   English

使用大型Excel文件插入/更新SQL

[英]Inserting/Updating SQL using large Excel File

I have a situation where I have to insert/update a lot of information in a table for our ERP. 我有一种情况,我必须在表格中为我们的ERP插入/更新很多信息。 I first have to check the table to see if the information exists, if not, insert, if so, update. 我首先必须检查表以查看信息是否存在,如果不存在,请插入,如果存在,请更新。

I have sample code below. 我下面有示例代码。

    IF NOT EXISTS(SELECT * FROM EXT00101 WHERE PT_Window_ID='ITEM_SHIP_MAINT' and PT_UD_Key='18 RND PA' and PT_UD_Number=5)
    BEGIN
          INSERT INTO EXT00101 VALUES('ITEM_SHIP_MAINT', '18 RND PA', 5, '70')
    End

IF EXISTS(SELECT * FROM EXT00101 WHERE PT_Window_ID='ITEM_SHIP_MAINT' and PT_UD_Key='18 RND PA' and PT_UD_Number=5)
    BEGIN
        UPDATE EXT00101
        SET STRGA255='70'
        WHERE PT_Window_ID='ITEM_SHIP_MAINT' and PT_UD_Key='18 RND PA' and PT_UD_Number=5;
    END

I used a mail merge against an excel file which has 23,000+ lines. 我对具有23,000多个行的excel文件使用了邮件合并。 This causes my SQL statement to be 260,000+ lines. 这导致我的SQL语句超过260,000行。 There has to be a more efficient way to complete this. 必须有一种更有效的方法来完成此操作。

If not, I will have to break this code up considerably. 如果没有,我将不得不大量分解此代码。

Microsoft SQL Server 2005 Microsoft SQL Server 2005

To get you on the right tracks using SSIS, the first step would be to use an Execute SQL task to create a staging table: 为了使用SSIS使您步入正轨,第一步是使用Execute SQL任务创建临时表:

IF (OBJECT_ID(N'dbo.tmpEXT00101Staging') IS NOT NULL 
    DROP TABLE dbo.tmpEXT00101Staging;

CREATE TABLE dbo.tmpEXT00101Staging
(       PT_Window_ID    VARCHAR(50) NOT NULL,
        PT_UD_Key       VARCHAR(50) NOT NULL,
        PT_UD_Number    INT NOT NULL,
        STRGA255        VARCHAR(50) NOT NULL
);

Then use a Data Flow task to import your data to this table (flat file or excel source, and your destination would be OLE DB Destination. You may need to set "Delay Validation" on the Data Flow task to false and/or "Validate External Meta Data" to false on the destination because the destination is created at run time). 然后使用“数据流”任务将数据导入到此表(平面文件或excel源,目标将为OLE DB目标。您可能需要在“数据流”任务上将“延迟验证”设置为false和/或“验证”外部元数据”在目标上为false,因为目标是在运行时创建的。 You could also just keep your staging table there permanently, and instead just use TRUNCATE TABLE dbo.tmpEXT00101Staging to clear it at the start of each execution. 您也可以只将登台表永久保留在那里,而只需在每次执行开始时使用TRUNCATE TABLE dbo.tmpEXT00101Staging清除它即可。

Finally use this staging table to update your main table (and drop the staging table to clean up if you don't want to keep it permanently) 最后,使用此登台表更新主表(如果您不想永久保留它,请删除该登台表以进行清理)

BEGIN TRAN;

-- UPDATE ROWS THAT EXIST
UPDATE  t
SET     STRGA255 = st.STRGA255
FROM    dbo.EXT00101 t
        INNER JOIN dbo.tmpEXT00101Staging st
            ON st.PT_Window_ID = T.PT_Window_ID
            AND st.PT_UD_Key = t.PT_UD_Key
            AND st.PT_UD_Number = t.PT_UD_Number;

-- INSERT ROWS THAT DO NOT EXIST
INSERT EXT00101 (PT_Window_ID, PT_UD_Key, PT_UD_Number, STRGA255)
SELECT PT_Window_ID, PT_UD_Key, PT_UD_Number, STRGA255
FROM    dbo.tmpEXT00101Staging st
WHERE   NOT EXISTS
        (   SELECT  1
            FROM    dbo.EXT00101 t
            WHERE   st.PT_Window_ID = T.PT_Window_ID
            AND     st.PT_UD_Key = t.PT_UD_Key
            AND     st.PT_UD_Number = t.PT_UD_Number
        );

COMMIT TRAN;

-- CLEAN UP AND DROP STAGING TABLE (OPTIONAL)
IF (OBJECT_ID(N'dbo.tmpEXT00101Staging') IS NOT NULL 
DROP TABLE dbo.tmpEXT00101Staging;

There is potential to meet a race condition here, so you should ensure that you have constraints in to stop any integrity violation of this by concurrent threads trying to insert the same record. 这里有可能满足竞争条件,因此您应确保自己有约束条件,以阻止试图插入同一记录的并发线程对此进行的任何完整性侵犯。

For the sake of completeness, the preferable option to do the UPSERT is MERGE (if you upgrade from 2005 at anypoint): 为了完整起见,进行UPSERT的首选选项是MERGE(如果您随时从2005升级):

MERGE dbo.EXT00101 WITH (HOLDLOCK) AS t
USING dbo.tmpEXT00101Staging AS st
    ON st.PT_Window_ID = T.PT_Window_ID
    AND st.PT_UD_Key = t.PT_UD_Key
    AND st.PT_UD_Number = t.PT_UD_Number
WHEN MATCHED THEN 
    UPDATE  
    SET STRGA255 = st.STRGA255
WHEN NOT MATCHED THEN 
    INSERT (PT_Window_ID, PT_UD_Key, PT_UD_Number, STRGA255)
    VALUES (st.PT_Window_ID, st.PT_UD_Key, st.PT_UD_Number, st.STRGA255);

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

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