我有大约15个不同的表填充了不同的数据和不同的实体关系。
我需要创建一个脚本,用这些表的内容填充我的数据库。
脚本完成后,我在cmd中运行它,使用sqlplus和稍后的START 路径到文件
我有两个不同的sql文件,一个名为db_spec.sql,另一个名为db_body.sql。
在我的db_body.sql中,我创建了一个存储来自两个具有1:N关系的表的数据的过程。
第一张表
CREATE TABLE LOCATION (
ID_LOCATION INTEGER NOT NULL,
LOCATION_NAME VARCHAR2 (20) NOT NULL,
POSTCODE INTEGER NOT NULL
);
ALTER TABLE LOCATION
ADD (CONSTRAINT PK_LOCATION PRIMARY KEY (ID_LOCATION));
第二张表
CREATE TABLE ADDRESS (
ID_ADDRESS INTEGER NOT NULL,
STREET VARCHAR2 (20) NOT NULL,
HOUSE_NUMBER INTEGER NOT NULL,
FK_ID_LOCATION INTEGER NOT NULL
);
ALTER TABLE ADDRESS
ADD (CONSTRAINT PK_ADRESS PRIMARY KEY (ID_ADRESS));
ALTER TABLE ADRESS
ADD (CONSTRAINT FK_ADRESS_ID_LOCATION FOREIGN KEY
(FK_ID_LOCATION) REFERENCES LOCATION(ID_LOCATION));
现在我需要使用数据填充它们。让我们说
LOCATION_NAME ="伦敦" POSTOCDE =" 394505" ......等等
我已经创建了这个脚本,但是当我运行它时,没有任何显示,所以显然有一些错误。
db_spec.sql脚本
CREATE OR REPLACE PACKAGE apartment AS
PROCEDURE fill_location(location_number NUMBER);
PROCEDURE fill_address(number_of_addresses NUMBER);
END apartment;
db_body.sql脚本
SET SERVEROUTPUT ON
SET LINESIZE 400
SET TIMING ON
CREATE OR REPLACE PACKAGE BODY address AS
PROCEDURE fill_location(location_number NUMBER) IS
p_location_name VARCHAR2(20);
p_postcode NUMBER (10,2);
BEGIN
FOR num IN 1..location_number LOOP
p_location_name := 'Location';
p_postcode := dbms_random.value(1000,9600);
p_postcode := p_postcode ||' '|| TO_CHAR(num);
INSERT INTO LOCATION (ID_LOCATION, LOCATION, POSTCODE)
VALUES (num, p_location_name, p_postcode);
dbms_output.put_line(num);
END LOOP;
END fill_location;
PROCEDURE fill_address(number_of_adresses NUMBER)IS
p_street_name VARCHAR(20);
p_house_number NUMBER (10,2);
p_id_address NUMBER(10);
CURSOR data IS
SELECT ID_LOCATION
FROM LOCATION;
BEGIN
FOR num_loop IN data LOOP
FOR num IN 1..number_of_adresses LOOP
p_street_name := 'Ulica';
p_house_number := dbms_random.value(1,99);
p_street_name := p_street_name ||' '|| TO_CHAR(num);
SELECT NVL(MAX(p_id_address)+1,1)
INTO p_id_address
FROM ADDRESS;
INSERT INTO ADDRESS (ID_ADDRESS, FK_ID_LOCATION, STREET, HOUSE_NUMBER)
VALUES (p_id_address, num_loop.ID_LOCATION, p_street_name, p_house_number);
dbms_output.put_line(num_loop.ID_LOCATION);
END LOOP;
END LOOP;
END fill_address;
END;
SHOW ERRORS;
你们可以帮我修复问题,这样代码就可以正常运行吗?任何意见都表示赞赏!
顺便说一句 oprema_stanovanja.polni_kraj = address.fill_location
答案 0 :(得分:0)
除非我错过了...... COMMIT;
根据您发布的屏幕截图,在您的正文和过程定义的末尾添加/
。
答案 1 :(得分:0)
首先,发布的包没有编译而没有错误。
您需要先修复以下内容:
FOR LOOP
END LOOP
语句
BEGIN
END <procedure_name_here>
个程序
LOCATION
具有相同名称的列名和PL / SQL变量。 Oracle无法解决在INSERT] 为了帮助您入门,我已修复了下面的fill_location
程序。对其他程序也同样进行。
create or replace package body apartment as
procedure fill_location(location_number number) is
p_location_name varchar2(20);
p_postcode number(10,2);
begin
for num in 1.. location_number loop
p_location_name := 'location';
p_postcode := dbms_random.value(1000,9600);
p_location_name := p_location_name ||' '|| to_char(num);
insert into location (id_location, location_name, postcode)
values (num, p_location_name, p_postcode);
dbms_output.put_line(num);
end loop;
end fill_location;
修复上述错误/建议,直到您成功编译PL / SQL。如果你编译错误&#39;使用&#34;显示错误&#34;找到并修复任何其他错误。