Error with putting SELECT SUM into an IF statement - sum

I encounter an error with code 1064 in my SQL statement but I cannot see where it is wrong.
I want to sum all the ETCS of all modules that a student have take part in. I have tried and the SELECT SUM statement is correct an return the sum.
However when I put it in an IF statement, error code 1064 is return.
The info on number of ETCS of each module is stored in table module_details.
The info of modules taken by student is stored in table student_module.
IF(SELECT SUM(module_etcs) AS total_etcs
FROM student_module
INNER JOIN module_details
ON student_module.module_id = module_details.module_id
WHERE student_module.student_id = '2222') = 10
THEN
INSERT INTO alumni_details(student_id,alumni_status)
VALUES('2222','S123');
END IF;
The value return by the SELECT SUM is 10.
But put in the IF statement cause error.

Related

Why am I getting the error: "Ambiguous column" in my query?

In this query I inserting records into a new empty table I created. These records are derived from another table where I am left joining that table to itself, in order to output records that are not included in the recent table that is appended on top of an older table. So basically it outputs records that were deleted.
CREATE DEFINER=`definer` PROCEDURE `stored_procedure_name`()
MODIFIES SQL DATA
SQL SECURITY INVOKER
BEGIN
START TRANSACTION;
INSERT INTO exceptions_table (
`insert_date`,
`updated`,
`account_number`,
`id_number`)
SELECT
`insert_date`,
`updated`,
`account_number`,
`id_number`
FROM original_table ot1
LEFT JOIN original_table ot2
ON ot1.`account_number` = vdcaas2.`account_number`
AND ot2.`insert_date` = '2022-12-20'
WHERE ot1.`insert_date` = '2022-12-10'
AND ot2.`account_number` IS NULL;
COMMIT;
END
I get an error stating: "SQL Error: Column "insert_date" in field list is ambiguous.
I'm not sure why because I have specified which table I am grabbing "insert_date" from when INSERTING and when SELECTING and JOINING..
Every row in your query has two columns called insert_date: one from the table you've aliased as "ot1", and one from the table (as it happens, the same table) you've aliased as "ot2".
The database system doesn't know which one you want, so you have to tell it by writing either "ot1.insert_date" or "ot2.insert_date", just as you do elsewhere in the query:
... ot2.`insert_date` = '2022-12-20'
...
... ot1.`insert_date` = '2022-12-10'
The same is true of the other columns you've listed to select.
You need to change this
SELECT
`insert_date`,
`updated`,
`account_number`,
`id_number`
to this
SELECT
ot1.`insert_date`,
ot1.`updated`,
ot1.`account_number`,
ot1.`id_number`
or this
SELECT
ot2.`insert_date`,
ot2.`updated`,
ot2.`account_number`,
ot2.`id_number`
or some combination
Issue
SQL Error: Column "insert_date" in field list is ambiguous error means that the query is trying to reference the "insert_date" column from both tables, ot1 and ot2.
Try the following:
SELECT
ot1.`insert_date`,
ot1.`updated`,
ot1.`account_number`,
ot1.`id_number`
Also, you have a typo in your query:
ON ot1.`account_number` = vdcaas2.`account_number` -> ON ot1.`account_number` = ot2.`account_number`

PgAdmin4 keeps returning: ERROR: syntax error at or near "IN"

I have a query that updates all records inside my table and the values inside of each column should be in sync with my other table so i created this query:
UPDATE dashboard.event SET operation_start_time IN
(SELECT operation_start FROM dashboard.inventory), operation_end_time IN
(SELECT operation_end FROM dashboard.inventory)
WHERE terminal_id IN (SELECT terminal_id FROM dashboard.inventory)
but the problem postgres keep returning me "ERROR: syntax error at or near "IN"" in which i don't get why. If i put "=" instead of "IN" it returns me the error:
ERROR: more than one row returned by a subquery used as an expression
For the logic of these query. I have an inventory table and in it has a column name operation_start and operation_end. I want the data in those columns to be updated or inserted in the event table for each terminal_id
Any help will be appreciated please. Thank you!
If you want to update the columns in event from inventory for a given terminal_id, the the syntax would look like:
UPDATE dashboard.event e
SET operation_start_time = e.operation_start,
operation_end_time = e.operation_end
FROM dashboard.inventory i
WHERE e.terminal_id = i.terminal_id;

