有没有办法将变量定义为整数,然后在select语句中将计算值分配给变量,以便可以在select语句中进一步使用变量值。
declare avg integer;
select
Round(V_COMB_PROD_CRW1.PROD_HRS * 3600 / Decode(V_COMB_PROD_CRW1.TOTAL_CYCLES,
0, 1, V_COMB_PROD_CRW1.TOTAL_CYCLES, 20, -3))
我想将值从select移动到avg,然后在select中进一步使用avg。
答案 0 :(得分:1)
正如大家所说,你应该使用SELECT...INTO
,但不要忘记把它放在BEGIN...END
部分,它不能在声明部分(每个人都省略它)< / p>
declare avg integer;
BEGIN
select Round(V_COMB_PROD_CRW1.PROD_HRS * 3600 / Decode(V_COMB_PROD_CRW1.TOTAL_CYCLES, 0, 1, V_COMB_PROD_CRW1.TOTAL_CYCLES, 20, -3))
INTO avg
From DUAL;
END;
答案 1 :(得分:0)
在T-SQL中你可以这样做
declare @avg integer;
set @avg = (select
Round(V_COMB_PROD_CRW1.PROD_HRS * 3600 / Decode(V_COMB_PROD_CRW1.TOTAL_CYCLES,
0, 1, V_COMB_PROD_CRW1.TOTAL_CYCLES, 20, -3)))
select @avg
Oracle语法应该像我记得的那样
declare avg integer
BEGIN
select Round(V_COMB_PROD_CRW1.PROD_HRS * 3600 / Decode(V_COMB_PROD_CRW1.TOTAL_CYCLES,
0, 1, V_COMB_PROD_CRW1.TOTAL_CYCLES, 20, -3)) into avg
from dual
END
答案 2 :(得分:0)
对于Oracle,您需要使用SELECT INTO Clause
select Round(V_COMB_PROD_CRW1.PROD_HRS * 3600 / Decode(V_COMB_PROD_CRW1.TOTAL_CYCLES, 0, 1, V_COMB_PROD_CRW1.TOTAL_CYCLES, 20, -3))
INTO avg
From DUAL;
如果需要对多行执行此操作,则建议使用游标
declare
avg integer;
Begin
For Cur in ( select Round(PROD_HRS * 3600 / Decode(TOTAL_CYCLES, 0, 1, TOTAL_CYCLES, 20, -3)) as avg From V_COMB_PROD_CRW1)
Loop
dbms_output.put_line("Processing ... "||Cur.avg);
avg := Cur.avg ;
-- process your code here using avg variable
End Loop;
end;
/
答案 3 :(得分:-1)
可以在Select。
的帮助下分配值Declare @avg Integer
Select @avg = Round(V_COMB_PROD_CRW1.PROD_HRS * 3600 / Decode(V_COMB_PROD_CRW1.TOTAL_CYCLES,
0, 1, V_COMB_PROD_CRW1.TOTAL_CYCLES, 20, -3)))