[英]How to insert one row of a table into two rows of another table
I have a record in a table Table1 and i want to split that one record into two rows and insert into another table Table2. 我在表Table1中有一条记录,我想将该记录分为两行并插入到另一表Table2中。
Table1 表格1
ID Date User value
1 29/05/18 XXX X_ID||X_value||22||xx
2 29/05/18 YYY Y_ID||Y_value||33|yy
and I want table1 values to be inserted into table2 as 我希望将table1值插入到table2中,作为
Table2 表2
P_ID ID Date User Field Value
1 1 29/05/18 XXX X_ID 22
2 1 29/05/18 XXX X_Value XX
3 2 29/05/18 YYY Y_ID 33
4 3 29/05/18 YYY Y_Value YY
Value from table1 is field in table2 and nothing could be hardcoded as I would be having many records in table table1. 来自table1的值是table2中的字段,没有任何东西可以硬编码,因为我在表table1中会有很多记录。 EDIT: and what if value in table1 has more values like X_ID||X_VALUE||Y_ID||Y_Value||22||xx||33||yy.
编辑:如果table1中的值具有更多值,例如X_ID || X_VALUE || Y_ID || Y_Value || 22 || xx || 33 || yy,该怎么办? How do I make this query dynamic so that irrespective of output it separates record in different rows of table2
我如何使此查询动态化,以便无论输出如何将表2中不同行中的记录分隔开
You can use regexp_substr()
: 您可以使用
regexp_substr()
:
select id, date, user, regexp_substr(val, '[^|]+', 1, 1) as field, regexp_substr(val, '[^|]+', 1, 3) as value
from table1
union all
select id, date, user, regexp_substr(val, '[^|]+', 1, 2) as field, regexp_substr(val, '[^|]+', 1, 4) as value
from table1;
You can just use insert
to put this into another table. 您可以只使用
insert
将其放入另一个表中。 This assumes that p_id
is assigned automatically. 假定
p_id
是自动分配的。 If you want to assign it yourself, you can use row_number()
: 如果要自己分配,可以使用
row_number()
:
select row_number() over (partition by id order by which) as p_id,
id, date, user, field, value
from ((select id, date, user, regexp_substr(val, '[^|]+', 1, 1) as field, regexp_substr(val, '[^|]+', 1, 3) as value, 1 as which
from table1
) union all
(select id, date, user, regexp_substr(val, '[^|]+', 1, 2) as field, regexp_substr(val, '[^|]+', 1, 4) as value, 2 as which
from table1
)
) t
I have tried to solve the problem by using cursors. 我试图通过使用游标来解决问题。
DECLARE @ID INT 声明@ID INT
DECLARE @NAME VARCHAR(50) 十进制@NAME VARCHAR(50)
DECLARE @DATE DATE 宣告@DATE DATE
DECLARE @VALUE VARCHAR(500) 十进制@VALUE VARCHAR(500)
DECLARE @List TABLE (item NVARCHAR(MAX)) 声明@List TABLE(项目NVARCHAR(MAX))
DECLARE @SELECTCURSOR CURSOR 声明@SELECTCURSOR游标
SET @SELECTCURSOR = CURSOR FOR SELECT * FROM tempTable1 SET @SELECTCURSOR =选择的光标* FROM tempTable1
OPEN @SELECTCURSOR 打开@SELECTCURSOR
FETCH NEXT FROM @SELECTCURSOR INTO @ID,@DATE,@NAME, @VALUE 从@SELECTCURSOR进入@ ID,@ DATE,@ NAME,@ VALUE
WHILE @@FETCH_STATUS = 0 @@ FETCH_STATUS = 0时
BEGIN 开始
SET @VALUE = Replace(@VALUE, '||', '.'); SET @VALUE =替换(@VALUE,'||','。');
INSERT INTO tempTable2(ID,[User],[Date],Field,Value) VALUES(@ID,@NAME,@DATE,ParseName(@VALUE,4),PARSENAME(@VALUE,2)) 插入tempTable2(ID,[User],[Date],Field,Value)VALUES(@ ID,@ NAME,@ DATE,ParseName(@ VALUE,4),PARSENAME(@ VALUE,2))
INSERT INTO tempTable2(ID,[User],[Date],Field,Value) VALUES(@ID,@NAME,@DATE,ParseName(@VALUE,3),PARSENAME(@VALUE,1)) 插入tempTable2(ID,[User],[Date],Field,Value)VALUES(@ ID,@ NAME,@ DATE,ParseName(@ VALUE,3),PARSENAME(@ VALUE,1))
FETCH NEXT FROM @SELECTCURSOR INTO @ID,@DATE,@NAME, @VALUE 从@SELECTCURSOR进入@ ID,@ DATE,@ NAME,@ VALUE
END 结束
CLOSE @SELECTCURSOR 关闭@SELECTCURSOR
DEALLOCATE @SELECTCURSOR 取消分配@SELECTCURSOR
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.