I have strange problem, which I cannot find solution...
I am trying to check how syntax in Oracle works, just to get started, but I can't get even started, because I get:
Incorrect syntax near the keyword 'declare'.
This is what I am trying to execute:
declare log varchar(100);
begin
log := 'abc';
log := log || LENGTH(log);
dbms_output.put_line(log);
end;
Also tried
declare log varchar(100) := '';
None worked... What is the right syntax?
EDIT
Worth mentioning: I am connected to SQL Server engine.
Related
I'm currently trying to implement a bash script that runs at the end of the day that runs a simple oracle query. The command works just fine in Oracle but when inside a .sql file it does not run.
I've attempted to put all of the code on one line and adding semicolons.
Contents of batch file (with user/pass altered):
sqlplus username/password#database #set_changed.sql
Contents of set_changed.sql file:
UPDATE ris_web a
SET a.changed = 0
where exists
(
select modified_date from invn_sbs b
where b.item_sid = a.item_sid
and b.modified_date <= sysdate-1
);
COMMIT;
END;
In SQLPlus you should use a / on a separate line to terminate an SQL statement or PL/SQL block instead of using a semicolon, so change your file to
UPDATE ris_web a
SET a.changed = 0
where exists
(
select modified_date from invn_sbs b
where b.item_sid = a.item_sid
and b.modified_date <= sysdate-1
)
/
SHOW ERRORS
/
COMMIT
/
SHOW ERRORS
/
END
You might also want to have a look at this question
You have end; command at the end of the script but no BEGIN for it.
Simply replace the END; with /
The original syntax was correct. There were uncommitted changes in sqldeveloper that were causing the .sql file to never finish running. Thank you everyone for your help.
This question already has answers here:
Calling another PL/SQL procedure within a procedure
(2 answers)
Closed 4 years ago.
I have looked it up already but couldn't find the right solution for my question. I have many Procedures which copy tables from one database Schema called 'source' into another Schema on the same database called 'target'. The Procedures themself work if I execute them individually. Now I want to execute them with only one command. The problem is I don't know how. I want to do that because there are many procedures and it is annoying to execute them all individually. It would be nice if someone could help me out. Thanks in advance :)
My approach (which is obviously wrong) was:
CREATE OR REPLACE PROCEDURE COPY_TABLES
IS
BEGIN
EXEC COPY_EAKTEPERSON;
EXEC COPY_EANDEREANSRPUECHE;
EXEC COPY_EANDEREBHBERECHT;
EXEC COPY_EBEIHILFEBEMSATZ;
EXEC COPY_EBESCHAEFTIGUNG;
EXEC COPY_EDIENSTSTELLE;
EXEC COPY_EEIGENBEHALT;
EXEC COPY_EPFLEGEVERS;
EXEC COPY_ESEHSCHAERFE;
EXEC COPY_EVERSLEISTUNG;
EXEC COPY_EWOHNISITZ;
EXEC COPY_EPERSON;
END;
A procedure calling another procedure.
create or replace procedure COPY_TABLES(
ret out varchar2) as
error varchar2(1000);
begin
COPY_EAKTEPERSON();
COPY_EANDEREANSRPUECHE();
COPY_EANDEREBHBERECHT();
COPY_EBEIHILFEBEMSATZ();
COPY_EBESCHAEFTIGUNG();
COPY_EDIENSTSTELLE();
COPY_EEIGENBEHALT();
COPY_EPFLEGEVERS();
COPY_ESEHSCHAERFE();
COPY_EVERSLEISTUNG();
COPY_EWOHNISITZ();
COPY_EPERSON();
ret := ''
return;
exception
when others then
error_info := sqlerrm;
ret := error_info;
end COPY_TABLES;
The breakpoints not match the code line when debug in SQL Server 2008 R2. Sometimes when hit the breakpoints,but not executing the right code line.How to fix it?
declare #SharedTable table(Num int,Name nvarchar(50))
Insert into #SharedTable(Num,Name) values('27149','Vader')
declare #numShared int=0;
select #numShared = COUNT(Num) from #SharedTable
declare #idx_Shared int =1
while #idx_Shared<=#numShared
begin
declare #Num int;declare #Name nvarchar(50)
select #Name=Name,#Num=Num from #SharedTable
declare #AllSharers nvarchar(max)=''
set #AllSharers+=('Num:'+Cast(#Num as nvarchar)+' '+'Name:'+#Name+CHAR(10))
set #idx_Shared+=1
end
print #AllSharers;
Very simple code copy from my query.
Every time the debug pointer step to line select #Name=Name,#Num=Num from #SharedTable,it'll ignore the code below in the 'while' loop, and from then on all the code below and breakpoints begin to mismatch.
Copy it to a new query,the problems still are there,but typed the content the same in a new query,the problem gone.
SQL Server will report the line of code a given statement started on when reporting an error or hitting a breakpoint. Thus, if you have something like this:
SELECT *
FROM [tableName]
WHERE 1 = 'text'
If you place the breakpoint on line 3, SQL Server is going to break at line 1 (and report that the error occurred on line 1 when you continue), because the statement did, in fact, start on line 1, and whitespace is irrelevant in SQL.
What is the problem with this package as it is giving an error?
CREATE OR REPLACE PACKAGE PKG_SHOW_CUST_DETAILS
AS
PROCEDURE SHOW_CUST_DETAILS( myArg VARCHAR2);
END PKG_SHOW_CUST_DETAILS;
CREATE OR REPLACE PACKAGE BODY PKG_SHOW_CUST_DETAILS
AS
PROCEDURE SHOW_CUST_DETAILS(myArg VARCHAR2)
IS
BEGIN
DBMS_OUTPUT.PUT_LINE(myArg);
END SHOW_CUST_DETAILS;
END PKG_SHOW_CUST_DETAILS;
/
On compilation of the above script, I am getting the following errors:
SQL> show errors
Errors for PACKAGE PKG_SHOW_CUST_DETAILS:
LINE/COL ERROR
-------- -----------------------------------------------------------------
6/1 PLS-00103: Encountered the symbol "CREATE"
The package is very simple and I am not able to compile it. I searched earlier answers on this error message and none of them did solve my problem.
I am consistently getting this error for 2 more packages and I am stuck on this error message no matter what I do. I even tried to strip everything to the barest minimum as shown above, but the error message does not seem to go away.
BTW I am executing this on command line SQL plus session after logging into my Oracle 11G database.
YES- SET SERVEROUTPUT ON -- is executed and the error message has nothing to do with this command.
What am I missing?
At line 5 there is a / missing.
There is a good answer on the differences between ; and / here.
Basically, when running a CREATE block via script, you need to use / to let SQLPlus know when the block ends, since a PL/SQL block can contain many instances of ;.
For me / had to be in a new line.
For example
create type emp_t;/
didn't work
but
create type emp_t;
/
worked.
In my case EXECUTE IMMEDIATE ('CREATE TABLE ...') works, for example:
DECLARE
myVar INT;
BEGIN
SELECT 2 INTO myVar FROM dual;
IF myVar > 1 THEN
EXECUTE IMMEDIATE('Create Global Temporary Table TestTemp ( id VARCHAR2(2) ) ON COMMIT PRESERVE ROWS');
END IF;
END;
Reference to Create Table As within PL/SQL?
Run package declaration and body separately.
I'm trying to update a package in Oracle, coming from SQL Server this has been confusing.
I have written a batch file that runs the .spec file first and the .body file second, but even running it manually does not work.
I use this syntax:
sqlplus username/password#databasename #c:\temp\myfile.spec
sqlplus username/password#databasename #c:\temp\myfile.body
When I go back to Sql Developer I can look at the stored procedures in the package and see that they have not been updated.
Why aren't my packages getting updated?
The spec and body files need to have / make SQL*Plus create/replace the object.
Without the /:
CREATE OR REPLACE PACKAGE TEST12_13 AS
PROCEDURE TEST12_13;
END;
STAGE#DB>#C:\TEST.PKS
6
With the /:
CREATE OR REPLACE PACKAGE TEST12_13 AS
PROCEDURE TEST12_13;
END;
/
STAGE#DB>#C:\TEST.PKS
Package created.
In reply to your comment about passing filename as parameter, instead of passing the filename as parameter, have SQL*Plus ask you for the filename
wrapper.sql
ACCEPT filename_var Prompt 'Enter filename'
#c:\temp\&filename_var
/
#c:\temp\&filename_var
/
Connect to SQL*Plus with
sqlplus username/password#databasename
Then run the script from the SQL*Plus prompt:
set echo on
#c:\temp\myfile.spec
You should be able to see whats going on like this, including any error messages.