Unable to create trigger in PhpMyAdmin - sql

Happy Thanksgiving!
I am trying to create a trigger phpmyadmin to update a salary amount.
I made the following table:
CREATE TABLE `salaryraise` (
`SalaryChangeID` decimal(12,0) NOT NULL,
`OldRate` decimal(8,2) NOT NULL,
`NewRate` decimal(8,2) NOT NULL,
`TeacherID` decimal(12,0) NOT NULL,
`ChangeDate` date NOT NULL
)
And I have my existing teacher_data Table
CREATE TABLE `teacherdata` (
`teacher_data_id` int(12) NOT NULL,
`gender` varchar(12) NOT NULL,
`race` varchar(12) NOT NULL,
`hire_date` date NOT NULL,
`salary` decimal(8,2) NOT NULL,
`certicationValid` varchar(12) NOT NULL,
`subjectTaught` varchar(32) NOT NULL
)
And I am trying to run my trigger
CREATE TRIGGER SalaryRaise
On Teacher
AFTER UPDATE
AS
BEGIN
DECLARE #OldRate DECIMAL(8,2) = (SELECT Rate FROM DELETED);
DECLARE #NewRate DECIMAL(8,2) = (SELECT Rate FROM INSERTED);
IF (#OldRate <> #NewRate)
INSERT INTO salaryraise(SalaryRaiseID, OldRate, NewRate, teacher_id, ChangeDate)
VALUES (ISNULL((SELECT MAX(SalaryRaiseID)+1 FROM SalaryRaise),1),
#OldRate,
#NewRate,
(SELECT teacher_id FROM INSERTED),
GETDATE());
END;
I get the following error in PHPMYADMIN
CREATE TRIGGER SalaryRaise
On Teacher
AFTER UPDATE
AS
BEGIN
DECLARE #OldRate DECIMAL(8,2) = (SELECT Rate FROM DELETED);
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'On Teacher
AFTER UPDATE
AS
BEGIN
DECLARE #OldRate DECIMAL(8,2) = (SELECT...' at line 2
Please Advise

Related

Simple Select SQL Query Running Infinitely on a SQL Server table NOT having large data

I am trying to do a select query on a SQL Server table which DOES NOT have large data but the query is running infinitely, every time I have to cancel it after waiting for 5-8 mins. Table only has about 3000 records.
Table structure as below:
Begin Try
CREATE TABLE tbl_Random
(
ID int NULL,
Cust_Id smallint NULL,
Mbr_Id int NULL,
Appl_Id int NULL,
Code varchar(20) NULL,
Req_Date datetime NULL,
Apd char(1) NULL DEFAULT 'N',
Apd_By varchar(20) NULL,
Apd_Date datetime NULL,
Status varchar(20) NULL,
Sent char(1) NULL DEFAULT 'N',
Sent_Date datetime NULL,
Post_Date datetime NULL,
Delt_Flag char(1) NULL DEFAULT 'N',
Delt_By varchar(5) NULL,
Delt_Date datetime NULL,
Delt_Reason varchar(100) NULL,
Created_By varchar(100) NULL DEFAULT CURRENT_USER,
Created_On datetime NULL DEFAULT CURRENT_TIMESTAMP,
Updated_By varchar(100) NULL,
Updated_On datetime NULL,
Notes nvarchar(255) NULL
);
End Try
Begin Catch
Print Error_Message();
End Catch
When I do run the below queries, it brings up the result in less then a second
Select Top 1000 * from tbl_Random
Select Top 2000 * from tbl_Random
Select Top 2500 * from tbl_Random
But when I do
Select Top 3000 * from tbl_Random
it loads the tabular data till 2945 rows quickly in a second but then keeps executing infinitely.
Also the below query keeps executing infinitely with no Result output.
Select Top 1 * from tbl_Random Order By ID Desc
So I think something wrong with the bottom rows, anything I can do to fix this please?
Using SQL Server Management Studio 18.

SQL Trigger throws error but still inserts

I am working on a trigger that is supposed to block an insert when #verkoperstatus is 0; this does function, but for some reason it also stops the insert when #verkoperstatus is 1. What could be the root cause behind this?
CREATE TRIGGER [dbo].[verkoper_check] ON [dbo].[Verkoper]
FOR INSERT,UPDATE
AS
BEGIN
DECLARE #verkoperstatus bit
DECLARE #gebruikersnaam varchar(25)
SELECT #gebruikersnaam = gebruikersnaam FROM inserted
SELECT #verkoperstatus = verkoper FROM Gebruiker WHERE gebruikersnaam = #gebruikersnaam
IF #verkoperstatus = 0
BEGIN
RAISERROR('Geen verkoper!',18,1);
ROLLBACK;
END
ELSE
BEGIN
COMMIT;
END
END
It should insert when #verkoperstatus is 1, and raise an error when #verkopstatus is 0.
The table Gebruiker is references, which includes a 'gebruikersnaam' and a 'verkoper' column. The value of the 'gebruikersnaam' column is the identifying column which (in this specific case is 'Lars'). Verkoper is a bit column, which indicated if one is a seller or not, so this has the value of a 0 or a 1.
The goal I am trying to achieve is to have an insert on the Verkoper tabel if a 'gebruikersnaam' has the 'verkoper' value of one.
This means if there is a row in Gebruiker with the 'gebruikersnaam' of Lars and the verkoper has a value of 1. This will be an allowed insert into the Verkoper tabel.
As the Verkoper has the following columns: 'gebruikersnaam', 'banknaam', 'rekeningnummer', 'controleoptienaam' and 'creditcardnummer'. When 'gebruikersnaam' corresponds with a 'gebruikersnaam' from the Gebruikers table AND has a value of 1 in the 'verkoper' column this record will be allowed to be inserted into the Verkoper table.
As of now there is a row in the Gebruikers column which includes the gebruikersnaam 'Lars' and a verkoper value of '1'. Meaning any SQL Insert with the gebruikersnaam of 'Lars' should be allowed into the Verkoper table.
This however does not function the way I believe it should.
These are the tables mentioned above:
CREATE TABLE Verkoper (
gebruikersnaam varchar(25) NOT NULL,
banknaam varchar(255) NULL,
rekeningnummer varchar(32) NULL,
controleoptienaam char(10) NOT NULL,
creditcardnummer integer NULL,
CONSTRAINT pk_Verkoper PRIMARY KEY (gebruikersnaam),
CONSTRAINT fk_Verkoper_Gebruikersnaam FOREIGN KEY (gebruikersnaam) REFERENCES Gebruiker(gebruikersnaam),
CONSTRAINT ck_rekening CHECK (rekeningnummer is NOT NULL OR creditcardnummer is NOT NULL),
CONSTRAINT ck_controleoptie CHECK (controleoptienaam IN('Post', 'Creditcard'))
)
CREATE TABLE Gebruiker(
gebruikersnaam varchar(25) NOT NULL,
voornaam varchar(25) NOT NULL,
achternaam varchar(25) NOT NULL,
adresregel_1 varchar(255) NULL,
adresregel_2 varchar(255) NULL,
postcode char(7) NULL,
plaatsnaam varchar(255) NULL,
land varchar(255) NULL,
geboortedag char(10) NOT NULL,
mailbox varchar(255) NOT NULL,
wachtwoord varchar(255) NOT NULL,
verkoper bit NOT NULL,
CONSTRAINT pk_gebruiker PRIMARY KEY (gebruikersnaam),
)
For the inserts I am using the following data:
INSERT INTO Gebruiker VALUES ('Lars', 'Lars', 'Last_name', null, null, null, null, null, '04/04/2019', 'lars#mymailbox.cloud', 'MyPassword', 1)
INSERT INTO Verkoper VALUES ('Lars', 'ING', 'NL32ABN32492809', 'Post', null)
This is untested, however, I suspect this is the logic you really need:
CREATE TRIGGER [dbo].[verkoper_check] ON [dbo].[Verkoper]
FOR INSERT,UPDATE
AS BEGIN
IF EXISTS(SELECT 1
FROM inserted i
JOIN Gebruiker G ON i.gebruikersnaam = G.gebruikersnaam
WHERE G.verkoper = 0) BEGIN
RAISERROR('Geen verkoper!',18,1);
ROLLBACK;
END;
END;

Trouble using an equal sign in SQL trigger

This is my table:
CREATE TABLE [dbo].[tblVisitors] (
[Id] BIGINT IDENTITY (1, 1) NOT NULL,
[IP] NVARCHAR (100) NOT NULL,
[ProfileId] INT NULL,
[DateVisit] DATE NOT NULL,
[TimeVisit] TIME (0) NOT NULL,
[Browser] NVARCHAR (50) NOT NULL,
[UserOS] NVARCHAR (500) NOT NULL,
CONSTRAINT [PK_tblVisitors] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_tblVisitors_tblProfile] FOREIGN KEY ([ProfileId]) REFERENCES [dbo].[tblProfile] ([Id]) ON DELETE SET NULL
);
I wrote a trigger to avoid redundancy:
CREATE TRIGGER [dbo].[Trigger_tblVisitors_OnInsert]
ON [dbo].[tblVisitors]
INSTEAD OF INSERT
AS
BEGIN
SET NoCount ON;
DECLARE #C INT;
SELECT *
INTO #TEMP
FROM inserted A
WHERE
NOT EXISTS (SELECT *
FROM tblVisitors B
WHERE (A.IP = B.IP)
AND (A.DateVisit = B.DateVisit)
AND (A.ProfileId = B.ProfileId));
IF (SELECT COUNT(*) FROM #TEMP) = 0
BEGIN
PRINT 'DUPLICATE RECORD DETECTED';
ROLLBACK TRANSACTION;
RETURN;
END
INSERT INTO tblVisitors (IP, ProfileId, DateVisit, TimeVisit, Browser, UserOS)
SELECT IP, ProfileId, DateVisit, TimeVisit, Browser, UserOS
FROM #TEMP;
END
But as this part of the code does not work, redundancy occurs:
(A.ProfileId = B.ProfileId)
Because after deleting this section, the operation is performed correctly. But this condition must be checked.
Using my psychic skills, I suspect that you have ProfileId values that are null, and in SQL the expression null = null is not true, but your logic requires it to be true.
Try this:
AND (A.ProfileId = B.ProfileId OR (A.ProfileId IS NULL AND B.ProfileId IS NULL))

How to use stored procedure to add mutiple locations to one customer?

I have trouble on how to use stored procedure to add multiple locations to one customer. Below are my tables, I am using indexing to help me can add multiple locations to one customer, but should I
SELECT #intLocationIndex = MAX(intLocationIndex) +1
too?
CREATE TABLE TCustomer
(
intCustomerID INTEGER NOT NULL,
strCustomerName VARCHAR(50) NOT NULL,
strAddress VARCHAR(50) NOT NULL,
CONSTRAINT TCustomer_PK PRIMARY KEY (intCustomerID)
)
CREATE TABLE TCustomerLocation
(
intLocationID INTEGER NOT NULL,
intLocationIndex INTEGER NOT NULL,
intCustomerID INTEGER NOT NULL,
CONSTRAINT TCustomerLocation_PK
PRIMARY KEY(intLocationID, intLocationIndex)
)
CREATE TABLE TLocation
(
intLocationID INTEGER NOT NULL,
strLocationName VARCHAR(50) NOT NULL,
strLocationAddress VARCHAR(50) NOT NULL,
strLocationCity VARCHAR(50) NOT NULL,
CONSTRAINT TLocation_PK PRIMARY KEY(intLocationID)
)
CREATE PROCEDURE uspAddCustomerLocation
#strCustomerName VARCHAR(50),
#strAddress VARCHAR(50),
#strLocationName VARCHAR(50),
#strLocationAddress VARCHAR(50),
#strLocationCity VARCHAR(50)
AS
BEGIN TRANSACTION
SET XACT_ABORT ON
DECLARE #intCustomerID INTEGER
SELECT #intCustomerID = MAX(intCustomerID) + 1
FROM TCustomer (TABLOCKX) -- LOCK TABLE UNTIL THE END OF TRANSACTION
SELECT #intCustomerID = COALESCE( #intCustomerID , 1)
INSERT INTO TCustomer (intCustomerID, strCustomerName , strAddress)
VALUES(#intCustomerID ,#strCustomerName ,#strAddress)
DECLARE #intLocationID INTEGER
DECLARE #intLocationIndex INTEGER
SELECT #intLocationID = MAX(intLocationID) + 1
FROM TLocation (TABLOCKX) -- LOCK TABLE UNTIL THEEND OF TRANSACTION
COMMIT TRANSACTION
GO
I am not a DBA but I wish these ID fields were IDENTITY fields. Anyway I think the select in question would be SELECT #intLocationIndex = MAX(intLocationIndex) +1 Where intCustomerID = #intCustomerID

trigger in sql problem

this is my trigger
ALTER trigger [dbo].[addpay]
on [dbo].[pays]
after insert
as
declare #idtutor int
set #idtutor =(select idtutor from inserted)
begin
insert into pays (idtutor,nopay,datex,paythismonth)values (#idtutor,600,GETDATE(),'no')
end
but it doesn't add a new pays after inserted a tutor... i dont watch any bug, mistake, why doesn't it work
my tables
create table Tutor
(
[IdTutor] int primary key identity not null,
[Nombre] varchar(150) not null,
[ApellidoPaterno] varchar (150) not null,
[ApellidoMaterno] varchar (150) not null,
[EstadoCivil] varchar (10) not null,
[FechaNacimiento] varchar(50),
[Municipio] varchar(150) not null,
[Estado] varchar(150) not null,
[Direccion] varchar(250) not null,
[Sexo] varchar (9) not null,
[TelefonoTutor] char(10) not null,
[CelularTutor] char(15) not null,
[EmailTutor] char(50) not null,
[Empresa] varchar(150) not null,
[Ocupacion] varchar(250) not null,
[DireccionEmpresa] varchar (250) not null,
[TelefonoEmpresa] char(10) not null,
[CelularEmpresa] char(15) not null,
[EmailEmpresa] varchar(50) not null
)
create table pays
(
idpay int primary key not null identity,
idtutor int not null,
nopay float,
datex datetime,
paythismonth varchar(2)
)
You need to create the trigger on the table for where you want it to fire when a new record is inserted (Tutor in this case).
Additionally you need to remember that inserts/update statements can affect multiple rows so assigning to scalar variables won't work. The trigger you need is
CREATE TRIGGER YourTrigger
ON [dbo].[Tutor]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON
INSERT INTO pays
(idtutor,
nopay,
datex,
paythismonth)
SELECT idtutor,
600,
GETDATE(),
'no'
FROM inserted
END
You will also need to drop the other trigger in your question with DROP TRIGGER [dbo].[addpay]