SQL Server Bulk Insert with FOREIGN KEY parameter (not existant in txt file, ERDs included)

戏子无情 提交于 2019-12-13 04:11:59

问题


Okay so I have a table ERD designed like so... for regular bulk inserts


(source: iforce.co.nz)

And a tab delimited \t text file with information about each customer (consists of about 100,000+ records).

# columnA columnB columnC
data_pointA data_pointB data_pointC

And a stored procedure that currently does its intended job fine.

CREATE PROCEDURE import_customer_from_txt_para @filelocation varchar(100)
AS BEGIN

    TRUNCATE TABLE dbo.[customer_stg]
    DECLARE @sql nvarchar(4000) = '
    BULK INSERT customer_stg
    FROM ''' + @filelocation + '''
    WITH
    (
        FIRSTROW=14,
        FIELDTERMINATOR=''\t'',
        ROWTERMINATOR=''\n''
    )';
    print @sql;
    exec(@sql);

END

But my question is about the relationship between customer_table and customer_stg is it possible to include a customer_id within the customer_stg bulk insert? with something like so? ( I'm not sure how to apply the foreign key parameter @customer_sk to the bulk insert ).

CREATE PROCEDURE import_customer_from_txt_para @filelocation varchar(100), @customer_sk int
AS BEGIN

    TRUNCATE TABLE dbo.[customer_stg]
    DECLARE @sql nvarchar(4000) = '
    BULK INSERT customer_stg
    FROM ''' + @filelocation + '''
    WITH
    (
        FIRSTROW=14,
        FIELDTERMINATOR=''\t'',
        ROWTERMINATOR=''\n''
    )';
    print @sql;
    exec(@sql);

END

Preferably after each bulk-insert I'd wish to be able to relate the data between the two tables.


(source: iforce.co.nz)


回答1:


Bulk inserts will either insert NULL or the default value for unspecified column (based on the KEEPNULLS argument), which of course will not work for your situation assuming you have (or will create) a constraint. I assume this is the case because otherwise you could just update your table directly after you run the insert.

I see two ways around this:
- If you have the ability, you can just macro-edit the text file before you run the bulk insert. Since I'm assuming that that isn't in the question...
- First of all, you will need to add your FK column to your _stg table if it's not already there. Then, in your stored procedure, create a temp table with the three columns specified in the input file:

CREATE TABLE dbo.#Temp_STG
(
    columnA,
    columnB,
    columnC
)

Then, batch insert into that table. Then you can insert from the temp table to the main _stg table, but add a column:

INSERT dbo.Customer_STG
SELECT
    T.columnA,
    T.columnB,
    T.columnC,
    [your customer key]
FROM dbo.#Temp_STG AS T

Make sure you drop the temp table when you're done.

As a side note, do you need to use dynamic SQL for this task? It's generally best to avoid unless absolutely necessary.

I suppose another option would be setting the default value for the column to whatever you want, and turning KEEPNULLS off. But, I would definitely NOT recommend doing this when you can just use the solution described above.

See more: http://msdn.microsoft.com/en-us/library/ms188365.aspx



来源:https://stackoverflow.com/questions/14469688/sql-server-bulk-insert-with-foreign-key-parameter-not-existant-in-txt-file-erd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!