DECLARE CURSOR must be the only statement in a query batch - sybase-ase

I have this below script that is getting "DECLARE CURSOR must be the only statement in a query batch". May I ask what's wrong this this?
This script is saved in a file and is being executed from isql client.
DECLARE c1 cursor FOR
select sql_query from tempdb..M3_Checks_Query
GO
DECLARE #sql_query text
OPEN c1
FETCH c1 into #sql_query --#check_id , #sql_query, #base_table, #run_flag
WHILE ##sqlstatus = 0
BEGIN
print "test"
FETCH c1 into #sql_query
END
CLOSE c1
DEALLOCATE c1

Related

i want to generate a patient details report based on the input (input should be operators like '>','<','=') given by the user

//Program to generate a patient details report based on the input given by the //user
CREATE or REPLACE procedure XX_TEAM18_PROc2
(Admit_date in date,
Bill_amt in number,
action in varchar2)
is
cursor c
is
SELECT P.Patient_name,ph.Symptom_issue Symptom,ph.Consulted_dr Doctor,ph.Diagnosis,ph.Bill_Amount
FROM
XX_Patient_Master_Team18 P,XX_Patient_History_Team18 ph
WHERE
p.Patient_Id=ph.Patient_id
AND
bill_amount action bill_amount;
rec c%rowtype;
cursor c1
is
SELECT P.Patient_name,ph.Symptom_issue Symptom,ph.Consulted_dr Doctor,ph.Diagnosis,ph.Admitted_date Admitted_date
FROM
XX_Patient_Master_Team18 P,XX_Patient_History_Team18 ph
WHERE
p.Patient_Id=ph.Patient_id
AND
Admitted_date action admit_date ;
rec1 c1%rowtype;
begin
open c;
dbms_output.put_line(' records belongs to '||Bill_Amt);
fetch c into rec ;
loop
exit when c%notfound;
dbms_output.put_line(rec.Symptom||' '||rec. Doctor||' '||rec.Diagnosis||' '||rec.bill_Amount);
end loop;
close c;
open c1;
dbms_output.put_line(' records belongs to given '||Admit_date);
fetch c1 into rec1 ;
loop
exit when c1%notfound;
dbms_output.put_line(rec1.Symptom||' '||rec1.Doctor||' '||rec1.Diagnosis||' '||rec1.Admitted_date);
end loop;
close c1;
end ;
You must use dynamic SQL as parameter action cannot be used in a fixed SQL statement within PL/SQL. Parameters can only be used as substitution values, not as column names or operators unless you are using dynamic SQL.
You will need to create a varchar2 variable to hold the SQL statement, then use the DBMS_SQL package to create the cursor. DBMS_SQL has pretty good documentation within it, don't forget to close the cursor when you are done using it.

How to use two cursors for writing in single file?

Suppose In my procedure, there are two cursors and I have a requirement of writing from both the cursors, 1st line from 1st cursor and 2nd line from 2nd cursor and so on..
How do i achieve it??? I know how to fetch from single curs
or.
Or there is some other way.
Thanks in Advance,
Nitika
You could use two for loop statements, like this:
CREATE OR REPLACE procedure aProc(filter IN number) IS
cursor c1 is select something, something_else from myTable where something = filter;
cursor c2 is select something2, something_else2 from myTable2;
begin
for alias1 in c1 loop
for alias2 in c2 loop
if(alias1.something = 1) then
insert into myTable3 (value1,value2,value3)
values (alias1.something_else,
alias2.something2,
alias2.something_else2);
end if;
end loop;
end loop;
end;
The for loop statement will close the open cursors when no more data is found.

Print Oracle Pl/sql Cursor

Can I directly open and print cursor in begin end block without using loops?
or without reading each column and printing it individually for each row.
If you want to display one record there is no need for looping in your cursor.
If multiple then you need looping in cursor.
Eg In **Test_Project** Table has two column:-
projectid projectname
p2 Programming
p1 Search Engine
p3 Testing
--Below is the code to display one record without using loop
Declare
cprojname varchar2(2000) ;
CURSOR c1
IS
select project_name from test_project WHERE projectid='p1';
BEGIN
OPEN c1;
FETCH c1 INTO cprojname;
if c1%notfound then
cprojname := 'no data';
else
dbms_output.put_line(cprojname);
end if;
CLOSE c1;
END;
o/p:- Search Engine

Outputting PL/SQL query results as tabbed result sets while using variables

