I'm building a system update feature for a client of mine, the client drags and drops a xls sheet (export form SAP) and that query basically TRUNCATES the existing "project data" in the import_table and inserts all project data from the xls, now that works fine, but what I need help with is writing a stored procedure that selects the project_number from the import_table and checks if that project_number already exists in the project_table, and if it doesn't exist then INSERT the new project data from import_table by project_number.
So far what I have:
CREATE PROCEDURE `update_projects` ()
CREATE DEFINER=`root`#`%` PROCEDURE `update_projects`()
BEGIN
INSERT INTO `permit`.`project2`(project,description,mat,city,stat,own,est_start,est_end,constr_start,constr_end,constr_cnf,plan_order,div_code,div_long,constr_total_plan,constr_total_act)
SELECT OrderNo,OrderDescription,mat,MainWorkCtr,OrdUsrStatus,JobOwner,ESTStartDate,ESTEndDate,ConstrStartDate,ConstrEndDate,ConstrCNFDate,PlanningOrder,division,division_long,cons_total_plan_hrs,cons_total_act_hrs
FROM `permit`.`wrm100_raw` i
WHERE NOT EXISTS (SELECT 1 FROM `permit`.`project2` pt WHERE i.OrderNo = pt.project)
END;
If I understand correctly, then you can do the check using a WHERE clause:
CREATE DEFINER=`root`#`%` PROCEDURE `update_projects`()
BEGIN
INSERT INTO `db`.`project_table` (project_table columns...)
SELECT import_table columns...
FROM db.import_table i
WHERE NOT EXISTS (SELECT 1
FROM db.project_table pt
WHERE i.project_number = pt.project_number
)
END;
If the concern is duplicated project_numbers, then you should define a unique constraint/index and let the database validate the data.
Related
I have a table called Contacts with a field called person_id that I have connected to a java application.
If no value is specified for person_id in the application, I want to select everything from the contacts table using a stored procedure.
The operation I want to perform is this:
Select * from Contacts where (person_id like "%")
For this I have written a stored procedure shown below:
Delimiter $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `selectTest2`(In p_id int(11))
BEGIN
if p_id = null then
set p_id = "%";
end if;
select * from Contacts where (person_id like p_id);
END $$
Delimiter ;
However when I run this procedure in my sql using the following
call selectTest2(null)
The table that is returned is blank. How do I make it show all the values in the table?
The parameter p_id gets its value from a text box in the application. If the user has entered an id, I want the procedure to show only that particular record else I want it to show all records.
What have I done wrong and how do I correct it? I am aware that p_id is an int however I tried the same thing with other fields of type varchar and the table failed to return any value.
Try using case statement in where clause like below
WHERE CASE WHEN p_id IS NOT NULL THEN person_id = p_id ELSE TRUE END
Hope this should solve your problem
Is it possible that each time a table has a record inserted, delete a view, then re-create a view using the data in the table. For example, my table structure is like such
Create Table NeedView (
Databasename varchar(100)
,DateAdded datetime
)
And the information in that table is just a fully qualified database name like so servername.database.dbo.table My thought is to have a trigger applied that each time a record is inserted into this table, 1st drop the view (it would be called dbo.ViewMaster then recreate it with a quick select of
Select fullname, phone
from each table in the database. Is this achievable through a trigger?
EDIT
I now just need to figure out how to cycle the table to create a view for each databasename in the table...I have this rough syntax ready, with the exception of actually iterating each database. Can someone help me with the missing piece?
CREATE TRIGGER GetViewReady ON [FullOn]
FOR INSERT
AS
IF OBJECT_ID('FullInformation', 'V') IS NOT NULL
DROP VIEW FullInformation
Declare #database varchar(100), #campaignID varchar(10)
Declare C1 Cursor FOR
Select database
FROM [FullOn]
Open C1
Fetch Next FROM C1 INTO #database
WHILE ##Fetch_Status = 0
Begin
'Here is where the actual iteration to create the view would go...
'How would that statement actually need to be syntactically written?
Fetch Next From C1 Into #database
End
GO
EDIT --
A few database names would be hellfire.master.dbo.hennigar, hellfire.mainframe.dbo.dekalb, hellfire.master.dbo.ftworth and the syntax I am after would be like so:
Select fullname, phone
FROM hellfire.master.dbo.hennigar
UNION ALL
select fullname, phone
from hellfire.mainframe.dbo.dekalb
UNION ALL
select fullname, phone
from hellfire.master.dbo.ftworth
I'm attempting to create a procedure that is a basic insert into a table, and then performs a quick update on another table afterwards in MySQL. Please find the code below:
DROP PROCEDURE IF EXISTS `sp_insertUserSocial`
GO
CREATE PROCEDURE sp_insertUserSocial
(
IN p_userSocialID INT(11),
IN p_socialID INT(11),
IN p_userID INT(11),
IN p_referralID INT(11)
)
BEGIN
INSERT INTO UserSocial
(
userSocialID,
socialID,
userID,
referralID
)
VALUES
(
IN p_userSocialID,
IN p_socialID,
IN p_userID,
IN p_referralID
) ;
UPDATE Users
SET connCount = connCount + 1
WHERE UserID = p_referralID;
END
GO
In PHPAdmin it's giving me a syntax error, but I'm not sure where exactly it is? It says line 23, which makes me think it's the semi-colon but I thought that these were needed after an insert statement?
Any help is appreciated, thanks!
I see a couple of problems:
GO, must specify the DELIMITER. 19.1. Defining Stored Programs.
In the INSERT (13.2.5. INSERT Syntax), the IN is optional to pass parameters to the stored procedure (13.1.15. CREATE PROCEDURE and CREATE FUNCTION Syntax), but not part of the syntax of the INSERT.
SQL Fiddle demo
So far I have tried many different ways of accessing the data on three tables using a stored procedure. First I tried a simple select statement :
create procedure reportCodes () begin
SELECT Numbers.serial_numb, numOwner.lName, numOwner.fName, numOwner.email,
location.long, location.lat, Numbers.dateValidated
FROM Numbers, Owner, location
WHERE Numbers.used = true AND Numbers.id=numOwner.Numbers_id AND
Numbers.id=location.Numbers_id;
end$$
(names changed to protect the guilty)
Running the stored procedure in phpmyadmin results in the first instance of the record (1 out of two ‘true’ in the test database). Running just:
SELECT Numbers.serial_numb, numOwner.lName, numOwner.fName, numOwner.email,
location.long, location.lat, Numbers.dateValidated
FROM Numbers, Owner, location
WHERE Numbers.used = true AND Numbers.id=numOwner.Numbers_id AND
Numbers.id=location.Numbers_id;
in the phpmyadmin SQL tab returns both records. Then I tried a temp table:
create procedure reportCodes () begin
CREATE TEMPORARY TABLE used_numbers AS (
SELECT Numbers.serial_numb, numOwner.lName, numOwner.fName, numOwner.email,
location.long, location.lat, Numbers.dateValidated
FROM Numbers, Owner, location
WHERE Numbers.used = true AND Numbers.id=numOwner.Numbers_id AND
Numbers.id=location.Numbers_id);
SELECT * FROM used_numbers; end$$
Returns 1 of 2 records as the procedure but both records in console. Finally I tried changing my table to a join:
CREATE PROCEDURE reportCodes()
begin
create temporary table used_numbers AS (
SELECT Numbers.serial_numb, numOwner.lName, numOwner.fName, numOwner.email,
location.long, location.lat, Numbers.dateValidated
FROM Numbers JOIN numOwner
ON Numbers.id=numOwner.Numbers_id
JOIN location ON
numOwner.Numbers_id=location.Numbers_id
WHERE Numbers.used = true
);
SELECT * FROM used_numbers; end$$
Same results as above. I’m at a loss as to why running just the SQL would show both test records but running the procedure with the exact same code only yields one.
Thanks
in your query, numOwners isn't a valid table being selected against, so something's wrong. Have you tried running your SQL in the Query window in phpMyAdmin to ensure that the EXACT same query is returning 2 rows?
I presume the "Owner" table is supposed to be "numOwner", so I've re-written the stored procedure call below. Also, I'm not sure what types of values you're storing in Numbers.used to evaluate to "TRUE". I will presume you're using a TINYINT(1), so I've altered that, as well. I hope this helps.
DELIMITER $$
USE `db`$$
DROP PROCEDURE IF EXISTS `reportCodes`$$
CREATE PROCEDURE `reportCodes`()
BEGIN
SELECT
n.serial_numb,
o.lName,
o.fName,
o.email,
l.long,
l.lat,
n.dateValidated
FROM Numbers n
INNER JOIN numOwner o ON n.id=o.Numbers_id
INNER JOIN location l ON n.id=l.Numbers_id;
WHERE n.used = 1
END$$
DELIMITER ;
Is there any way to create triggers on two different databases in Mysql? my requirement is like:-
database: test1 -> table: tmp1
database: test2 -> table: tmp2
now I have to use trigger on test1 insert operation happens on tmp1 a value has to be inserted into tmp2 of test2 database. And also vice a verse.
i.e. one more trigger on tmp2 table of test2 database, if insert into tmp2 then inserted into tmp1 table of test1 database.
I have tried to write the trigger on both but I think it will goes into loop to insert each other tables.
DELIMITER $$
CREATE TRIGGER trigger_ad_t1 AFTER insert ON `test1`.tmp1
FOR EACH ROW
Begin
INSERT INTO `test2`.tmp2 VALUES (NEW.employeeNumber,New.fname,New.lname)
END$$
DELIMITER ;
same type of trigger written for insert into tmp1 after insert into tmp2 table.
One more thing I have tested this trigger on my local pc which has mysql 5.1.63 but when I am trying this trigger on my testing server which has mysql 5.0.45 then it gives me syntax error(1064). Don't know what is the problem?
UPDATE:
Can anybody help me to get rid of it.
Thanks
Use fully qualified table names in your trigger.
I.e.
db1.test1.* and d2.test2.*
P.S. After looking at your SQL one more time I realised that you ARE doing the above already.
Edit: Comment field is to restrictive to post code, so here is how you prevent the endless insert loop (assuming employeeNumber is unique key):
Edited code:
IF NOT EXISTS(SELECT employeeNumber FROM otherDB.otherTable WHERE employeeNumber = NEW.employeeNumber) THEN
INSERT INTO otherDB.otherTable VALUES (NEW.employeeNumber,New.fname,New.lname)
END IF;
Correction was needed in the code provided originally:
... EXISTS(SELECT * FROM otherDB.otherTable ...) is replaced with
... EXISTS(SELECT employeeNumber FROM otherDB.otherTable ...)
The reason being that the first query will always return true because the inner query SELECT * FROM ... always returns one record containing the number of results =>
EXISTS(SELECT * FROM ...) is always true