cursor B1 is select user_id, user_code, user_name, electric_power_ct, elec_number, zone_code from be_user t where t.zone_code = Chzonecode and t.user_type in ('0', '1', '8', '15'); B B1%rowtype;把这段sql中Chzonecode改成if判断不为空的时候才在where后面去匹配
时间: 2023-06-18 11:01:59 浏览: 93
你可以这样修改:
```
IF Chzonecode IS NOT NULL THEN
OPEN B1 FOR
SELECT user_id, user_code, user_name, electric_power_ct, elec_number, zone_code
FROM be_user t
WHERE t.zone_code = Chzonecode AND t.user_type IN ('0', '1', '8', '15');
ELSE
OPEN B1 FOR
SELECT user_id, user_code, user_name, electric_power_ct, elec_number, zone_code
FROM be_user t
WHERE t.user_type IN ('0', '1', '8', '15');
END IF;
FETCH B1 INTO B;
WHILE B1%FOUND LOOP
-- do something with B
FETCH B1 INTO B;
END LOOP;
CLOSE B1;
```
这样修改后,如果Chzonecode为空,就会执行第二个SELECT语句,否则就执行第一个SELECT语句。
相关问题
解释一下这段sql:cursor B1 is select user_id, user_code, user_name, electric_power_ct, elec_number, zone_code from be_user t where t.zone_code = Chzonecode and t.user_type in ('0', '1', '8', '15'); B B1%rowtype;
这段SQL是PL/SQL语言中用于定义游标的代码。具体解释如下:
- `cursor B1 is`:定义游标名称为B1。
- `select user_id, user_code, user_name, electric_power_ct, elec_number, zone_code from be_user t where t.zone_code = Chzonecode and t.user_type in ('0', '1', '8', '15');`:查询语句,查询表be_user中的user_id、user_code、user_name、electric_power_ct、elec_number和zone_code字段,其中筛选条件是zone_code等于变量Chzonecode的值,并且user_type字段的值为0、1、8或15。
- `B B1%rowtype;`:定义一个记录类型为B1游标返回结果的记录变量B。
综上,这段SQL的作用是定义了一个名为B1的游标,用于查询be_user表中符合条件的记录,并将结果赋值给记录变量B。
IF Chzonecode IS NOT NULL THEN OPEN B1 FOR SELECT user_id, user_code, user_name, electric_power_ct, elec_number, zone_code FROM be_user t WHERE t.zone_code = Chzonecode AND t.user_type IN ('0', '1', '8', '15');ELSE OPEN B1 FOR SELECT user_id, user_code, user_name, electric_power_ct, elec_number, zone_code FROM be_user t WHERE t.user_type IN ('0', '1', '8', '15');END IF;FETCH B1 INTO B;WHILE B1%FOUND LOOP -- do something with B FETCH B1 INTO B;END LOOP;CLOSE B1; 这样会报错请声明B1,请重新修改一下
好的,修改如下:
DECLARE
B1 SYS_REFCURSOR;
B be_user%ROWTYPE;
BEGIN
IF Chzonecode IS NOT NULL THEN
OPEN B1 FOR
SELECT user_id, user_code, user_name, electric_power_ct, elec_number, zone_code
FROM be_user t
WHERE t.zone_code = Chzonecode AND t.user_type IN ('0', '1', '8', '15');
ELSE
OPEN B1 FOR
SELECT user_id, user_code, user_name, electric_power_ct, elec_number, zone_code
FROM be_user t
WHERE t.user_type IN ('0', '1', '8', '15');
END IF;
FETCH B1 INTO B;
WHILE B1%FOUND LOOP
-- do something with B
FETCH B1 INTO B;
END LOOP;
CLOSE B1;
END;
阅读全文