目前正在做从应用程序迁移到存储过程的工作,程序是用C#书写的。
我现在写存储过程,参数传的是条件。我想在Sql查询出的结果中做循环,但因为是动态Sql,所以目前编译通不过。具体如下(简化过):
CREATE OR REPLACE PROCEDURE RetrieveReport
(
AccountCond Varchar2,
)
Is
AccountSql Varchar2(1000);
Begin
AccountSql := 'SELECT URID FROM Accounts ';If AccountCond <> '' Then
AccountSql := AccountSql + AccountCond;
End If;For rec In (AccountSql) Loop
dbms_output.put_line(rec.AccountID);
End Loop;
End RetrieveReport;
红色字的地方就是出问题的地方,请问各位有什么办法解决这个问题?
我现在写存储过程,参数传的是条件。我想在Sql查询出的结果中做循环,但因为是动态Sql,所以目前编译通不过。具体如下(简化过):
CREATE OR REPLACE PROCEDURE RetrieveReport
(
AccountCond Varchar2,
)
Is
AccountSql Varchar2(1000);
Begin
AccountSql := 'SELECT URID FROM Accounts ';If AccountCond <> '' Then
AccountSql := AccountSql + AccountCond;
End If;For rec In (AccountSql) Loop
dbms_output.put_line(rec.AccountID);
End Loop;
End RetrieveReport;
红色字的地方就是出问题的地方,请问各位有什么办法解决这个问题?
create table Accounts(URID varchar2(100));
insert into Accounts select '1' from dual
declare
type rs is ref cursor;
AccountSql Varchar2(1000);
s rs;
aURID varchar2(100);
Begin
AccountSql:= 'SELECT URID FROM Accounts ';
open s for AccountSql;
loop
fetch s into aURID;
exit when s%notfound;
dbms_output.put_line(aURID);
end loop;
close s;
end;
(
AccountCond Varchar2,
)
Is
AccountSql Varchar2(1000);
ln_count NUMBER;
Begin AccountSql := 'SELECT URID FROM Accounts '; If AccountCond != NULL Then
AccountSql := AccountSql + AccountCond;
End If; EXECUTE IMMEDIATE AccountSql INTO ln_count;For rec In (ln_count) Loop
dbms_output.put_line(rec.AccountID);
End Loop;
End RetrieveReport;
Open Cursor_Variable FOR Select_Statement;
目前我看的书中上面的语句后面的Select_Statement都是具体的SQL语句,但我的SQL语句因为是动态的,所以需要用一个Varchar2的变量来存储SQL语句,那我这样做的话上面的语句怎么写才能编译通过按我的意愿运行呢?