DDL Create Trigger script fails - firebird

A DDL script to create a trigger (source below) fails with 2 errors:
Statement failed, SQLSTATE = 42000
Dynamic SQL Error
-SQL error code = -104
-Unexpected end of command - line 3, column 44
After line 0 in file C:\CRMDemo\Database\DDL\Trigger_Orders.sql
Statement failed, SQLSTATE = 42000
Dynamic SQL Error
-SQL error code = -104
-Token unknown - line 1, column 1
-end
At line 14 in file C:\CRMDemo\Database\DDL\Trigger_Orders.sql
(line 3, column 44 looks like it may be the closing parthesis).
I can't find any information about errors 42000 or -104.
The trigger is designed to assign a record number from a generator, which does exist. This trigger works properly in Interbase from the same script.
The only thing I can think of is that the column size, Integer, is incorrect for the value returned. But the documentation says the value may be truncated but should work for the expected value (1).
CREATE TRIGGER ORDERS_GENERATE_KEY FOR ORDERS ACTIVE BEFORE INSERT POSITION 95 AS
begin
NEW.ORDER_NR = GEN_ID(NEW_ORDER_NUMBER, 1);
end;
Firebird is ver 2.5.2, just downloaded. Windows 7. Database should be 32bit.

If you run your statement using isql utility take sure that SET TERM operators are used:
SET TERM ^ ;
CREATE TRIGGER ORDERS_GENERATE_KEY FOR ORDERS
ACTIVE
BEFORE INSERT
POSITION 95
AS
begin
NEW.ORDER_NR = GEN_ID(NEW_ORDER_NUMBER, 1);
end
^
SET TERM ; ^

Related

Why am I getting an error near semi colon (:)?

