0
我有一個客戶表,其中包含客戶的詳細信息。以下是字段使用外鍵將BULK INSERT插入表
CustId (PrimaryKey), Name, Date of Birth
我有另一個表,資產信息。以下字段有 -
AssetId (PrimaryKey), AssetValue, CustId (Foreign Key Reference)
我的CSV文件是這樣
Name, Date of Birth, AssetValue
,我必須把它插入到兩個表。我分割了CSV文件,一個帶有出生日期的名稱,另一個帶有AssetValue。
這裏是我做過什麼 -
/*Creation of Table*/
CREATE TABLE Customer
(
custid int identity(1,1) not null,
name nvarchar(50) not null,
dateofbirth datetime not null,
primary key (custid)
)
CREATE TABLE Asset
(
AssetId int identity(1,1) not null,
AssetDollars money not null,
primary key (AssetId),
CustId int foreign key references Customer(custid)
)
對於批量插入我所做的就是這一點。我使用兩個字段名稱和出生日期爲客戶創建了一個視圖,然後插入記錄。
這裏是我做過什麼 -
CREATE view vw_bulk_insert_customer
AS
SELECT name, dateofbirth FROM customer
BULK INSERT vw_bulk_insert_customer
FROM 'C:\Customer.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
這工作完全正常。
現在,我該如何使用CustId將它插入Asset表中(因爲它在CSV文件中不可用)。
我不允許更改CSV文件。我可以分割CSV文件,這是允許的。
我不知道該怎麼做......任何想法?