I'm trying to do something that seems simple in PL/SQL Developer but eludes me. If I open a 'SQL Window' and run a series of SELECT statements as below, it appears as tabbed result sets in the bottom part of the same window.
SELECT * FROM t1 WHERE r1 = 'xyz';
SELECT * FROM t2 WHERE r1 = 'xyz';
However, if I want to declare 'xyz' as a variable, I have to use a DECLARE and BEGIN-END block, which means I now have to use INTO otherwise it moans. So, I've tried various methods and come up with this:
DECLARE
val VARCHAR(10) := 'xyz';
CURSOR c1 IS SELECT * FROM t1 WHERE r1 = val;
CURSOR c2 IS SELECT * FROM t2 WHERE r1 = val;
TYPE c1_type IS TABLE OF c1%ROWTYPE;
TYPE c2_type IS TABLE OF c2%ROWTYPE;
rec1 c1_type;
rec2 c2_type;
BEGIN
OPEN c1;
FETCH c1 BULK COLLECT INTO rec1;
FOR i IN 1..rec1.count LOOP
dbms_output.put_line(rec1(i).col1||' 'rec1(i).col2);
END LOOP;
OPEN c2;
FETCH c2 BULK COLLECT INTO rec2;
FOR j IN 1..rec2.count LOOP
dbms_output.put_line(rec2(j).col1||' 'rec2(j).col2);
END LOOP;
END
However, this means I have to know all the column names of t1 and t2 and it puts out the results in textual format rather than the nice output table in the bottom part of the window.
Is there an easy way of doing this so that the results appear as tabbed result sets in the bottom part of the SQL Window and still allow me to declare variables to use throughout the query?

DBMS_OUTPUT.PUT_LINE not printing

When executing the following code, it just says the procedure is completed and doesn't print the infomation i want it to (firstName, lastName) and then the other values from the select query in a table below.
CREATE OR REPLACE PROCEDURE PRINT_ACTOR_QUOTES (id_actor char)
AS
CURSOR quote_recs IS
SELECT a.firstName,a.lastName, m.title, m.year, r.roleName ,q.quotechar from quote q, role r,
rolequote rq, actor a, movie m
where
rq.quoteID = q.quoteID
AND
rq.roleID = r.roleID
AND
r.actorID = a.actorID
AND
r.movieID = m.movieID
AND
a.actorID = id_actor;
BEGIN
FOR row IN quote_recs LOOP
DBMS_OUTPUT.PUT_LINE('a.firstName' || 'a.lastName');
end loop;
END PRINT_ACTOR_QUOTES;
/
When setting server output on, I get
a.firstNamea.lastName
a.firstNamea.lastName
a.firstNamea.lastName
a.firstNamea.lastName
multiple times!
What is "it" in the statement "it just says the procedure is completed"?
By default, most tools do not configure a buffer for dbms_output to write to and do not attempt to read from that buffer after code executes. Most tools, on the other hand, have the ability to do so. In SQL*Plus, you'd need to use the command set serveroutput on [size N|unlimited]. So you'd do something like
SQL> set serveroutput on size 30000;
SQL> exec print_actor_quotes( <<some value>> );
In SQL Developer, you'd go to View | DBMS Output to enable the DBMS Output window, then push the green plus icon to enable DBMS Output for a particular session.
Additionally, assuming that you don't want to print the literal "a.firstNamea.lastName" for every row, you probably want
FOR row IN quote_recs
LOOP
DBMS_OUTPUT.PUT_LINE( row.firstName || ' ' || row.lastName );
END LOOP;
Ensure that you have your Dbms Output window open through the view option in the menubar.
Click on the green '+' sign and add your database name.
Write 'DBMS_OUTPUT.ENABLE;' within your procedure as the first line.
Hope this solves your problem.
this statement
DBMS_OUTPUT.PUT_LINE('a.firstName' || 'a.lastName');
means to print the string as it is.. remove the quotes to get the values to be printed.So the correct syntax is
DBMS_OUTPUT.PUT_LINE(a.firstName || a.lastName);
Set Query as below at first line
SET SERVEROUTPUT ON
I am using Oracle SQL Developer,
In this tool, I had to enable DBMS output to view the results printed by dbms_output.put_line
You can find this option in the result pane where other query results are displayed.
so, in the result pane, I have 7 tabs. 1st tab named as Results, next one is Script Output and so on. Out of this you can find a tab named as "DBMS Output" select this tab, then the 1st icon (looks like a dialogue icon) is Enable DBMS Output. Click this icon. Then you execute the PL/SQL, then select "DBMS Output tab, you should be able to see the results there.
All of them are concentrating on the for loop but if we use a normal loop then we had to use of the cursor record variable. The following is the modified code
CREATE OR REPLACE PROCEDURE PRINT_ACTOR_QUOTES (id_actor char)
AS
CURSOR quote_recs IS
SELECT a.firstName,a.lastName, m.title, m.year, r.roleName ,q.quotechar from quote q, role r,
rolequote rq, actor a, movie m
where
rq.quoteID = q.quoteID
AND
rq.roleID = r.roleID
AND
r.actorID = a.actorID
AND
r.movieID = m.movieID
AND
a.actorID = id_actor;
recd quote_recs%rowtype;
BEGIN
open quote_recs;
LOOP
fetch quote_recs into recs;
exit when quote_recs%notfound;
DBMS_OUTPUT.PUT_LINE(recd.firstName||recd.lastName);
end loop;
close quote_recs;
END PRINT_ACTOR_QUOTES;
/

Resources