What actually I am trying to do is, insert a row with INSERT INTO table (col2, col3, <...>) VALUES (col1, $something, <...>).
col1 is an auto_increment id column, and basically I want to copy that value to col2 on INSERT statement, within one query. Is this possible?
If I have understood correct then in the same INSERT statement you are trying to insert the generated AUTO_INCREMENT ID to another column.
Well, that's not possible IMO; cause if you do so the AUTO_INCREMENT ID is still not generated.
One solution is to use a stored procedure and perform the INSERT+UPDATE together like below (a sample)
create procedure usp_dowork(parameter list ...)
as
begin
INSERT INTO table (col2, col3, <...>) VALUES (NULL, $something, <...>);
update table set col2 = LAST_INSERT_ID()
where col1 = LAST_INSERT_ID();
end
You should consider using a transaction block around the DML's to make sure both the operations happens successfully.
Related
I want to add values according to index in my table.
This is the code I am trying right now.
insert into A(1,2) values ('ABC','CBA');
The insert statement does not accepts column indexes, but column names. Say your table called a has three columns called id, col1 and col2, and you want to insert into the last two columns, you would do:
insert into a (col1, col2) values('ABC', 'CBA');
The only way that you can add columns according to position is to leave out the column list and include all columns:
insert into A
values ('ABC', 'CBA');
That said, you really should be explicit about which columns are getting values, by including the column names:
insert into A (col1, col2)
values ('ABC', 'CBA');
Or using the MySQL set extension:
insert into A (col1, col2)
set col1 = 'ABC', col2 = 'CBA';
I'm new to SQL, but trying to build a simple trigger that will execute on each insertion. I want it to update some table values if the key already exists, or insert it with some new values if it doesn't exist.
Below is what I have attempted, but I would ideally want tester to be the thing that is being inserted. Something like NEW.reference_key?
CREATE TRIGGER key_access_monitor
BEFORE INSERT ON individual_key_log
FOR EACH ROW
BEGIN
IF EXISTS (SELECT reference_key FROM individual_key_log WHERE key = 'tester')
SELECT 'Found it!'
ELSE
SELECT 'Cannot find it!'
END IF
END
You want to insert a new row if a row with the same key does not already exist, and update the existing row if it does exist, is that correct?
You'd probably find it easier to use INSERT ... ON DUPLICATE KEY UPDATE, as follows:
INSERT INTO individual_key_log
(key, col1, col2, col3, ...)
VALUES
('tester', 'val1', 'val2', 'val3', ...)
ON DUPLICATE KEY UPDATE
SET col1 = 'val1', col2 = 'val2', col3 = 'val3', ...;
One of the problems with your trigger approach is that it is bad practice to attempt to query a table from within a row trigger on that same table, as it violates the ACID principle; many DBMSes do not permit it at all, raising a "Mutating Table" error. Updating a row from within an insert row trigger has the same problem magnified. A row trigger should never try to access its target table directly, instead accessing only the triggering row indirectly through OLD and NEW.
Hope that helps.
I need to create a SQL script with many step in it.
First of all, I need to insert data into a Parent Table.
How can I Get the list of primary key value
Here is an example of what I'm trying to perform.
MyParentTable
MyParentID PK
col1,
col2,
col3
--INSERT VALUE INTO THE PARENT TABLE
insert into MyParentTable(col1,col2,col3)
select SDATA1,SDATA2,SDATA3
from ExampleTables
I Would like to get the list of my newly entries.
How to do that?
IMPORTANT NOTE : Consider that MyParentTable can alreaydy contains data.
insert into dbo.MyParentTable(col1,col2,col3)
output inserted.identity_column_name
select SDATA1,SDATA2,SDATA3
from dbo.ExampleTables;
If there are foreign keys involved, you may have to use a #table variable for temporary holding.
DECLARE #t TABLE(id INT);
insert into dbo.MyParentTable(col1,col2,col3)
output inserted.identity_column_name INTO #t
select SDATA1,SDATA2,SDATA3
from dbo.ExampleTables;
SELECT id FROM #t;
I find that there are only after and instead of triggers in sql server. And it is illegal to modify the values in the inserted pesudo table. Then my problem occurs: If I want to check the data which is going to be inserted into my table, and when the data violates my constraints I should modify these values to default values, how to do it ? How about updateing the values after inserted ? However, if there's no primary key or colum which is unique in my table, how can I locate the row just inserted and then update it ?
Basically, with an INSTEAD OF INSERT trigger, you can achieve what you're looking for - just read out the data from the INSERTED pseudo table, modify it, and insert it into the table
So your trigger would look something like this:
CREATE TRIGGER YourTrigger ON dbo.YourTable
INSTEAD OF INSERT
AS
SET NOCOUNT ON
-- do the INSERT based on the INSERTED pseudo table, modify data as needed
INSERT INTO dbo.YourTable(Col1, Col2, ....., ColN)
SELECT
Col1, 2 * Col2, ....., N * ColN
FROM
INSERTED
Of course, you could also add e.g. checks in the form of WHERE clause to that SELECT .... FROM INSERTED statement to e.g. ignore certain rows - the possibilities are endless!
I was looking for a way to create a trigger that would insert the same row into two tables with the same values.
For example, a new row is inserted into pushNotificationQueue as soon as that is inserted, I would like that same exact row to be inserted into messages.
I tried this
CREATE TRIGGER add_to_messages
after insert on mbb_pushNotificationQueue
FOR EACH ROW
insert into mbb_messages select * from mbb_pushNotificationQueue
the only problem with that is that it goes through and adds entries that have already been previously added.
You have to say with which rdbms you are working.
Anyway, you have to use a special table normally named inserted or similar.
This is for Sql Server:
INSERT INTO mbb_messages SELECT * FROM INSERTED
Others like Sybase use a REFERENCES clause to get to the newly inserted record:
create trigger TriggerName after insert on
TableName
referencing new as new_name
And for MySQL (which you are seem to use) you can refer to the newly inserted records by using the NEW table:
CREATE TRIGGER add_to_messages
after insert on mbb_pushNotificationQueue
FOR EACH ROW BEGIN
insert into mbb_messages select * from NEW;
END;
You need to use the column name with new keyword. Please find the trigger below:
DELIMITER $$
CREATE TRIGGER add_to_message
after insert on mbb_pushNotificationQueue
FOR EACH ROW BEGIN
insert into mbb_oushNotificationQueue(`col1`, `col2`) values(new.col1, new.col2);
END$$
DELIMITER ;
First of all, I say that using select * with an insert-select statement is really, really a bad idea. The reason is that you can never predict the order of the columns that are returned from a selection.
Secondly, assuming SQL Server, I would suggest using the following:
create trigger add_to_message
instead of insert on mbb_pushNotificationQueue
for each row
as
begin transaction
insert into mbb_oushNotificationQueue (col1, col2, col3)
select col1, col2, col3
from inserted
insert into mbb_messages (col1, col2, col3)
select col1, col2, col3
from inserted
if ##ERROR_LEVEL = 0
commit
else
rollback
Disclaimer:
This code has not been tested and may require some minor fixes, but is illustrating the idea very well.
I ended up using
CREATE TRIGGER add_to_messages
after insert on mbb_pushNotificationQueue
FOR EACH ROW
INSERT INTO mbb_messages SET messageID = NEW.messageID,
toUserID = NEW.toUserID,
fromUserID = NEW.fromUserID, message = NEW.message, dateReceived = NEW.dateReceived
Thanks to everyone who posted.
So only add the last record added.
insert into mbb_messages select blah from mbb_pushNotificationQueue where blah meets some criteria....
Having max(id) or something.