Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.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 当第二个表引用第一个表中的主键时,如何将记录插入两个表中?_Sql_Sql Server_Insert - Fatal编程技术网

Sql 当第二个表引用第一个表中的主键时,如何将记录插入两个表中?

Sql 当第二个表引用第一个表中的主键时,如何将记录插入两个表中?,sql,sql-server,insert,Sql,Sql Server,Insert,我正在尝试将记录插入到两个表中。第二个表有一列引用第一个表的主键。如何使用一个脚本插入到这两个脚本中?我应该使用scope_identity和a逐个插入,还是有更有效的方法 第一个表列: OrgUnitAttributeID (PK) OrgUnitID AttributeTypeID AttributeValue CreatedByUserID CreatedDateUTC UpdatedByUserID UpdatedDateUTC IsActive DisplayMessageAttri

我正在尝试将记录插入到两个表中。第二个表有一列引用第一个表的主键。如何使用一个脚本插入到这两个脚本中?我应该使用scope_identity和a逐个插入,还是有更有效的方法

第一个表列:

OrgUnitAttributeID (PK)
OrgUnitID
AttributeTypeID
AttributeValue
CreatedByUserID
CreatedDateUTC
UpdatedByUserID
UpdatedDateUTC
IsActive
DisplayMessageAttributeID
DisplayMessageTypeID
OrgUnitAttributeID (PK that's created in the first table)
CreatedDateUTC
第二个表列:

OrgUnitAttributeID (PK)
OrgUnitID
AttributeTypeID
AttributeValue
CreatedByUserID
CreatedDateUTC
UpdatedByUserID
UpdatedDateUTC
IsActive
DisplayMessageAttributeID
DisplayMessageTypeID
OrgUnitAttributeID (PK that's created in the first table)
CreatedDateUTC

这是一般模式。您必须调整名称和列列表。此外,还需要向回调临时表添加第二列,以将新生成的ID链接到第二个表上的唯一键字段。祝你好运。

你试试怎么样?它有用吗?这是你的答案!使用OUTPUT values子句而不是scope_identity()