Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2005 使用外键大容量插入到表中_Sql Server 2005 - Fatal编程技术网

Sql server 2005 使用外键大容量插入到表中

Sql server 2005 使用外键大容量插入到表中,sql-server-2005,Sql Server 2005,我有一张表,上面有客户的详细信息。以下是字段 CustId (PrimaryKey), Name, Date of Birth 我还有一张表,资产信息。以下字段位于此处: AssetId (PrimaryKey), AssetValue, CustId (Foreign Key Reference) 我的CSV文件就是这样 Name, Date of Birth, AssetValue 我必须把它插入两个表中。我分割了CSV文件,一个带有出生日期的名称,另一个只有AssetValue

我有一张表,上面有客户的详细信息。以下是字段

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'
)
对于批量插入,我所做的就是这样。我为客户创建了一个视图,其中包含两个字段名称和出生日期,然后插入记录

这就是我所做的-

/*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将其插入资产表中(因为它在CSV文件中不可用)

我不允许更改CSV文件。我可以分割CSV文件,这是允许的


我不知道该怎么做……有什么想法吗?

您可以通过外键禁用novalidate。导入所有数据后,可以通过外键启用novalidate。主键也可以这样做。novalidate选项的好处是,您以前的数据不会根据该约束的规则进行检查,但之后的数据会因此进行检查。如果您需要,我可以粘贴整个语法来执行相同的操作,一个选项是按原样将数据导入临时表,然后将此表中的数据复制到最终表中。这本质上是规范化数据库的后一部分(跳过规范化表的设计和定义)


这并不能回答这个问题,即当只有自然密钥可用时使用代理密钥。