【问题标题】:Retrieving the first value of the column to a variable in pl sql将列的第一个值检索到pl sql中的变量
【发布时间】:2014-12-16 07:34:52
【问题描述】:

我会尽量说清楚。

我有这 3 张桌子 CustomerLinkCustomer_link

我正在使用以下查询从Customer 中检索customer_no 列,该列的值在表Customer_linkcustomer_no 中不可用:

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 表中获取尚未添加到表Customer_link 中的customer_no(这是一个约束,因为Customer_link 不能有相同的customer_no 两次在表中是唯一的。link_no 也是如此)和类似的查询从Link 表中获取link_no

然后从各自的结果中使用link_nocustomer_no 然后添加到表Customer_link (已经有一个函数,我需要用我在结果中得到的值调用它)。

我需要在这里使用一个循环,以便在将每个表中的值添加到表 Customer_link 后更新结果,这样我在尝试添加相同的 customer_no 或 @ 时不会出错987654344@两次上桌。

使用光标是我在互联网上找到的一种方法。但这对我来说不是很清楚。

所以我在这里要做的就是从Customer 和Link_no 从Link 获取未使用的customer_no 的结果,并在row1 中插入值是表Customer_link 中的相应列并循环到更新结果并再次获取 row1 中的值,以将它们作为参数添加到我调用的函数中

【问题讨论】:

  • 表也都已经创建好了。该函数(我需要调用的函数)的作用是,它获取我提供给它的值,从表(客户和链接)中验证,然后将其与其他必需值一起添加到表 Customer_link 中

标签: sql oracle plsql plsqldeveloper


【解决方案1】:

我认为它不需要循环,只需按照此步骤操作即可

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

【讨论】:

  • 我想不会。表 Customer_link 有多个列也需要有值。因此循环,以便我可以添加强制值以及所需的客户编号。所以我需要一些能让我只复制结果列的第一个值的东西
  • select c.customer_no into #temp ... 对 Oracle(或标准 SQL)无效。要基于选择创建新表,您需要在 Oracle(和标准 SQL)中使用 create table ... as select ...。另外,根本不需要临时表。 insert into customer_link (customer_no) select ... 将与原始选择一起使用。
  • 我不知道 oracle 但在 sql 中我已经完成了这个服务时间
  • 您没有在“SQL”中完成此操作,您已在“SQL Server”中完成此操作 - 差别很大。 “SQL”是一种查询语言而不是 DBMS 产品
  • 请原谅这是 SQL SERVER @a_horse_with_no_name
【解决方案2】:

不需要循环。只需使用 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_nocustomer 表中的主键,这两个语句只会为customer_no 列选择(并插入)唯一值(即没有重复值)。

【讨论】:

  • 我已经编辑了这个问题。希望它现在更有意义:) 我可能会以不同于我需要的方式表达它
【解决方案3】:

customer_no 的第一个值是什么?我猜是这样的顺序。

此查询将仅添加返回的第一个值 (rownum =1) 和指定顺序中的 customer_no (ORDER BY c.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。

【讨论】:

  • 我现在已经编辑了这个问题。看来我完全错了。我希望它现在更有意义:)
【解决方案4】:

我终于找到了一种使用光标来完成这项工作的方法。我将在这里提出解决方案,以便其他人可能需要帮助。

我创建了一个类型为 Customer.Customer_no 的变量 cust

cust Customer.Customer_no%type;

然后我创建了一个光标,以便能够将未使用的客户编号的值添加到

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

【讨论】:

    猜你喜欢
    • 2014-07-22
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    相关资源
    最近更新 更多