注意:我无权访问 IQ 数据库;我确实可以访问 SQLAnywhere 数据库;下面使用的函数确实出现在 IQ 参考手册中;建议的解决方案适用于我的 SQLAnywhere 数据库;最终结果是以下应该在 IQ 中起作用 ... >>交叉手指
先介绍一下背景:
1 - 表中的每条记录都有一个唯一的行 ID,可以通过 rowid(<table_name>) 函数访问
2 - number() 函数可用于为结果集生成唯一编号(以 1 开头)但是,此函数不能用于子查询或派生表(这意味着我们可以' t 在我们的 UPDATE 语句中合并派生表)
解决方案要点:
1 - 使用number()函数为每组#temp表记录生成唯一的行号(从1开始)从Table1和Table2中提取所需的行到#temp表中;还为 Table1 拉rowid()
2 - 编写我们的 UPDATE 语句来引用我们的 #temp 表,根据行号(由 number() 函数生成)连接唯一的 #temp 表行对,并通过 @987654327 连接回主表@值
设置:
create table Table1 (numseq int, Article char(1), ID int, Num1 int, Num2 int, Num3 int, Num4 int, Col07 varchar(10), Col08 int)
go
create table Table2 (numseq int, Article char(1), ID int, Num1 int, Num2 int, Num3 int, Num4 int, Col07 varchar(10), Col08 int)
go
insert Table1 values (3023,'F',242840794,4,12,1,13,'RCN03',9)
insert Table1 values (3023,'F',242840794,4,12,1,13,'RCN03',9)
insert Table1 values (3023,'F',242840794,4,12,1,13,'RCN03',9)
go
insert Table2 values (3023,'F',242840794,4,12,1,13,'RCN031',9)
insert Table2 values (3023,'F',242840794,4,12,1,13,'RCN035',9)
insert Table2 values (3023,'F',242840794,4,12,1,13,'RCN037',9)
go
numseq Article ID Num1 Num2 Num3 Num4 Col07 Col08
----------- ------- ----------- ----------- ----------- ----------- ----------- ---------- -----------
3023 F 242840794 4 12 1 13 RCN03 9
3023 F 242840794 4 12 1 13 RCN03 9
3023 F 242840794 4 12 1 13 RCN03 9
numseq Article ID Num1 Num2 Num3 Num4 Col07 Col08
----------- ------- ----------- ----------- ----------- ----------- ----------- ---------- -----------
3023 F 242840794 4 12 1 13 RCN031 9
3023 F 242840794 4 12 1 13 RCN035 9
3023 F 242840794 4 12 1 13 RCN037 9
构建/填充#temp 表:
-- we'll use WHERE clauses in our 'select/into' statements in case you
-- are planning on processing just a subset of Table1 and/or Table2; and
-- while the 'order by' clauses aren't needed for this simple example, you
-- will need to include them if you plan on updating multiple sets of
-- duplicate rows at the same time (ie, you need to make sure the number()
-- values match between sets of duplicate rows)
select rowid(Table1) as rid, number() as nbr, * into #t1 from Table1 where numseq = 3023 and Article = 'F' and ID = 242840794 and Num1 = 4 and Num2 = 12 and Num3 = 1 and Num4 = 13 and Col08 = 9
order by numseq, Article, ID, Num1, Num2, Num3, Num4, Col08
go
select number() as nbr, * into #t2 from Table2 where numseq = 3023 and Article = 'F' and ID = 242840794 and Num1 = 4 and Num2 = 12 and Num3 = 1 and Num4 = 13 and Col08 = 9
order by numseq, Article, ID, Num1, Num2, Num3, Num4, Col08
go
select * from #t1
select * from #t2
go
rid nbr numseq Article ID Num1 Num2 Num3 Num4 Col07 Col08
-------------------- ----------- ----------- ------- ----------- ----------- ----------- ----------- ----------- ---------- -----------
68222976 1 3023 F 242840794 4 12 1 13 RCN03 9
68222977 2 3023 F 242840794 4 12 1 13 RCN03 9
68222978 3 3023 F 242840794 4 12 1 13 RCN03 9
nbr numseq Article ID Num1 Num2 Num3 Num4 Col07 Col08
----------- ----------- ------- ----------- ----------- ----------- ----------- ----------- ---------- -----------
1 3023 F 242840794 4 12 1 13 RCN031 9
2 3023 F 242840794 4 12 1 13 RCN035 9
3 3023 F 242840794 4 12 1 13 RCN037 9
现在是更新:
update Table1
set Table1.Col07 = #t2.Col07
from Table1
join #t1
-- rowid() is unique and thus the only 'column' we need in the join between Table1 and itself (aka #t1)
on rowid(Table1) = #t1.rid
-- and the rest of the join columns between Table1 and #t1; optional
/*
and Table1.numseq = #t1.numseq
and Table1.Article = #t1.Article
and Table1.ID = #t1.ID
and Table1.Num1 = #t1.Num1
and Table1.Num2 = #t1.Num2
and Table1.Num3 = #t1.Num3
and Table1.Num4 = #t1.Num4
and Table1.Col08 = #t1.Col08
*/
join #t2
-- join on our number() values
on #t1.nbr = #t2.nbr
-- and the rest of the join columns between #t1 and #t2; optional but
-- provides an extra safety check to make sure we don't update records
-- with the wrong values (eg, number() is generated in wrong order)
and #t1.numseq = #t2.numseq
and #t1.Article = #t2.Article
and #t1.ID = #t2.ID
and #t1.Num1 = #t2.Num1
and #t1.Num2 = #t2.Num2
and #t1.Num3 = #t2.Num3
and #t1.Num4 = #t2.Num4
and #t1.Col08 = #t2.Col08
go
select * from Table1
go
numseq Article ID Num1 Num2 Num3 Num4 Col07 Col08
----------- ------- ----------- ----------- ----------- ----------- ----------- ---------- -----------
3023 F 242840794 4 12 1 13 RCN031 9
3023 F 242840794 4 12 1 13 RCN035 9
3023 F 242840794 4 12 1 13 RCN037 9