简体   繁体   中英

Retrieving the first value of the column to a variable in pl sql

I'll try to be as clear as possible.

I have these 3 tables Customer , Link & Customer_link .

I am using the following query to retrieve the customer_no column from Customer which has values that are not available in the customer_no of the table 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

The table Customer_link has the following columns:

ID (generated automatically using a sequence)
Customer_no (linked to Customer table)
Link_no (linked to the Link table)
Maker_id 

What I'm trying to do is, use the above query to get the customer_no from the Customer table that are not added to the table Customer_link yet (this is a constraint since Customer_link cannot have the same customer_no twice in the table which is unique. Same goes for link_no as well) & a similar query to get the link_no from the Link table.

Then use a link_no & a customer_no from the respective results & add then to the table Customer_link (there is already a function for this which I will need to call with the values that I get in the result).

I need to use a loop here so that I can update the results after adding a value from each table to the table Customer_link which will so that I do not get an error trying to add the same customer_no or link_no twice to the table.

Using a cursor is one way I found on the internet. But it's not very clear to me.

So what I'm trying to do exactly here is get the result for unused customer_no from Customer & Link_no from Link & insert the values in row1 is the respective columns in the table Customer_link & loop over to update the results & get the values in row1 again to add them as parameter to the function that I call

i think it doesn't require loop just follow this step

1) Insert all customer_no that not in Customer_link into #temp table

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)insert Customer_no into Customer_link

insert into Customer_link(customer_no)
select customer_no from #temp

There is no need for a loop. Just use the select as a source for the insert:

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;

Or alternatively use a NOT EXISTS query which is sometimes faster:

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);

Both statements will only select (and insert) unique values (ie no duplicates) for the customer_no column assuming customer_no is the primary key in the customer table.

Which is the first value from customer_no ? I guess in such kind of order.

This query will add only the first value returned ( rownum =1 ) with customer_no in the specified order ( ORDER BY c.customer_no ), if you don't need the values ordered just remove the ORDER BY clause.

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

You can find a demo I have prepared here in SQL Fiddle .

EDIT: After the changes in your question, I think you are looking for something like:

  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

This takes all the customer_no not already present in Customer_link and put them in that table with the link_no returned by your function.

I finally found a way to get this working with the use of a cursor. I will be putting up the solution here so that someone else may need help with this.

I created a variable cust that was of the type Customer.Customer_no

cust Customer.Customer_no%type;

I then created a Cursor to be able to add the values of unused customer number to

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;

Now i used a loop to get these all working this way

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM