简体   繁体   English

Sql 服务器,将数据插入到两个表中,一个在另一个之前填充

[英]Sql server, insert data into two tables, populate one before the other

My first post here as I am starting in a new job where my old sql skills already are on the test - I am not advanced user from before either.我在这里的第一篇文章是在我开始一份新工作时,我的旧 sql 技能已经在测试中 - 我也不是以前的高级用户。 I believe there might be some answers here already that might be answering my question, but I am still a bit unfamiliar - both with the forum and more advanced sql syntaxes.我相信这里可能已经有一些答案可以回答我的问题,但我仍然有点不熟悉——无论是论坛还是更高级的 sql 语法。 Some answers are pretty old as well.有些答案也很老了。 And - please excuse me for any grammatical errors.并且 - 请原谅我的任何语法错误。

Anyways, if anyone might be able to help.无论如何,如果有人可以提供帮助。

I will be receive huge lists (.csv or similar) with data input.我将收到带有数据输入的大量列表(.csv 或类似的)。 The datalists will contain fields for type customerdata (name, address etc) and fields for type real estate/property data (street address, buildingIDs etc).数据列表将包含客户数据类型的字段(名称、地址等)和房地产/财产数据类型的字段(街道地址、建筑物 ID 等)。

The customerdata and propertydata needs to be put into two separate tables. customerdata 和 propertydata 需要放入两个单独的表中。

My problem is like this: - The two tables are depended on eachother as in the propertydata table needs to be populated with data first, which will generate a unique GUID - which again will be used when populating the customerdata table - connecting the customer to the correct property/real estate.我的问题是这样的: - 这两个表相互依赖,因为在 propertydata 表中需要先填充数据,这将生成一个唯一的 GUID - 在填充 customerdata 表时再次使用它 - 将客户连接到正确的财产/不动产。

The first thing that came to mind is to populate all data into a temporary table.想到的第一件事是将所有数据填充到临时表中。

But I am not quite sure how to loop through each row so I ensure that propertytable is populated first, then the customertable using the GUID.但我不太确定如何遍历每一行,所以我确保先填充属性表,然后使用 GUID 填充客户表。

  1. Get data that involves propertydata and populate the property table获取涉及propertydata的数据并填充属性表
  2. Get the unique GUID generated in property table获取属性表中生成的唯一 GUID
  3. Get data that involves customerdata and populate the customer table - with correct GUID获取涉及 customerdata 的数据并填充客户表 - 使用正确的 GUID
  4. Loop through rest of the set until there are no more rows with data.循环遍历集合的 rest,直到不再有数据行。

I have seen some things like transactions, cursors, output etc that seems to be within my scope, but not sure which would be best way to solve my challenge?我已经看到了一些东西,比如事务、游标、output 等似乎在我的 scope 中,但不确定哪种方法是解决我的挑战的最佳方法? Am I near something when thinking like that?当我这样想的时候,我在附近吗?

EDIT:编辑:
These are example fields that will come as one row in.xlsx/.csv format.这些示例字段将以 .xlsx/.csv 格式作为一行出现。 Number of rows in such list will vary from time to time.此类列表中的行数会不时变化。

  1. Property ID物业编号
  2. Property address物业地址
  3. Property building ID (only one)物业大楼ID(只有一个)
  4. Property established date物业成立日期
  5. ...Misc other property related fields ...其他其他属性相关领域
  6. Customer ID客户ID
  7. Customer Name顾客姓名
  8. Customer Address客户地址
  9. Postal code邮政编码
  10. ...Misc other customer related fields ...其他与客户相关的领域

Fields 1 through 5 will need to populate a property table first.字段 1 到 5 需要先填充属性表。 When each row in property table is populated it will generate unique GUID.填充属性表中的每一行时,它将生成唯一的 GUID。 Then fields 6 through 10 will be used to populate a customer table, but also need to be populated with the corresponding unique GUID created above in the property table.然后字段 6 到 10 将用于填充客户表,但还需要使用上面在属性表中创建的相应唯一 GUID 进行填充。

Property table:属性表:

  1. Property ID物业编号
  2. Property address物业地址
  3. Property building ID (only one)物业大楼ID(只有一个)
  4. Property established date物业成立日期
  5. ...Misc other property related fields ...其他其他属性相关领域
  6. UNIQUE PROPERTY GUID (created when populating each new row in table)唯一的属性 GUID (在填充表中的每个新行时创建)

Customer table:客户表:

  1. UNIQUE PROPERTY GUID独特的物业指南
  2. Customer ID客户ID
  3. Customer Name顾客姓名
  4. Customer Address客户地址
  5. Postal code邮政编码
  6. ...Misc other customer related fields ...其他与客户相关的领域

I suggest you use a staging table.我建议你使用临时表。

  1. Load all data into a staging / temporary table and assign GUID numbers to each row.将所有数据加载到暂存/临时表中,并为每一行分配 GUID 编号。
  2. Copy property details from staging table to property table taking GUID from the staging table将临时表中的属性详细信息复制到从临时表中获取 GUID 的属性表
  3. Copy customer details from staging table to customer table taking GUID from the staging table.将客户详细信息从临时表复制到客户表,并从临时表中获取 GUID。
  4. Delete data in staging table.删除临时表中的数据。

A quick example:一个简单的例子:

INSERT INTO PropertyDetails( GUID, PropertyID, PropertyAddress, ... )
SELECT GUID, PropertyID, PropertyAddress, ...
FROM StagingTable

INSERT INTO CustomertDetails( GUID, CustomerID, CustomerName, ... )
SELECT GUID, PropertyID, PropertyAddress, ...
FROM StagingTable

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

相关问题 SQL Server 2008 - 将一个表中新创建的记录的ID插入另外两个表中 - SQL Server 2008 - Insert ID from newly created record in one table, into two other tables SQL:将数据从一个表插入到彼此相关的两个表中 - SQL : insert data from one table into two tables related to each other INSERT SELECT sql 语句 - 如何在一个语句中从另外两个表中获取数据 - INSERT SELECT sql statement - how to fetch data from two other tables in one statement 当一个表具有大量数据而另一个表具有少量数据时,在SQL Server中联接两个表 - Joining two tables in SQL Server when one table has huge data and other has few data SQL Server:从两个表中选择并插入其中一个 - SQL Server: Select from two tables and insert into one 如何用一个查询SQL在两个表中插入数据 - how to insert data in two tables with one query sql SQL查询:合并两个表以在另一个表中插入数据 - SQL query: Combining two tables to insert data in another one 如何在SQL Server中同时将数据插入两个表? - How can I INSERT data into two tables simultaneously in SQL Server? 无法在SQL Server中同时将数据插入到两个相关表中 - Unable to Insert data into two related tables simultaneously in sql server 同时在两个关系表中插入数据SQL Server - Insert data in two relational tables simultaneously SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM