Trigger after insert with condition in oracle - if-statement

I have an error with my trigger. it show
LINE/COL ERROR
-------- --------------------------------------------
2/2 PL/SQL: SQL Statement ignored
2/6 PL/SQL: ORA-00922: missing or invalid option
I want this trigger run if the customertype is member.
Here are my table
TABLE CUSTOMER
CREATE TABLE CUSTOMER
(CUSTOMERID VARCHAR2(100) primary key,
CUSTOMERNAME VARCHAR2(50),
CUSTOMERADDRESS VARCHAR2(100),
CUSTOMERPHONE VARCHAR2(15),
CUSTOMEREMAIL VARCHAR2(50),
CUSTOMERTYPE VARCHAR2(15)
)
TABLE MEMBER
CREATE TABLE MEMBER
(MEMBERID VARCHAR2(100),
USERNAME VARCHAR2(25),
PASSWORD VARCHAR2(10),
CUSTOMERID VARCHAR2(100),
CONSTRAINT FK_Member Foreign Key (CustomerId)
REFERENCES Customer(CustomerId)
);
This is my trigger
CREATE or replace TRIGGER insertMember
after insert ON CUSTOMER
for each row
BEGIN
SET NOCOUNT ON
If (select customertype from customer) = 'member'
begin
INSERT INTO MEMBER (customerid ) values
(:new.customerid);
END insertMember;
/

Related

how to insert/update data in sql database using azure databricks notebook jdbc

I got lots of example to append/overwrite table in sql from AZ Databricks Notebook. But no single way to directly update, insert data using query or otherway.
ex. I want to update all row where (identity column)ID = 1143, so steps which I need to taken care are
val srMaster = "(SELECT ID, userid,statusid,bloburl,changedby FROM SRMaster WHERE ID = 1143) srMaster"
val srMasterTable = spark.read.jdbc(url=jdbcUrl, table=srMaster,
properties=connectionProperties)
srMasterTable.createOrReplaceTempView("srMasterTable")
val srMasterTableUpdated = spark.sql("SELECT userid,statusid,bloburl,140 AS changedby FROM srMasterTable")
import org.apache.spark.sql.SaveMode
srMasterTableUpdated.write.mode(SaveMode.Overwrite)
.jdbc(jdbcUrl, "[dbo].[SRMaster]", connectionProperties)
Is there any other sufficient way to achieve the same.
Note : Above code is also not working as SQLServerException: Could not drop object 'dbo.SRMaster' because it is referenced by a FOREIGN KEY constraint. , so it look like it drop table and recreate...not at all the solution.
You can use insert using a FROM statement.
Example: update values from another table in this table where a column matches.
INSERT INTO srMaster
FROM srMasterTable SELECT userid,statusid,bloburl,140 WHERE ID = 1143;
or
insert new values to rows where one of the existing column value matches
UPDATE srMaster SET userid = 1, statusid = 2, bloburl = 'https://url', changedby ='user' WHERE ID = '1143'
or just insert multiple values
INSERT INTO srMaster VALUES
(1, 10, 'https://url1','user1'),
(2, 11, 'https://url2','user2');
In SQL Server, you cannot drop a table if it is referenced by a FOREIGN KEY constraint. You have to either drop the child tables before removing the parent table, or remove foreign key constraints.
For a parent table, you can use the below query to get foreign key constraint names and the referencing table names:
SELECT name AS 'Foreign Key Constraint Name',
OBJECT_SCHEMA_NAME(parent_object_id) + '.' + OBJECT_NAME(parent_object_id) AS 'Child Table'
FROM sys.foreign_keys
WHERE OBJECT_SCHEMA_NAME(referenced_object_id) = 'dbo' AND
OBJECT_NAME(referenced_object_id) = 'PARENT_TABLE'
Then you can alter the child table and drop the constraint by its name using the below statement:
ALTER TABLE dbo.childtable DROP CONSTRAINT FK_NAME;

Problem with starting the trigger in Oracle Apex Application

I created triggers after creating a database in oracle apex. When testing in SQL code everything was fine, they worked. In the application I have the Professors table and when I change the value of the column "name", in the Courses table the value of the column "professor_name" should change. However when I change the value of the column "name" in the Professors table it throws me the error.
Here is the trigger definition:
CREATE OR REPLACE TRIGGER profesori_upd
AFTER UPDATE OF ime on profesori
FOR EACH ROW
BEGIN
UPDATE kursevi set ime_profesora = :new.ime
where pr_jmbg= :new.jmbg_pr;
END;
Here is the table Proffesors:
CREATE TABLE PROFESORI( jmbg_pr VARCHAR2(13) PRIMARY KEY,
ime VARCHAR2(20) NOT NULL,
prezime VARCHAR2(20) NOT NULL,
id_spreme NUMBER(5) NOT NULL,
CONSTRAINT profesor_sprema_fk FOREIGN KEY (id_spreme) REFERENCES
STRUCNE_SPREME(id)
);
Here is the table Courses:
CREATE TABLE KURSEVI(
opis VARCHAR2(40),
id_jezik NUMBER(10) NOT NULL,
id_nivo NUMBER(10) NOT NULL,
pr_jmbg VARCHAR2(13) NOT NULL,
CONSTRAINT kurs_jezik_fk FOREIGN KEY (id_jezik) REFERENCES JEZICI(id_j),
CONSTRAINT kurs_nivo_fk FOREIGN KEY (id_nivo) REFERENCES NIVOI_KURSEVA(id_n),
CONSTRAINT kurs_profesor_fk FOREIGN KEY (pr_jmbg) REFERENCES PROFESORI(jmbg_pr),
CONSTRAINT kurs_pk PRIMARY KEY(id_jezik,id_nivo,pr_jmbg)
);
After denormalization I added columns ime_profesora and prezime_profesora to the table Courses:
ALTER TABLE KURSEVI
ADD ime_profesora VARCHAR2(20);
ALTER TABLE KURSEVI
ADD prezime_profesora VARCHAR2(20);
UPDATE KURSEVI SET ime_profesora=( SELECT ime FROM PROFESORI WHERE profesori.jmbg_pr=kursevi.pr_jmbg);
UPDATE KURSEVI SET prezime_profesora=( SELECT prezime FROM PROFESORI WHERE profesori.jmbg_pr=kursevi.pr_jmbg);
ALTER TABLE KURSEVI
MODIFY ime_profesora varchar2(20) not null;
ALTER TABLE KURSEVI
MODIFY prezime_profesora varchar2(20) not null;
Error occurs after I try to change the value of the column name in the application, it does not say what the error is.

