Sql 如何在块中运行查询和事务,然后检查运行时并将结果存储在单独的表中?
我有一个游戏的想法,创建了一些虚拟数据,带有一些查询和事务。然而,我很难将我的查询和事务包含到一个块中进行度量。我正在使用Oracle 18c express。 我的问题是,我找不到正确的语法来运行选择整行、甚至表或块内事务的查询 我在文档中发现的东西不起作用 以下是我的代码,我希望查询和事务成为块的一部分-以下几行正是我需要的内容,但它们工作得很好:Sql 如何在块中运行查询和事务,然后检查运行时并将结果存储在单独的表中?,sql,oracle,Sql,Oracle,我有一个游戏的想法,创建了一些虚拟数据,带有一些查询和事务。然而,我很难将我的查询和事务包含到一个块中进行度量。我正在使用Oracle 18c express。 我的问题是,我找不到正确的语法来运行选择整行、甚至表或块内事务的查询 我在文档中发现的东西不起作用 以下是我的代码,我希望查询和事务成为块的一部分-以下几行正是我需要的内容,但它们工作得很好: SET SERVEROUTPUT ON alter system flush buffer_cache; alter system flush
SET SERVEROUTPUT ON
alter system flush buffer_cache;
alter system flush shared_pool;
CREATE OR REPLACE function timestamp_diff(a timestamp, b timestamp) return number is
begin
return extract (day from (a-b))*24*60*60 +
extract (hour from (a-b))*60*60+
extract (minute from (a-b))*60+
extract (second from (a-b));
end;
/
在这里,我创建了我的块。此版本为空,但正在运行
DECLARE
inde_x number(4);
temp_be timestamp;
temp_af timestamp;
temp_diff_a long;
temp_diff_b long;
temp_diff_c long;
temp_diff_d long;
temp_diff_e long;
temp_diff_f long;
temp_diff_g long;
BEGIN
FOR inde_x in 1 .. 1 LOOP
temp_be := current_timestamp;
Rollback;
temp_af := current_timestamp;
temp_diff_a := timestamp_diff(temp_af, temp_be);
dbms_output.put_line(temp_diff_a);
temp_be := current_timestamp;
temp_af := current_timestamp;
temp_diff_b := timestamp_diff(temp_af, temp_be);
dbms_output.put_line(temp_diff_b);
temp_be := current_timestamp;
temp_af := current_timestamp;
temp_diff_c := timestamp_diff(temp_af, temp_be);
dbms_output.put_line(temp_diff_c);
temp_be := current_timestamp;
temp_af := current_timestamp;
temp_diff_d := timestamp_diff(temp_af, temp_be);
dbms_output.put_line(temp_diff_d);
temp_be := current_timestamp;
temp_af := current_timestamp;
temp_diff_e := timestamp_diff(temp_af, temp_be);
dbms_output.put_line(temp_diff_e);
temp_be := current_timestamp;
temp_af := current_timestamp;
temp_diff_f := timestamp_diff(temp_af, temp_be);
dbms_output.put_line(temp_diff_f);
temp_be := current_timestamp;
temp_af := current_timestamp;
temp_diff_g := timestamp_diff(temp_af, temp_be);
dbms_output.put_line(temp_diff_g);
INSERT INTO time_in_ms (run_id, query_one, query_two, query_three, transaction_one, transaction_two, transaction_three, transaction_four)
VALUES (inde_x, temp_diff_a, temp_diff_b, temp_diff_c, temp_diff_d, temp_diff_e, temp_diff_f, temp_diff_g);
END LOOP;
END;
这里我将介绍一些示例查询和事务。完成的版本会大得多。这些查询和事务都像现在一样工作。然而,我希望他们在街区,他们需要的时间也将被衡量
--query_one
select villager_id from villagers
inner join families
on villagers.family_id = families.family_id
inner join houses
on families.house_id = houses.house_id
inner join villages
on houses.village_id = villages.village_id
inner join locations
on villages.location_id = locations.location_id
order by villagers.age;
--query_two
select families.family_id, count(animals.animal_id) from animals
left join villagers
on animals.animal_id = villagers.animal_id
left join families
on villagers.family_id = families.family_id
WHERE families.family_happiness
between
(select avg(family_happiness) from families)-0.4
and
(select avg(family_happiness) from families)+0.4
GROUP by families.family_id
ORDER by families.family_id;
--query_three
--something with dates
--transaction_one
update jobs j
set j.wage = j.wage*(10 + dbms_random.value*0.5)
where j.villager_id in (
select v.villager_id from villagers v
inner join families
on v.family_id = families.family_id
inner join houses
on families.house_id = houses.house_id
inner join villages
on houses.village_id = villages.village_id
inner join locations
on villages.location_id = locations.location_id
where j.location_id != villages.location_id);
Rollback;
--transaction_two
--something that changes strings
--transaction_three
update jobs j
set j.risk_factor = j.risk_factor/2
WHERE j.job_name NOT LIKE '%De%' or j.job_name NOT LIKE '%Po%' or j.job_name NOT LIKE '%En%'
and j.location_id in (
SELECT l.location_id from locations l
left join villages
on l.location_id = villages.location_id
WHERE l.location_name NOT LIKE '%De%' or l.location_name NOT LIKE '%Po%' or l.location_name NOT LIKE '%En%'
AND villages.village_name NOT LIKE '%De%' or villages.village_name NOT LIKE '%Po%' or villages.village_name NOT LIKE '%En%'
AND l.Max_Number_of_Animals > 10
AND l.food_content < (
SELECT avg(ll.food_content) from locations ll
WHERE ll.Max_Number_of_Animals > 10));
Rollback;
--transaction_four
update animals a
set a.meat_content = CASE
WHEN a.domesticated = 'true'
THEN a.meat_content*(1.1 + dbms_random.value*0.30)
ELSE a.meat_content*(1.05 + dbms_random.value*0.15)
END
WHERE a.animal_id in (
select an.animal_id from animals an
full join villagers
on an.animal_id = villagers.animal_id
full join jobs
on villagers.villager_id = jobs.villager_id
left join locations
on jobs.location_id = locations.location_id
WHERE (jobs.ending_on-starting_on > 2)
OR (locations.food_content > 0.3)
OR villagers.age between 15 and 50);
Rollback;
更新了游标示例:
begin
for c_record in (
select families.family_id, count(animals.animal_id) cnt
from animals
... etc ... ) loop
--do something with the record
c_record.family_id
c_record.cnt
..etc..
end loop;
如果要在PL/SQL块中选择值,则需要使用select。。。进入您需要声明变量以将SELECT的结果插入。例如:
declare
vid number;
begin
select villager_id
into vid
from villagers
inner join families.. etc;
恐怕不清楚你的实际问题是什么,我会更新的。谢谢。是的,但是我试着用行,而不是单个值来做这件事。还有,如何在块中运行update语句?然后声明所需字段的记录,并在记录中选择。