0

我是 sql 新手,我无法弄清楚如何在我们使用带有 pl/sql 变量的别名的地方使用连接。这类似于我的匿名块的样子

DECLARE
 l_count PLS_INTEGER;  
 l_name EmpTable.Name%Type;
BEGIN
   FOR l_row In (
     Select * 
      From User_Tab_Columns
     Where Table_Name = 'EmpTable')
   Loop
     l_name := l_row.Name;
     Select Count(A.l_name)
       INTO l_count
       FROM EmpTable A
      INNER JOIN CustTable B on A.id = B.emp_id;
     DBMS_OUTPUT ("The number of employees who are also customers are " || l_count);
   END LOOP;
END;

我收到错误,因为必须声明“l_name”和“A”。“l_name”标识符无效。流程对我来说非常有意义。我应该知道一些细微差别吗?

4

1 回答 1

1

l_name 是一个变量,而不是 SQL 表达式的一部分。我也更喜欢使用 NUMBER 与 PLS_INTEGER。我想你会想做这样的事情:

DECLARE
 l_count NUMBER;
BEGIN
   Select Count(A.Name)
     INTO l_count
     FROM EmpTable A
    INNER JOIN CustTable B on A.id = B.emp_id;
   DBMS_OUTPUT ("The number of employees who are also customers are " || l_count);
END;
于 2013-10-31T19:22:23.377 回答