I have no idea but this is throwing an error i think it is on line "set #al = 2020". I have read the documentation and not seeming to translate over.
begin
declare #al int;
set #al = 2020;
exec dbo.get_egus_trdg_desk_rpt_no_clrg_broker(#al int);
end
Error message:
SQL Error [102] [42000]: Incorrect syntax near ';'.
You do not need to use ';' after every line of code. I can successfully generate the following script in SQL Server 2019.
Sample Output

Select query in FOR loop does not works in Netezza

I am trying to run a simple plsql in Netezza, to run a loop on a result set of select query,
here is the below code I am trying to run
CREATE OR REPLACE PROCEDURE UPDATE_SUSPECT_GROUP_ID()
RETURNS CHARACTER VARYING(ANY) EXECUTE AS CALLER
LANGUAGE NZPLSQL AS
BEGIN_PROC
DECLARE
VAL BIGINT := 100000000000000;
BEGIN
RAISE NOTICE 'lets start';
FOR r IN
select
suspect_id as suspect_id
from apollo_customer_analysis.ASHKUMAR.REFINED_SUSPECT
LOOP
RAISE NOTICE 'val: %',r.suspect_id;
END LOOP;
END;
END_PROC;
EXECUTE UPDATE_SUSPECT_GROUP_ID();
When try to run the code, I get the below error
ERROR [01000] NOTICE: plpgsql: ERROR during compile of UPDATE_SUSPECT_GROUP_ID near line 15
ERROR [01000] NOTICE: line 1 at execute statement
ERROR [HY000] ERROR: syntax error, unexpected ';', expecting LOOP at or near ";"
Can someone help me how this error could be resolved?

Does perl DBI support command like this #file_name?

I would like to run commands which is placed in 1.sql file This is my code:
$sth=$dbh->do( q { #1.sql } );
But this code isn't working:
There is output:
DBI::db=HASH(0xf18edc0) trace level set to 0x0/2 (DBI # 0x0/0) in DBI 1.634-ithread (pid 10389)
-> do in DBD::_::db for DBD::Oracle::db (DBI::db=HASH(0xf18ee50)~0xf18edc0 ' #1.sql ') thr#eebf010
dbd_st_execute (STMT TYPE 0) (out0, lob0)...
Statement Execute Mode is 32 (COMMIT_ON_SUCCESS)
-> DESTROY for DBD::Oracle::st (DBI::st=HASH(0xf18f190)~INNER) thr#eebf010
ERROR: 900 'ORA-00900: invalid SQL statement (DBD ERROR: error possibly near <*> indicator at char 1 in ' <*>#1.sql ')' (err#1)
<- DESTROY= undef at run.pl line 12
!! ERROR: 900 'ORA-00900: invalid SQL statement (DBD ERROR: error possibly near <*> indicator at char 1 in ' <*>#1.sql ')' (err#0)
<- do= undef at run.pl line 12
DBD::Oracle::db do failed: ORA-00900: invalid SQL statement (DBD ERROR: error possibly near <*> indicator at char 1 in ' <*>#1.sql ') [for Statement " #1.sql "] at run.pl line 12.
DBD::Oracle::db do failed: ORA-00900: invalid SQL statement (DBD ERROR: error possibly near <*> indicator at char 1 in ' <*>#1.sql ') [for Statement " #1.sql "] at run.pl line 12.
! -> DESTROY for DBD::Oracle::db (DBI::db=HASH(0xf18edc0)~INNER) thr#eebf010
ERROR: 900 'ORA-00900: invalid SQL statement (DBD ERROR: error possibly near <*> indicator at char 1 in ' <*>#1.sql ')' (err#0)
! <- DESTROY= undef during global destruction
No, that won't work (obviously). Some DBI drivers support multiple statements (MySQL and SQL Server via ODBC come to mind), but I don't think Oracle does. At any rate, you'd still have to handle the file reading part yourself. As you can see in the DBI docs, do() is expecting a valid SQL statement, which the string #1.sql is not.
Your basic options for executing an SQL file from Perl are:
Split the SQL file yourself and then execute each statement.
If you have control of the file and know it won't contain anything tricky, this is a fairly easy task. It could be as easy as splitting the file based on a semicolon character, or you might want to add some basic stripping of comments first.
However, this is quite hard to do 100% correctly for the full range of possible file content (what if an SQL statement has a string containing a semicolon? What if there are nested sets of comments? etc.). This requires fully parsing the file--and I'm not aware of any tool that allows you to do this easily. There is an SQL::Parse module, but I don't think it handles multiple statements.
Call an external program from Perl to do it for you.
For example, call the Oracle command line client. This is easy, but you don't have granular control of the statements being executed.

DB2 trigger illegal token

Fairly new to DB2 sql, so forgive my ignorance :)
I have a trigger with a condition inside. I want to then insert some params depending on the condition.. Here it is:
I've looked at DB2 documentation for triggers and also for if statements, and at least to my eyes it appears to comply with it, however i get a -104 error (Illegal symbol token) on the insert line.
The insert works fine provided i use values not from 'N'.
OK, it works if i have nothing in the if then statement.. but only if i have nothing!
Thanks
My guess would be that DB2 is confused by your statement terminators. You use a semicolon to terminate the CREATE TRIGGER statement, but at the same time you use the same terminator inside the CREATE TRIGGER statement, after the INSERT.
In whatever client you use to execute your code, redefine the statement terminator to something else and place that terminator at the end of CREATE TRIGGER, after END ID. For example, if you use the command line processor, save this to a file trig.sql:
CREATE OR REPLACE TRIGGER AUTO_INSERT_DEPO_NOMINEE
AFTER INSERT ON CA_ENTITLEMENT
REFERENCING NEW AS N
FOR EACH ROW
IF NOT EXISTS (SELECT D.DEPO_CD FROM DEPO D WHERE D.DEPO_CD = N.DEPO_CD)
THEN
INSERT INTO DEPO (DEPO_CD, DEPO_NME, BRANCH_CD, AUTO_GENERATED)
VALUES(N.DEPO_CD, NULL, N.BRANCH_CD, 'Y');
END IF#
then run it, specifying "#" as the statement terminator:
db2 -td# -f mytrig.sql

SQL Server Management Studio Transaction and Variable

In SSMS 2012, after setting Options > Query Execution > ANSI > SET IMPLICIT_TRANSACTIONS, cf this SO post
I have the following code in a query window:
begin transaction
select ##TRANCOUNT
begin
declare #someNumber int; set #someNumber = 1;
print #someNumber;
end
rollback
When I select the whole block and press Execute, I see the expected result, i.e. 1.
However, when I select the first 4 lines and execute, then select line 5, i.e. print #someNumber;, I got the following message:
Msg 137, Level 15, State 2, Line 1
Must declare the scalar variable "#someNumber".
What is exactly the scope of the variable?
I'm baffled. Can someone shed any light or point me to the right direction please?
The variable is scoped per batch.
The scope of a local variable is the batch in which it is declared.
Each press of "Execute" is a batch. So, for the 2nd run, it isn't declared
What are doing with 2 runs is this
--start of batch 1
begin transaction
select ##TRANCOUNT
begin
declare #someNumber int; set #someNumber = 1;
--end of batch 1
GO --separate batch after this
--start of batch 2
print #someNumber;
end
rollback
--end of batch 2
In SSMS, a variable lives for the duration of the execution.
If you execute the declaration only, it dies when the execution ends.
If you then execute the print statement of the variable, it was not declared within this execution so it does not exists.