Using Copy statement in Stored procedure
The following statement works in sqlplus session:
copy from comment/password@servername append amcomment_temp using
select * from amcomment
where commentid in(1,2,3,4)
I want to use this in a stored procedure. There is a long datatype in this table. The
procedure will not compile. Have tried execute immediate and compiler rejects this statement also.
'COPY' is a SQL*Plus command, not PL/SQL. This is why the PL/SQL compiler throws it out.
Similar Messages
-
Using temporary tables in stored procedures
Suppose that I have created a temporary table in a stored procedure using an "EXECUTE IMMEDIATE" statement. When I compile the procedure, that table is not created yet, so the compiler says that the table does not exist.
What is the way of using temporary tables in stored procedures?It's a good practice to avoid using DDL statements being executed from stored procedures. "Truncate Table" via dynamic SQL from stored procedure is a different story and is useful in DSS environments.
But if you insist on "creating" tables using Dynamic SQL from Stored Procedures then you must also embed your DML statements in Dynamic SQL to avoid compilation errors.
Example:
Create or Replace Procedure Proc_TestDynamicSQL is
Begin
Execute Immediate 'Create table myTable as select * from user_tables' ;
Execute Immediate 'Update myTable set table_name = ''Test'' ' ; --two single quotes before and after the string "Test"
End;
In this case, Oracle wouldn't care about the table references during compilation. -
MS SQL Server 7 - Performance of Prepared Statements and Stored Procedures
Hello All,
Our team is currently tuning an application running on WL 5.1 SP 10 with a MS
SQL Server 7 DB that it accesses via the WebLogic jConnect drivers. The application
uses Prepared Statements for all types of database operations (selects, updates,
inserts, etc.) and we have noticed that a great deal of the DB host's resources
are consumed by the parsing of these statements. Our thought was to convert many
of these Prepared Statements to Stored Procedures with the idea that the parsing
overhead would be eliminated. In spite of all this, I have read that because
of the way that the jConnect drivers are implemented for MS SQL Server, Prepared
Statments are actually SLOWER than straight SQL because of the way that parameter
values are converted. Does this also apply to Stored Procedures??? If anyone
can give me an answer, it would be greatly appreciated.
Thanks in advance!Joseph Weinstein <[email protected]> wrote:
>
>
Matt wrote:
Hello All,
Our team is currently tuning an application running on WL 5.1 SP 10with a MS
SQL Server 7 DB that it accesses via the WebLogic jConnect drivers.The application
uses Prepared Statements for all types of database operations (selects,updates,
inserts, etc.) and we have noticed that a great deal of the DB host'sresources
are consumed by the parsing of these statements. Our thought was toconvert many
of these Prepared Statements to Stored Procedures with the idea thatthe parsing
overhead would be eliminated. In spite of all this, I have read thatbecause
of the way that the jConnect drivers are implemented for MS SQL Server,Prepared
Statments are actually SLOWER than straight SQL because of the waythat parameter
values are converted. Does this also apply to Stored Procedures???If anyone
can give me an answer, it would be greatly appreciated.
Thanks in advance!Hi. Stored procedures may help, but you can also try MS's new free type-4
driver,
which does use DBMS optimizations to make PreparedStatements run faster.
Joe
Thanks Joe! I also wanted to know if setting the statement cache (assuming that
this feature is available in WL 5.1 SP 10) will give a boost for both Prepared Statements
and stored procs called via Callable Statements. Pretty much all of the Prepared
Statements that we are replacing are executed from entity bean transactions.
Thanks again -
How to use order by in stored procedure base block?
How to use order by in stored procedure base block? I need to change order by dynamically
Use SET_BLOCK_PROPERTY('BLOCK_NAME',ORDER_BY,'COLUMN_NAME1, COLUMN_NAME2');
-
Using VIEW inside a stored procedure
Hi,
So I have a View designed in one file that validates someone who is authorized.
Then I have a stored procedure to display authorized stationeries created before a given date. Its takes a parameter @CreatedOn.
How can I use VIEW in my stored procedure so I can display all the authorized which are set 1 before a given date?
My VIEW:
ALTER VIEW vw_AuthorizedStationeries AS
SELECT Authorized
FROM dbo.Stationeries
WHERE Authorized = 1
And my stored procedure:
CREATE PROCEDURE usp_stationeries (@CreatedOn datetime)ASBEGINSELECTFROMENDGO
Thanks!Try something like below, You need to replace CreatedDate column with appropriate date column in your table.
1. Alter the view to get the required date:
ALTER VIEW vw_AuthorizedStationeries AS
SELECT Authorized, CreatedDate
FROM dbo.Stationeries
WHERE Authorized = 1
2. Alter SP:
CREATE PROCEDURE usp_stationeries (@CreatedOn datetime)
AS
BEGIN
SELECT Authorized
FROM vw_AuthorizedStationeries
WHERE CreatedDate <= @CreatedOn
END
GO
OR
You can pull required information directly from your table:
CREATE PROCEDURE usp_stationeries (@CreatedOn datetime)
AS
BEGIN
SELECT Authorized
FROM dbo.Stationeries
WHERE CreatedDate <= @CreatedOn and Authorized = 1
END
GO
If this post answers your query, please click "Mark As Answer" or "Vote as Helpful". -
Can rs.last() be used after calling the stored procedure?
Can rs.last() be used after calling the stored procedure? If yes what should be the CURSOR types?
Can rs.last() be used after calling the stored
procedure? If yes what should be the CURSOR types?That would depend on the driver/database.
And as I said in your other post it is far more efficient to count records by just returning a count rather than a complete collection regardless of how you get to the end. -
Use database inside a stored procedure
Hi guys, a stupid question: I'm working with several database and sometimes I'm afraid to run an alter or a create something in the wrong database, bear in mind that several database have the same tables. Sometime happens to run a query oin the db A
instead in the db B. So, the question: If I create a procedure like this,
create procedure creatingPillar as
use USA
begin
IF EXISTS (SELECT * FROM sys.objects WHERE object_id =
OBJECT_ID(N'pillarretail') AND type in (N'U'))
DROP TABLE PillarRetail
create table PillarRetail (anid int , ancore varchar(20), period dec(18,6), settlem datetime2,
settlementper int, asf dec(18,6),astlf dec (18,6), tot dec (18,6) )
insert into PillarRetail select anid, ancore, Period, settlem, settlementper,
asf, astlf, tot from createsemipillar2011
insert into PillarRetail select select anid, ancore, Period, settlem, settlementper,
asf, astlf, tot createsemipillar
end
but I'm wondering if is useless to put the use USA in the stored procedure by the moment that I'm creating the procedure in USA. I mean, having the SP in the USA db even if I run the procedure from another db it should run in USA. Just a stupid question
I know but I got this doubt...
ThanksYou may try as below:
create proc as below: (This will create the proc in USA database.
use USA
Go
create procedure dbo.creatingPillar as
begin
IF EXISTS (SELECT * FROM sys.objects WHERE object_id =
OBJECT_ID(N'pillarretail') AND type in (N'U'))
DROP TABLE PillarRetail
create table PillarRetail (anid int , ancore varchar(20), period dec(18,6), settlem datetime2,
settlementper int, asf dec(18,6),astlf dec (18,6), tot dec (18,6) )
insert into PillarRetail select anid, ancore, Period, settlem, settlementper,
asf, astlf, tot from createsemipillar2011
insert into PillarRetail select select anid, ancore, Period, settlem, settlementper,
asf, astlf, tot createsemipillar
end
Then you can call as below:
Exec USA.dbo.creatingPillar -
SELECT statement in stored procedure
I want to be able to execute a simple SELECT statement from within a stored procedure and return a stream of data which consists of all the rows from the SELECT statement.
Ultimately, I want to ouput this stream of data to a Crystal Report.
Apparently, Oracle will not allow you to execute a simple SELECT statement from within a stored procedure. It will only allow execution of a SELECT INTO statment, which requires that you define a cursor, etc. etc.
Any way around this?
Thanks
BillLook into REF CURSORs. Still not sure about whether APEX uses them though.... Good luck.
-
UCCX 9.0 using CUIC - Accessing Custom Stored Procedure
Hello,
We recently upgraded to UCCX 9.0 to take advantage of CUIC. So far it's been going well but we had a custom report that we used on the old HR client. It was a copy of the Agent Call Summary report that called a different stored procedure. It was easy enough to create for the HR client. The only difference between the SP's is that the custom one doesn't count outbound calls under 5 digits so it excludeds any internal calls. I've tried exporting the Agent Call Summary report in CUIC and importing a new one but I can't get it to change what SP is called. Does anyone have any idea how I can get this to work? I've seen other threads that suggest that a stand alone copy of CUIC Premium is needed but I was hoping it wasn't since it's a fairly simple change.
Thanks,
EricHello,
Any change to the storeprocedure or database access will require the premium version since the standard doesn't have the option to see the report definition.
If you have a LAB of CUIC, it comes with premium version for partners with only 1000 USD, you can use it and export the report and then import it to the customer, this how i usually do for small changes, other than that the customer is required to buy the premium version.
Amer -
XmlAgg Order By in SQL Statement vs Stored Procedure - 9.2.0.3.0
Hi All,
I'm having a problem with the XMLAgg function's ORDER BY clause after upgrading to 9.2.0.3.0.
I'm finding that I can succesfully execute a SQL statement with the XMLAgg ORDER BY clause, but cannot compile a stored procedure using ORDER BY. Below are two examples executing against the SCOTT Schema. They use the same code, except one is contained in a procedure.
I'm running 9.2.0.3.0 on Windows XP Pro, SP1
Plain SQL Statement (executes correctly):
SELECT
XMLElement("test",
XMLAgg(
XMLElement("element",
XMLAttributes(Scott.Emp.Ename as "ename", Scott.Emp.Empno as "enum")
) --xmlElement element
ORDER BY Scott.Emp.Ename desc
) --XmlAgg
).getClobVal() --xmlElement test
as TestXML
from Scott.Emp;
Stored Procedure:
create or replace procedure zorder(TestXML OUT clob) is
begin
SELECT
XMLElement("test",
XMLAgg(
XMLElement("element",
XMLAttributes(Scott.Emp.Ename as "ename", Scott.Emp.Empno as "enum")
) --xmlElement element
ORDER BY Scott.Emp.Ename desc
) --XmlAgg
).getClobVal() --xmlElement test
into TestXML
from Scott.Emp;
end zorder;
I get the following errors when attempting to compile the stored procedure:
7 PLS-00306: wrong number of types or arguments in call to 'XMLAGG'
7 PL/SQL: ORA-00904: "XMLAGG": invalid identifier
5 PL/SQL: SQL Statement ignored
Does anybody know why this code executes correctly in a SQL statement, but fails in a procedure? Is the Order By clause not available in a procedure? I need to get this functionality working in the procedure.
Thanks,
BrianA good simple workaround (that doesn't require runtime parsing) is to simply sub-query:
SELECT
XMLElement("test",
    XMLAgg(
        XMLElement(
            "element",
            XMLAttributes(Scott.Emp.Ename as "ename", Scott.Emp.Empno as "enum")
        ) --xmlElement element
    ) --XmlAgg
).getClobVal() --xmlElement test
into TestXML
from (
    SELECT Ename,Empno
    FROM Scott.Emp
    ORDER BY Scott.Emp.Ename desc -
Create table statement in stored procedures
Hi,
I am including the following statement in a stored procedure and receive the following error:
STATEMENT:
create table wrk_date (date date,
qty smallint)
tablespace temptabs;
ERROR:
PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:
begin declare exit for goto if loop mod null pragma raise
return select update while <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
close current delete fetch lock insert open rollback
savepoint set sql commit <a single-quoted SQL string>
Is this statement not allowed or is there a possible syntax error above?
I am using Oracle database version 8.0.5.
Thanks.
nullJimmy - You might want to buy the Following book from Oreilley Press Oracle Built in Pacakages it's very very good and came in handy when I has to write dynamic SQL (DBMS_SQL package).
You might want to try www.bookpool.com which has technical books cheaper than amazon.
Hope this helps.
_Satish
Oracle 8 Certified D.B.A.
Sun Certified Sysadmin -
Prepared Statement and Stored Procedure difference?
For SQL in my web applications I use PreparedStatement object alot in my JDBC working with Oracle 9i.
I also heard the term Stored Procedure. What is the difference between Stored Procedure and Prepared Statement?I am new to java programming, can anybody explain
what exactly precompiled means
Thank you
PalspaceWhat does you subject line have to do with your question?
The difference between a stored proc and a prepared statement is mainly this.
A stored proc is stored in the database server permanently and can be used and re-used from a different connections.
A PreparedStatement is not valid across connections.
Stored procs are almost always precompiled. (I am just hedging a bit here just in case but you can consider it 100%)
PreparedStatements MAY be precompiled. Hard to say.
Precompiling means at least one of and perhaps all of the following depending on the database
- The parsing of the SQL statement
- The checking of the SQL against the data dictionary to ensure that the tables and columns referenced actually exist
- The preparation of a query plan
Last but not least Stored procedures may (and often do) contain more than simple queries but are in fact relatively complex programs written in a DB specific language (an offshoot of SQL of some sort). -
Collable statement and stored procedure problem
Hi,
I am using a collable statement to execute a stored procedure.
The stored procedure is a bit complex since it uses a function to retrieve sequence nextval (newId),
than insert a row and returns the newId.
Anyway, I checked it with sql plus and it works, but when trying to execute it from my Java code, I get the following error message:
java.sql.SQLException: ORA-06550: line 1, column 33:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
. ( ) , * @ % & | = - + < / > at in mod not range rem => ..
<an exponent (**)> <> or != or ~= >= <= <> and or like
between is null is not || indicator is dangling
The symbol ")" was substituted for ";" to continue.
Anyone can help?
Thanks,
MichalMy PL/SQL code:
CREATE OR REPLACE FUNCTION getNotifSeq RETURN NUMBER
IS
newId NUMBER;
BEGIN
SELECT NOTIFICATION_SEQ.NEXTVAL INTO newId FROM DUAL;
RETURN newId;
END;
run
CREATE OR REPLACE PROCEDURE insertNotifMsg (
sentBy IN VARCHAR,
subject IN VARCHAR,
msg IN VARCHAR,
newId OUT NUMBER)
IS
BEGIN
newId := getNotifSeq;
INSERT INTO NOTIFICATIONS(NOTIF_ID,SENT_BY,SUBJECT,MSG)
VALUES(newId,sentBy,subject,msg);
END insertNotifMsg;
run;
My Java code to call the procedure:
try
conn = myDBconn.getConnection();
CallableStatement callStmt = conn.prepareCall("{call insertNotifMsg(?,?,?,?}");
callStmt.setString(1,notif.getSentBy());
callStmt.setString(2,notif.getSubject());
callStmt.setString(3,notif.getMsg());
callStmt.registerOutParameter(4, Types.INTEGER);
ResultSet rs = callStmt.executeQuery();
if (!rs.next()) {
throw new SQLException("ERROR: Notification was not inserted into database!");
long newId = callStmt.getInt(4);
callStmt.close();
notif.setNotifId(newId);
conn.commit();
callStmt.close();
finally
myDBconn.closeConnection(conn);
Thanks,
Michal -
How to convert simple SQL Select statements into Stored Procedures?
Hi,
How can I convert following SELECT statement into a Stored Procedure?
SELECT a.empno, b.deptno
FROM emp a, dept b
WHERE a.deptno=b.deptno;
Thanking in advance.
Wajidstored procedure is nothing but a named PL/SQL block
so you can do it like this see below example
SQL> create or replace procedure emp_details is
2 cursor c1 is SELECT a.empno, b.deptno
3 FROM scott.emp a, scott.dept b
4 WHERE a.deptno=b.deptno;
5 begin for c2 in c1
6 LOOP
7 dbms_output.put_line('name is '||c2.empno);
8 dbms_output.put_line('deptno is ' ||c2.deptno);
9 END LOOP;
10 END;
11 /
Procedure created.and to call it use like below
SQL> begin
2 emp_details;
3 end;
4 /
PL/SQL procedure successfully completed.
SQL> set serveroutput on;
SQL> /
empno is 7839
deptno is 10
empno is 7698
deptno is 30
empno is 7782
deptno is 10
empno is 7566
deptno is 20
empno is 7654
deptno is 30
empno is 7499
deptno is 30
empno is 7844
deptno is 30
empno is 7900
deptno is 30
empno is 7521
deptno is 30
empno is 7902
deptno is 20
empno is 7369
deptno is 20
empno is 7788
deptno is 20
empno is 7876
deptno is 20
empno is 7934
deptno is 10Edited by: Qwerty on Sep 17, 2009 8:37 PM -
Sp_executesql vs transaction statement within Stored Procedure
Experts,
Any difference between sp_executesql vs Transaction/Commit statement or try/catch block within Stored Procedure?
What is the best practice to use and why?
Thank You
Regards,
Kumar
Please do let us know your feedback. Thank You - KG, MCTSYour question is a bit strange. sp_executesql is used for dynamic SQL. Unless the problem demands dynamic SQL and, therefore, sp_executesql, there is no need to use it.
For a single statement I would not use transaction and try/catch. For multiple statements you do need to use transaction. It's up to you if you want to use try/catch in the SP or not and trap the error in the client application.
For every expert, there is an equal and opposite expert. - Becker's Law
My blog
My TechNet articles
Maybe you are looking for
-
How to create a domain for a non-root user using the JES installer
Some questions have been circulating on what are the steps to create a whole domain configuration using a non-root user. Here is one method that you can try.... - Login as user "testuser", all operations are using the user I want to start the whole d
-
No sound in playback since moving to 10.5
I have tried playing back movies created in iMovie 6 HD under 10.3. They had sound then. I now have no sound when I play the movies. The audio lines are checked. itunes works fine. It seems that I'm missing some minor detail. Thanks Ken
-
What's the error??
Hello All, Could some one tell me what is the error in this procedure? CREATE OR REPLACE PROCEDURE STUDENTS_COUNT AS TOT NUMBER; BEGIN TOT:=0; SELECT COUNT(*) INTO TOT FROM STUDENTS; CASE WHEN TOT = 0 THEN RAISE_APPLICATION_ERROR(-20001,'SORRY!NO STU
-
Need some documentation on CRM middleware
Hi, I need some really good documentation on middleware, can anyone please give me some links. Regards, Kamesh Bathla
-
Recover oracle 9i without backup
we have HP for network mointoring and at backend we using 9i oracle ok and whats happened actully we have OS cluster here and the cluster machine was down from last 4 month i boot it after booting... mine oracle 9i crashed on oracle9i there was 2 dat