我会尽量保持清醒。
我有这3张表Customer
,Link
& Customer_link
。
我正在使用以下查询从customer_no
检索Customer
列,该列具有表customer_no
的{{1}}中未提供的值:
Customer_link
表SELECT c.customer_no
FROM CUSTOMER c
LEFT JOIN Customer_link cl
ON c.customer_no = cl.customer_no
WHERE cl.customer_no IS NULL
包含以下列:
Customer_link
我尝试做的是,使用上面的查询从ID (generated automatically using a sequence)
Customer_no (linked to Customer table)
Link_no (linked to the Link table)
Maker_id
表中获取尚未添加到表customer_no
的{{1}}(这是一个约束,因为Customer
在表中不能有两次相同的Customer_link
,这是唯一的。Customer_link
也是如此。&从customer_no
表中获取link_no
的类似查询。
然后使用link_no
&来自各自结果的Link
&然后添加到表link_no
(已经有一个函数,我将需要使用我在结果中得到的值调用)。
我需要在这里使用一个循环,这样我可以在将每个表中的值添加到表customer_no
后更新结果,这样我就不会在尝试添加相同的{{1}时出错表格中两次或Customer_link
。
使用光标是我在互联网上找到的一种方式。但这对我来说并不是很清楚。
所以我在这里尝试做的就是从Customer_link
&获取未使用的customer_no的结果。来自customer_no
&的Link_no在行1中插入值是表link_no
&中的相应列。循环以更新结果&再次获取row1中的值,将它们作为参数添加到我调用的函数
答案 0 :(得分:0)
我认为它不需要循环只需遵循这一步
1)将不在Customer_link中的所有customer_no插入 #temp 表
select c.customer_no into #temp from CUSTOMER c LEFT JOIN Customer_link cl on c.customer_no = cl.customer_no where cl.customer_no is null
2)将Customer_no插入Customer_link
insert into Customer_link(customer_no)
select customer_no from #temp
答案 1 :(得分:0)
不需要循环。只需使用select作为插入源:
insert into customer_link (customer_no)
select c.customer_no
from customer c
left Customer_link cl ON c.customer_no = cl.customer_no
where cl.customer_no is null;
或者使用有时更快的NOT EXISTS查询:
insert into customer_link (customer_no)
select c.customer_no
from customer c
where not exists (select *
from customer_link cl
where cl.customer_no = c.customer_no);
两个语句只会为customer_no
列选择(并插入)唯一值(即没有重复项),假设customer_no
是customer
表中的主键。
答案 2 :(得分:0)
customer_no
中的第一个值是多少?我想这样的顺序。
此查询将仅按指定的顺序(rownum =1
)添加返回的第一个值(ORDER BY c.customer_no
)和customer_no,如果您不需要订购的值,只需删除ORDER BY
子句。
INSERT INTO customer_link (customer_no)
SELECT customer_no
FROM (
SELECT c.customer_no
FROM CUSTOMER c
LEFT JOIN Customer_link cl
ON c.customer_no = cl.customer_no
WHERE cl.customer_no IS NULL
ORDER BY c.customer_no
) sub
WHERE rownum = 1
您可以在SQL Fiddle找到我准备的演示。
编辑:在您的问题发生更改后,我认为您正在寻找类似的内容:
INSERT INTO customer_link (customer_no, link_no)
SELECT customer_no,
your_function(customer_no)
FROM (
SELECT c.customer_no
FROM CUSTOMER c
LEFT JOIN Customer_link cl
ON c.customer_no = cl.customer_no
WHERE cl.customer_no IS NULL
) sub
这将获取Customer_link中尚未存在的所有customer_no
,并将它们放入该表中,并使用您的函数返回的link_no。
答案 3 :(得分:0)
我终于找到了一种方法来使用游标。我将在这里提出解决方案,以便其他人可能需要帮助。
我创建了一个类型为Customer.Customer_no
的变量cust
cust Customer.Customer_no%type;
然后我创建了一个Cursor,以便能够将未使用的客户编号的值添加到
Cursor cr_cust
IS
select c.customer_no into cust from CUSTOMER c LEFT JOIN Customer_link cl on c.customer_no = cl.customer_no where cl.customer_no is null;
现在我用循环让这些都以这种方式工作
begin
FOR i IN cr_cust LOOP --to loop through the rows
cust := i.customer_no; --to add the value of corresponding row to the variable --cust
IF j <= 5 THEN --this for getting the values from link table & assigning them to --variable so that the variables can be passed to the required function
{code for assigning values to variables & the function calling go here}
j:=j+1; --to get the next value in link table
END IF;
IF j > 5 THEN --exit if 5 customers have been added to the Customer_link table & --there is no need for more links to be fetched from link table
EXIT;
END IF;
END LOOP;
end