无法理解如何使用PLSQL
我们在课堂上学习游标,我相信我理解的第一个问题是: 设置包含销售、退货和采购的库存表和交易表(交易表的代码应为销售为S,退货为R,采购为p)。创建脚本以创建数据并将其插入这些表中 我相信我只是建立了一个表并生成了一些PLSQL,这样我就可以在每一行中输入。下面的问题措辞更为含糊 使用您在第四个问题中创建的表,处理事务并确定对库存的影响。显示原始库存和处理销售、退货和采购后的库存的信息。你需要使用游标 我该如何解决这个问题?谢谢你的帮助无法理解如何使用PLSQL,sql,oracle,plsql,logic,Sql,Oracle,Plsql,Logic,我们在课堂上学习游标,我相信我理解的第一个问题是: 设置包含销售、退货和采购的库存表和交易表(交易表的代码应为销售为S,退货为R,采购为p)。创建脚本以创建数据并将其插入这些表中 我相信我只是建立了一个表并生成了一些PLSQL,这样我就可以在每一行中输入。下面的问题措辞更为含糊 使用您在第四个问题中创建的表,处理事务并确定对库存的影响。显示原始库存和处理销售、退货和采购后的库存的信息。你需要使用游标 我该如何解决这个问题?谢谢你的帮助 编辑:我相信我也会使用带有游标的过程,但仍然会丢失 我假设您
编辑:我相信我也会使用带有游标的过程,但仍然会丢失 我假设您有一个运行SQL命令的工具,并且您熟悉如何使用它。如果没有,你需要先得到那个工具。如果您想使用命令行风格,可以使用sqlplus客户端,如果您想使用友好的GUI,可以使用Oracle的sqldeveloper
祝你好运。一般来说,人们不喜欢回答这类问题,因为它们显然是为了完成学校作业而产生的。我不会完成你的作业,但会回答你关于光标的问题 在PL/Sql过程、函数和包中使用curse来存储指向查询结果的指针。例如:
-- You cannot use a REF CURSOR directly, you need to create variable that is a REF CURSOR TYPE
-- I use a Type to declare a REF CURSOR
Type MyDataCursor is REF CURSOR;
-- And then declare a variable of that type
pDataOut MyDataCursor;
-- You then can use the cursor in code
Open pDataOut FOR Select * from sometable
FETCH MyDataCursor INTO MyDataRowStructure;
-- This particular structure is a TYPE of RECORD
TYPE MyDataRowStructure IS RECORD (
ID NUMBER,
Name NUMBER,
Address VARCHAR2(50),
City VARCHAR2(50),
State Varchar2(2),
ZIP Varchar2(5)
);
--You start with the first fetch
FETCH MyDataCursor INTO MyDataRowStructure;
-- Now loop through all the rows
WHILE MyDataCursor %FOUND LOOP
-- Do some stuff here with the first row, then fetch the next row ....
FETCH MyDataCursor INTO MyDataRowStructure;
END LOOP;
这将从表中检索数据,您现在可以通过多种方式访问该数据。最常见的方法是获取数据并将其存储在与表行结构匹配的结构中。例如:
-- You cannot use a REF CURSOR directly, you need to create variable that is a REF CURSOR TYPE
-- I use a Type to declare a REF CURSOR
Type MyDataCursor is REF CURSOR;
-- And then declare a variable of that type
pDataOut MyDataCursor;
-- You then can use the cursor in code
Open pDataOut FOR Select * from sometable
FETCH MyDataCursor INTO MyDataRowStructure;
-- This particular structure is a TYPE of RECORD
TYPE MyDataRowStructure IS RECORD (
ID NUMBER,
Name NUMBER,
Address VARCHAR2(50),
City VARCHAR2(50),
State Varchar2(2),
ZIP Varchar2(5)
);
--You start with the first fetch
FETCH MyDataCursor INTO MyDataRowStructure;
-- Now loop through all the rows
WHILE MyDataCursor %FOUND LOOP
-- Do some stuff here with the first row, then fetch the next row ....
FETCH MyDataCursor INTO MyDataRowStructure;
END LOOP;
该结构将模拟表中一行中的所有数据列,例如:
-- You cannot use a REF CURSOR directly, you need to create variable that is a REF CURSOR TYPE
-- I use a Type to declare a REF CURSOR
Type MyDataCursor is REF CURSOR;
-- And then declare a variable of that type
pDataOut MyDataCursor;
-- You then can use the cursor in code
Open pDataOut FOR Select * from sometable
FETCH MyDataCursor INTO MyDataRowStructure;
-- This particular structure is a TYPE of RECORD
TYPE MyDataRowStructure IS RECORD (
ID NUMBER,
Name NUMBER,
Address VARCHAR2(50),
City VARCHAR2(50),
State Varchar2(2),
ZIP Varchar2(5)
);
--You start with the first fetch
FETCH MyDataCursor INTO MyDataRowStructure;
-- Now loop through all the rows
WHILE MyDataCursor %FOUND LOOP
-- Do some stuff here with the first row, then fetch the next row ....
FETCH MyDataCursor INTO MyDataRowStructure;
END LOOP;
最后,您希望遍历从表中检索到的所有数据行,并对它们进行处理。例如:
-- You cannot use a REF CURSOR directly, you need to create variable that is a REF CURSOR TYPE
-- I use a Type to declare a REF CURSOR
Type MyDataCursor is REF CURSOR;
-- And then declare a variable of that type
pDataOut MyDataCursor;
-- You then can use the cursor in code
Open pDataOut FOR Select * from sometable
FETCH MyDataCursor INTO MyDataRowStructure;
-- This particular structure is a TYPE of RECORD
TYPE MyDataRowStructure IS RECORD (
ID NUMBER,
Name NUMBER,
Address VARCHAR2(50),
City VARCHAR2(50),
State Varchar2(2),
ZIP Varchar2(5)
);
--You start with the first fetch
FETCH MyDataCursor INTO MyDataRowStructure;
-- Now loop through all the rows
WHILE MyDataCursor %FOUND LOOP
-- Do some stuff here with the first row, then fetch the next row ....
FETCH MyDataCursor INTO MyDataRowStructure;
END LOOP;
这应该让您开始。“您需要使用游标”。这真的很可悲。是的,我真的很紧张,想知道该怎么做。你对游标了解多少?如果您对它们有更多的了解,您将在任务中找到可以使用游标的点。如果您有更详细的答案,请向我们展示您所取得的成就、一些代码示例以及您遇到的问题。顺便说一句,文档中的很多例子都把它们的游标命名为
c1
,在我看来,这一直是最没用的名字。也不需要像示例那样用大写字母编写数据库代码。叛逆:)你(和/或你的老师)应该了解显式游标和隐式游标之间的区别代码>应该是MyDataCursor sys\u refcursor代码>此外,在某些特定场景之外,显式获取是不寻常的(批量收集、单行查找和循环外所需的记录),但谁知道课程导师的想法。无论哪种方式都有效,我在数百万行代码中使用MyDataCursor is REF CURSOR已经超过30年了sys\u refcursor
,对我来说,是相对较新的。我想为初学者使用简单的代码,fetch
很容易理解。1987年你在PL/SQL中声明了ref游标?直到90年代初的Forms 3.0,我才记得见过一个。无论如何,在您的示例中,MyDataCursor
是一种类型。在那里的某个地方,不记得确切的时间,Oracle 8i如果我记得正确,我可能不记得。自1987年开始为海军工作,现已退休,我是第一个将甲骨文引入我的司令部的人,它最终成为海军标准,他们获得了海军范围的网站许可证。我希望我能为那个决定赢得荣誉,但这是在我的工资等级之上做出的,但我当然支持它!今天,我仍然为自己的公司编写PL/Sql以及VB.NET、C#、javascript等代码。我从未在过程中使用过sys\u refcursor。MyDataCursor是从一个类型派生出来的,就像我说的,工作很好。1987年应该是Oracle5。PL/SQL以(或实际上的Forms 3.0,作为