Flyway migration throwing Validation error for Sybase GOTO label by splitting the keyword in separate 'GO' and 'to' - flyway

I'm trying to run flyway (6.3.3) migration in SYbase ASE on the sql below but getting a validation error.
declare #count smallint
select #count = 1
restart:
print 'yes'
select #count = #count + 1
while #count <= 4
goto restart
Flyway response is
Migration R__wes.sql failed
---------------------------
SQL State : 37000
Error Code : 102
Message : Incorrect syntax near '4'.
Line : 1
Statement : declare #count smallint
select #count = 1
restart:
print 'yes'
select #count = #count + 1
while #count <= 4
The issue is that FLyway is breaking the goto keyword to
GO
to
which results in the error.
i.e.
declare #count smallint
select #count = 1
restart:
print 'yes'
select #count = #count + 1
while #count <= 4
GO
to restart
I found a similar issue on SQL Server that was resolved last year
https://github.com/flyway/flyway/issues/2307
Thanks in advance!

I created a pull request for a possible fix - https://github.com/flyway/flyway/pull/2775

Related

ORA-00932: inconsistent datatypes: expected DATE got NUMBER

I am using Oracle 11g and running the following query in oracle toad:
WITH
CTEDevices (CREATED_ON, CUSTOMER_INFO_ID, DEVICE_MAKE, DEVICE_Model) AS (
SELECT
Trunc(RD.CREATED_ON),
RD.CUSTOMER_INFO_ID,
RD.DEVICE_MAKE,
RD.DEVICE_Model
FROM Schema.Table1 RD
WHERE RD.PARAM_CHANNEL_ID = 1
AND RD.CREATED_ON >= '01-may-2022'
AND RD.CREATED_ON <= '02-may-2022'
GROUP BY
Trunc(RD.CREATED_ON),
RD.CUSTOMER_INFO_ID,
RD.DEVICE_MAKE,
RD.DEVICE_Model
),
CTERegistration (CREATED_ON, CUSTOMER_INFO_ID, DEVICE_MAKE, DEVICE_Model) AS
(
SELECT
CI.Customer_Info_Id,
Trunc(CI.created_on),
'CI.DEVICE_MAKE',
'CI.DEVICE_Model'
FROM Schema.Table2 CI
Where CI.CUSTOMER_TYPE = 'A'
AND CI.CREATED_ON >= '01-may-2022'
AND CI.CREATED_ON <= '02-may-2022'
)
SELECT
CTEDevices.CREATED_ON, CTEDevices.CUSTOMER_INFO_ID, CTEDevices.DEVICE_MAKE, CTEDevices.DEVICE_Model,
CTERegistration.CREATED_ON, CTERegistration.CUSTOMER_INFO_ID, CTERegistration.DEVICE_MAKE, CTERegistration.DEVICE_Model
FROM CTEDevices INNER JOIN CTERegistration ON (CTEDevices.CUSTOMER_INFO_ID = CTERegistration.CUSTOMER_INFO_ID AND (CTEDevices.CREATED_ON = CTERegistration.CREATED_ON));
Individual qeueries were running successfully but when going to run as combined one getting the following error:
ORA-00932: inconsistent datatypes: expected DATE got NUMBER
Please help.
Thanks
Culprit is the 2nd CTE:
CTERegistration (CREATED_ON, CUSTOMER_INFO_ID, DEVICE_MAKE, DEVICE_Model) AS
( 1st 2nd
SELECT
CI.Customer_Info_Id, 1st
Trunc(CI.created_on), 2nd
'CI.DEVICE_MAKE', ...
The 1st column is supposed to be CREATED_ON (which is, apparently, DATE datatype value), while the 2nd column is then CUSTOMER_INFO_ID. Your query has it just the opposite, so - fix it:
CTERegistration (CREATED_ON, CUSTOMER_INFO_ID, DEVICE_MAKE, DEVICE_Model) AS
(
SELECT
Trunc(CI.created_on),
CI.Customer_Info_Id,
'CI.DEVICE_MAKE', ...
If you wonder why separate queries work, well - no reason why not. Oracle doesn't care how you named columns, but you can't join them any way you want.

"SQL Error (1292): Truncated incorrect DOUBLE value: 'unknown'" upon INSERT but not when only SELECTing

Can someone please help me understand why below INSERT query
INSERT INTO ha_archive.pond(last_updated, water)
SELECT DATE(s.last_updated) 'last_updated',
SUM(s.state) 'water'
FROM home_assistant.states s
WHERE s.entity_id = 'sensor.pond_last_refill' AND s.state > 0
GROUP BY DATE(s.last_updated)
ON DUPLICATE KEY update
water = VALUES(water);
triggers
SQL Error (1292): Truncated incorrect DOUBLE value: 'unknown'
While the SELECT query below does not?
SELECT DATE(s.last_updated) 'last_updated',
SUM(s.state) 'water'
FROM home_assistant.states s
WHERE s.entity_id = 'sensor.pond_last_refill' AND s.state > 0
GROUP BY DATE(s.last_updated)
There are occasional 'unknown' strings among float values, and I've tried casting as float, but that doesn't avoid the error.

Use fields of outer query in group by of subquery

My table: CREATE TABLE T(id INT PRIMARY KEY, value INT UNIQUE)
This query, as I considered, would produce a median value of value in the table. But sqlite v.3.9.1 gives me the error no such column: ot.value to the line with group by. And it process line with where successfully, although it uses a similar expression. What's the problem of the query?
select
ot.id,
ot.value
from T as ot
where (
select count(c) > count(DISTINCT c) from (
select count(*) c from T as it
where it.value != ot.value
group by it.value < ot.value
) LessMore
)
The same query succeeds in PostgreSQL and prints what was expected. MySQL gives the error Error Code: 1054. Unknown column 'ot.value' in 'where clause'

Errors in triggers

This is my trigger:
CREATE OR REPLACE TRIGGER trg_CheckStaffID
BEFORE INSERT ON ASSIGN
FOR EACH ROW
BEGIN
DECLARE id integer := 0;
SET id := (select count(*) from (select staffid from staff where staffid ='T2');
IF (id=0) THEN
RAISE_APPLICATION_ERROR(-20000,'Please Enter A Valid Staff ID');
END IF;
END;
/
And this is the error message I get:
PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:
( - + case mod new not null
continue avg count current exists max min prior sql stddev
sum variance execute forall merge time timestamp interval
date
pipe
PLS-00103: Encountered the symbol "IF"
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
end not pragma final instantiable order overriding static
member constructor map
Invalid syntax on different places; DECLARE section should be before BEGIN. There's no SET command in PL/SQL.
Here's code that compiles; whether it does what you meant, can't tell. (I'm creating dummy tables, just to make sure that trigger creation wouldn't fail).
SQL> create table assign (id number);
Table created.
SQL> create table staff (staffid varchar2(2));
Table created.
SQL> create or replace trigger trg_checkstaffid
2 before insert on assign
3 for each row
4 declare
5 id integer := 0;
6 begin
7 select count(*)
8 into id
9 from (select staffid
10 from staff
11 where staffid ='T2');
12
13 if id = 0 then
14 raise_application_error(-20000, 'Please Enter A Valid Staff ID');
15 end if;
16 end;
17 /
Trigger created.
SQL>

how to do you assign an anonymous variable to a column in plsql?

I am trying to assign an input variable into pl/sql.
I want to query the database by letting the user input department_id and outputs the first name, last name, and salary. The salary is second highest salary in the table.
declare
v_dept_id int;
Begin
Select emp1.first_name, emp1.last_name, emp1.salary, emp1.department_id
From employees emp1
Where (1) =
(select count(distinct(emp1.salary))
From employees emp2
Where emp2.salary > emp1.salary) ;
End;
clarification
EDIT: I edited the code to include V_dept_id however it doesnt run
declare
v_dept_id int;
Begin
Select emp1.first_name, emp1.last_name, emp1.salary, emp1.department_id
into v_dept_id
From employees emp1
Where ((1) =
(select count(distinct(emp1.salary))
From employees emp2
Where emp2.salary > emp1.salary)) and v_dept_id = '&Enter_dept_id' ;
End;
error:
Error starting at line : 4 in command -
declare
v_dept_id int;
Begin
Select emp1.first_name, emp1.last_name, emp1.salary, emp1.department_id
into v_dept_id
From employees emp1
Where ((1) =
(select count(distinct(emp1.salary))
From employees emp2
Where emp2.salary > emp1.salary)) and v_dept_id = '&Enter_dept_id' ;
End;
Error report -
ORA-06550: line 6, column 16:
PL/SQL: ORA-00947: not enough values
ORA-06550: line 5, column 1:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Edit2 based on answer below feedback with the error
I don't know how to let the enter a number into the prompt correctly
declare
v_dept_id number;
v_fname varchar(50);
v_lname varchar(50);
v_salary NUMBER(8,2);
Begin
Select emp1.department_id, emp1.first_name, emp1.last_name, emp1.salary
into v_dept_id, v_fname, v_lname, v_salary
From employees emp1
Where ((1) =
(select count(distinct(emp1.salary))
From employees emp2
Where emp2.salary > emp1.salary)) and v_dept_id = '&Enter_dept_id' ;
End;
Error starting at line : 1 in command -
declare
v_dept_id number;
v_fname varchar(50);
v_lname varchar(50);
v_salary NUMBER(8,2);
Begin
Select emp1.department_id, emp1.first_name, emp1.last_name, emp1.salary
into v_dept_id, v_fname, v_lname, v_salary
From employees emp1
Where ((1) =
(select count(distinct(emp1.salary))
From employees emp2
Where emp2.salary > emp1.salary)) and v_dept_id = '&Enter_dept_id' ;
End;
Error report -
ORA-01403: no data found
ORA-06512: at line 8
01403. 00000 - "no data found"
*Cause: No data was found from the objects.
*Action: There was no data from the objects which may be due to end of fetch.
Raj_Te has explained why your code will not compile. But I don't much like your method of retrieving the second highest salary. I guess your method works but it's not clear what that section of code is trying to achieve.
In the following code the DENSE_RANK function will return the salary ranking of each row. Any rows that have the same salary will get the same rank :
SELECT
emp.first_name
,emp.last_name
,emp.salary
,emp.department_id
FROM
(SELECT
emp1.first_name
,emp1.last_name
,emp1.salary
,emp1.department_id
,DENSE_RANK() OVER (PARTITION BY emp1.department_id ORDER BY emp1.salary DESC) dr
FROM
employees emp1
WHERE emp1.department_id = &dept_id
) emp
WHERE 1=1
AND emp.dr = 2
;
By stating emp.dr = 2 we are saying we want all rows where the salary is ranked second highest.
You are selecting 4 fields in a single variable. Thats why its givng not enough values. You must decalre either 4 variables or create a records with 4 columns and then capture the values.
Select emp1.first_name, emp1.last_name, emp1.salary, emp1.department_id
into v_dept_id <--- trying to fetch 4 columns values to a single varibale.
You can decalre new variables of same datatype as of your selection and do it:
Select emp1.first_name, emp1.last_name, emp1.salary, emp1.department_id
into v_emp_first_nm,v_emp_last_nm ,v_emp_sal,v_dept_id

Resources