Oracle - Error at line 4: PL/SQL: SQL Statement

I am trying to build a trigger for my database and I am getting an error and I suspect it is because of my into clause but I am not getting the reason. I was reading the documentation of it (https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/selectinto_statement.htm) and there is saying:
By default, a SELECT INTO statement must return only one row.
Otherwise, PL/SQL raises the predefined exception TOO_MANY_ROWS and
the values of the variables in the INTO clause are undefined. Make
sure your WHERE clause is specific enough to only match one row
Well, at least I am trying to be sure that my where clause is returning one and just one row. Can you give me some advice about it?
CREATE OR REPLACE TRIGGER t_ticket
INSTEAD OF INSERT ON V_buyTicket FOR EACH ROW
declare ticketID number; busy number; seatRoom number;
BEGIN
select count(a.id_ticket) into busy , s.freeSeats into seatRoom
from assigned a
inner join show e on (a.id_movie= e.id_movie)
inner join rooms s on (e.id_room = s.id_room)
where a.id_session = 1 AND a.id_movie = 1
group by s.freeSeats;
if(busy < seatRoom ) then
ticketID := seq_ticket.NEXTVAL;
insert into tickets(id_ticket, type, number, cc, store) values(ticketID, :new.type, :new.number, :new.cc, :new.store);
insert into assigned (id_ticket, id_movie, id_session) values(ticketID, :new.id_movie, :new.id_session);
else
DBMS_OUTPUT.PUT_LINE('No available seats');
end if;
END;
Don't use group by if you want exactly one row returned:
select count(a.id_ticket), sum(s.freeSeats)
into busy, seatRoom
from assigned a inner join
show e
on a.id_movie = e.id_movie inner join
rooms s
on e.id_room = s.id_room
where a.id_session = 1 and a.id_movie = 1;
An aggregation query without a group by always returns exactly one row. The alternative would be to include an explicit rownum = 1 in the WHERE clause.
My guess is that you conditions are not choosing exactly one room. You might want to check on the logic.

Possible error not shown at SQLite manager

I have a table 'student' in which I have five attributes {id,first,last,age,marks}.
The following are the two tuples inserted.
1,Suresh,Kumar,35,95
2,ramesh,Kapoor,21,90
When I execute the query with mistake
select * from stdent where first='Suresh'
I get the alert message - no such table stdent.Very fair!
But when I execute the following query
select * from student where first='sachin'
I get no alerts or error message even though sachin is not present in any of the tuple.
What is the reason?
It's because the query below is correct. No issues with the query itself. However, no tuple exists with first=sachin. Therefore, nothing is returned.
select * from student where first='sachin'
If you try
select * from student where first='Suresh'
then the query will successfully return 1 row, as a tuple with first=Suresh exists.

No rows returned when calling stored procedure although running code in that very SP returns rows

Whenever I call a stored procedure that contains more than a single select statement I get no rows in the resultset although running the same code outside the SP returns the expected result. This has started happening by the time a database backup from a production environment was installed on the development server.
The example below returns no rows when run in Enterprise Manager 2008:
exec dbo.USP_S_CustomerLimit '00000053-0000-1148-5300-000000000000'
If I however execute all the code in that procedure by itself (like select it and press "run") I get the expected amount of result rows:
SELECT * FROM CustomerLimit WHERE Department =
(SELECT Department FROM Customer WHERE CustomerID = CONVERT(BINARY(16), CONVERT(UNIQUEIDENTIFIER, '00000053-0000-1148-5300-000000000000')))
I have also tried this variant of the code which unfortunately behaves in exactly the same way:
SELECT CustomerLimit.* FROM CustomerLimit
JOIN Customer ON Customer.Department = CustomerLimit.Department
WHERE Customer.CustomerID = CONVERT(BINARY(16), CONVERT(UNIQUEIDENTIFIER, '00000053-0000-1148-5300-000000000000'))
I feel it's some sort of right issue but I don't know where to check. I am db_owner on the server and I also belong to a group that is db_owner in the database in question. What could be the cause of this problem? If I create a testprocedure with just one select, f.e Select * from Customer then I get the correct result when executing that procedure but if I write f.e Select * from Customer where CustomerID in (select CustomerID from Customer) then I get no rows.. :(
Check whether you have rights/permission on all these three tables. Check the sys.objects for the listing of these tables.