[英]Select statement in PL/SQL
Does anyone know how to use their tables and columns from pre-existing databases in a pl/sql command. 有谁知道如何在pl / sql命令中使用现有数据库中的表和列。 I am using something from Oracles webpage to use as an example. 我正在使用Oracles网页上的内容作为示例。
SQL> declare
2 v_line varchar2(40):= '&v_string';
3 begin
4 v_line := 'Hello '||v_line;
5 dbms_output.put_line (v_line);
6 end;
7 /
old 2: v_line varchar2(40):= '&v_string';
New 2: v_line varchar2(40):= 'Thomas';
Hello Thomas
PL/SQL procedure successfully completed.
I am wanting to be able to manipulate the data from above. 我希望能够从上方操纵数据。 Lets say I have a table called name_s and I have columns called n_first and n_last and I want to use those columns to output 'Hello Thomas' but using those tables. 可以说我有一个名为name_s的表,并且有名为n_first和n_last的列,我想使用这些列来输出“ Hello Thomas”,但要使用这些表。
I am new to pl.sql and trying to learn the syntax. 我是pl.sql的新手,正在尝试学习语法。
I wasn't sure if you could, 我不确定是否可以
DECLARE
n_first
n_last
BEGIN
SELECT * FROM name_s
n_fisrt := 'Thomas'||n_first;
dbms_output.put_line(n_first)
END;
/
Desired Output: 'Thomas'
I understand that this is not syntactically correct but I am just trying to pick up on how you can use data from your existing tables to get this sort of desired output 我知道这在语法上是不正确的,但我只是想尝试如何使用现有表中的数据来获得所需的输出
EDIT: The above table is hypothetical and only contains two columns for this example. 编辑:上表是假设的,对于此示例仅包含两列。 Now using real tables and data. 现在使用真实的表和数据。
SET SERVEROUTPUT ON;
begin
for cur_r in (select loc_t from loc_t_matrix where store_s in(1,2,3,4,5); loop
dbms_output.put_line('Hello store' || cur_r.loc_traits );
end loop;
end;
/
The above does not compile 上面没有编译
Do you need something like this 你需要这样的东西吗
declare
v_authName author.author_last_name%type;
begin
select
author_last_name into v_authName
from
author
where
author_key = 'A103';
dbms_output.put_line('Name: '||v_authName);
end;
/
Name: weaton
http://www.dba-oracle.com/t_pl_sql_plsql_select_into_clause.htm http://www.dba-oracle.com/t_pl_sql_plsql_select_into_clause.htm
As tables usually contain more than one row, I presume that NAME_S also contains several rows. 由于表通常包含多行,因此我认为NAME_S也包含多行。 In that case, the simplest option to display those names is a cursor FOR loop (Google for it). 在这种情况下,显示这些名称的最简单的选择是游标FOR循环(适用于Google)。 For example: 例如:
begin
for cur_r in (select n_first, n_last from name_s) loop
dbms_output.put_line('Hello ' || cur_r.n_first ||' '||cur_r.n_last);
end loop;
end;
/
As you can see, you don't need local variables in that case. 如您所见,在这种情况下,您不需要局部变量。
If you used your SELECT ... INTO (as suggested by mCeviker), that would work unless it raises TOO-MANY-ROWS (as you can't put all rows' names into a single variable). 如果您使用了SELECT ... INTO(如mCeviker所建议),那么它将起作用,除非它引起TOO-MANY-ROWS(因为您不能将所有行的名称放入单个变量中)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.