How to get ID from LOV?

I'm learning APEX 5
I have a control named X_CONTROL, where I want to populate his content with an SQL query.
To do that, I need the ID primary key from a table, which should be the ID of the row selected on a Select List control named MY_LIST_CONTROL.
MY_LIST_CONTROL has a list of values taken from a column of the table "MyTable", which is not the ID primary key.
I tried to populate X_CONTROL with this SQL
Select ID from MyTable where ColumnName=:MY_LIST_CONTROL
It doesn't work, and should not work because ColumnName is not "unique", like ID is.
So, the question is, how do I recover, with SQL, the ID of the selected row which correspond to the selected value in MY_LIST_CONTROL.
It should be SQL, because APEX 5 demands an SQL query to populate the X_CONTROL.
I have set up a simple example here on apex.oracle.com:
Whenever a Department is selected (item P32_DEPTNO), its Location is copied into the second item (P32_LOC).
This is done by a dynamic action on P32_DEPTNO defined as follows:
Event: Change
Selection Type: Item(s)
Item(s): P32_DEPTNO
TRUE Action:
Action: Set Value
Set Type: SQL Statement
SQL Statement:
select loc
from dept
where deptno = :P32_DEPTNO
Items to Submit: P32_DEPTNO

How to insert values in the different table by adding values to only one table using sql query?

I have a table 'Person' with columns as 'Person_id as primary key','DOB' and 'place' as follows:
'Person'
Person_id |Name|DOB | place
Another table is "employee" where emp_id is primary key as follows:
'employee'
Person_id |emp_id|dateofjoin
And one more table "Details":
'Details'
emp_id|competency|rating
Now what i want is once i add the 'Person' table details the rest of the two tables as'employe' and 'Details' to get updated also with respect to the new Person added in the Person table. So, how can i have this using sql query? Also i want to clear that i am not very much familiar with database.
I think your after something like this ( for SQL Server ):
Create Procedure dbo.CreateMyEmployee ( #empName varchar(50),
#dob datetime,
#doj datetime,
#place as varchar(100),
#competency varchar(100),
#rating int)
As
Begin
Declare #empId int
Begin Transaction
Begin Try
Insert into Person (Name, DOB, Place)
Values ( #empName, #dob, #place)
Insert into employe (Name, dateofJoin) -- Assuming emp_id is identity columen
Values ( #empName, #doj)
Select #empId = SCOPE_IDENTITY()
Insert Into Details(emp_id, competency, rating)
Values (#empId, #competency, #rating)
Commit transaction
End Try
Begin Catch
Rollback Transaction
SELECT ERROR_NUMBER() AS ErrorNumber, ERROR_MESSAGE() AS ErrorMessage
End Catch
End

Complex SQL syntax

I have a game, and in the database I'm saving the user actions by date & time.
CREATE TABLE user_actions
(
aId BIGSERIAL PRIMARY KEY NOT NULL,
userId BIGINT NOT NULL REFERENCES users(uId) DEFERRABLE INITIALLY DEFERRED,
aDate TIMESTAMP without time zone DEFAULT now(),
aType INTEGER NOT NULL DEFAULT 0
);
My users are identified with email
CREATE TABLE users(
uId BIGSERIAL PRIMARY KEY NOT NULL,
uName VARCHAR (50) NOT NULL,
uEmail VARCHAR (75) UNIQUE NULL
);
and each day new prizes are added each day has a different number of prizes
CREATE TABLE prizes(
pId BIGSERIAL PRIMARY KEY NOT NULL,
pDate TIMESTAMP without time zone DEFAULT now(),
pType INTEGER NULL
pSize INTEGER NULL
);
This query list the userId and his last action date, per user
select distinct userId, max(aDate) from user_actions GROUP BY userId order by userId;
I want to create a query that will count the number of prizes added since each user last action.
I'm running:
OS: Debian
DB: Postgresql
code: Django
I think I will use CTE though It has not been tested
WITH last_actions AS (
SELECT DISTINCT userId, MAX(aDate) as last_logged
FROM user_actions
GROUP BY userId ORDER BY userId)
SELECT a.userId, COUNT(b.pDate)
FROM last_actions a, prizes b
WHERE b.pDate >= a.last_logged
GROUP BY a.userId;