CREATE OR REPLACE PROCEDURE country_demographics
(p_country_name IN countries.country_name%TYPE,
p_country_demo_rec OUT ed_type)
IS
TYPE ed_type IS RECORD (
c_name countries.country_name%TYPE,
c_location countries.location%TYPE,
c_capitol countries.capitol%TYPE,
c_population countries.population%TYPE,
c_airports countries.airports%TYPE,
c_climate countries.climate%TYPE);
BEGIN
SELECT country_name, location, capitol, population, airports, climate
INTO ed_type.c_name, ed_type.c_location, ed_type.c_capitol, ed_type.population, ed_type.airports, ed_type.climate
FROM countries;
DBMS_OUTPUT.PUT_LINE('Country Name:' || v_country_demo_rec.country_name ||
'Location:' || v_country_demo_rec.location ||
'Capitol:' || v_country_demo_rec.capitol ||
'Population:' || v_country_demo_rec.population ||
'Airports:' || v_country_demo_rec.airports ||
'Climate:' || v_country_demo_rec.climate );
IF SQL%NOTFOUND THEN
RAISE_APPLICATION_ERROR(-20201, 'This country does not exist.');
END IF;
END;
问题是要我创建一个名为country_demograhics
的程序。将country_name
作为IN参数传递。显示CONTRY_NAME
,LOCATION
,CAPITOL
,POPULATION
,AIRPORTS
,CLIMATE
。对select语句的INTO子句使用用户定义的记录结构。如果该国家不存在,则提出例外。
现在这里是我的代码的副本,不断返回错误:
Error at line 0: PL/SQL: Compilation unit analysis terminated.
该错误应该是告诉您的第二个错误,它不会再看了。还应该有另一个错误。我想ed_type在程序之外不存在,因此它不能将ed_type作为OUT参数。外面不知道ed_type。
第一件事 - 看你在声明(p_country_demo_rec)和开始(v_country_demo_rec)部分时使用了不同的变量。我认为这可能是一个错误。
请尝试以下脚本: - 它可能对您有所帮助。
CREATE OR REPLACE PROCEDURE COUNTRY_DEMOGRAPHICS
IS
TYPE ED_TYPE IS TABLE OF countries%ROWTYPE;
p_country_demo_rec ED_TYPE;
BEGIN
SELECT * BULK COLLECT INTO p_country_demo_rec FROM countries;
FOR i IN p_country_demo_rec.FIRST..p_country_demo_rec.LAST
LOOP
DBMS_OUTPUT.PUT_LINE('Country Name:'||p_country_demo_rec(i).country_name ||
'Location:' || p_country_demo_rec(i).location ||
'Capitol:' || p_country_demo_rec(i).capitol ||
'Population:' || p_country_demo_rec(i).population ||
'Airports:' || p_country_demo_rec(i).airports ||
'Climate:' || p_country_demo_rec(i).climate );
END LOOP;
IF SQL%NOTFOUND THEN
RAISE_APPLICATION_ERROR(-20201, 'This country does not exist.');
END IF;
END;
/
EXECUTE COUNTRY_DEMOGRAPHICS;
注意: - 您可以在过程中使用一个参数(IN参数)来获取特定国家/地区受众特征数据,并在select语句中使用该参数来过滤掉特定国家/地区。
例:
CREATE OR REPLACE PROCEDURE COUNTRY_DEMOGRAPHICS(p_country_name IN varchar2)
Select语句如下所示:
SELECT * BULK COLLECT INTO p_country_demo_rec FROM countries where
country_name = ||p_country_name;
执行部分:
EXECUTE COUNTRY_DEMOGRAPHICS(p_country_name);