MySQL has this incredibly useful yet proprietary record holding the "new row"(NEW).
I wonder if there is any SQL server command to replace the New in MySQL.
In mysql, in a trigger, I would use something like this.
INSERT INTO products(idProduct,idReference,date)
VALUES (NEW.idProduct,idReference,NOW());
However I don't know how if is it possible to do the same command but in Sql Server.
I hope I have been clear in my question.
If not explicit, or if it isn't possible to implement what I want, I apologize.
Thank you all.
In your trigger
INSERT products(idProduct,idReference,date)
SELECT idProduct, idReference, GETDATE()
FROM inserted
new is only available in a trigger if I'm not mistaken.
As SQL Server uses statement-level triggers and MySQL uses row-level triggers there is not direct equivalent to the concept of "the new row" in a trigger. Read the documentation on the inserted virtual table that is available in SQL Server.
Example usage: SQL Insert trigger to update INSERTED table values
Related
In Oracle Database (SQL Plus), there is an alternative method to insert values into a table, which my lecturers called "insert by reference". It looks like this:
SQL> INSERT INTO table_name ('&col_name1','&col_name2' ...);
Enter value for col_name1: value1
Enter value for col_name1: value1
...
This enables you to use the same command repeatedly (by pressing up arrow) to enter multiple records in the table; you only need to enter the specific values separately after executing the command. And there is no need to go back to each value, erase it and type in the new value.
So my question is, is there any way to replicate this handy command in MySQL?
This is a feature of sqlplus, not of oracle's, taking advantage of oracle's prepared statement feature.
You need to find or develop an sql client for mysql that can similarly use mysql's prepared statement feature in a nicer way either directly through SQL or through an API (C API is just an example).
We cannot recommend 3rd party tools or utilities here on SO, you need to find the one that best suits your needs.
perhaps use the multi row insert:
insert into table_name (col_name1, col_name2)
values (value_1_1, value_1_2), (value_2_1, value_2_2) [...]
I had a trigger set in MySQL workbench that used to accomplish what I wanted. Recently, my workplace updated MySQL workbench to version 6.3.10 across all computers and the triggers were dropped during the update for some reason.
Basically, the trigger should update a column with the current date when a different column is changed. This was previously achieved by using the following syntax:
if (NEW.colname <> OLD.colname or (OLD.colname is null && NEW.colname is not null))
then
set NEW.othercolname = current_date()
The issue is when I try to recreate the same trigger in the new MySQL version and apply it, I am met with the error:
ERROR 1363: There is no OLD row in on INSERT trigger
It seems to me that MySQL is looking for a row named OLD, which does not exist; however, this syntax used to work in previous versions for the intended result. Can anyone suggest an alternative syntax to accomplish this?
First of all, the error message comes from mysql, not from mysql workbench, so mysql workbench's version is irrelevant here.
Secondly, the error message is pretty clear: you are trying to refer to the OLD row in an insert trigger. This is obviously impossible, since there is no old row when you insert a new one.
You need to change either the trigger to catch an update event or change the trigger logic.
ERROR 1363: There is no OLD row in on INSERT trigger
You've got MySQL Workbench set to create an INSERT trigger. As the error message says, there's no OLD row in an INSERT trigger.
What you want is an UPDATE trigger.
how to check for new table entry in MySql?
I need to automate the task in java so every time a new row added to the table the scrip that I am writing give me alert.
You can use an INSERT trigger.
Here is a Stackoverflow article: SQL Server 2008 - Help writing simple INSERT Trigger
you can refer link below for you reference
http://solutioncenter.apexsql.com/get-an-alert-when-a-certain-record-changes/
I am wondering if there is a specific Insert or Update option in SSIS.
Will I have to do some coding if I want to let the program check if it is an update or insert?
Or can there be an option enabled so it will check itself if the PK exists then update and otherwise insert?
Kind regards
Just one solution
SSIS update insert
better update syntax
If there are more records it will be slow.
I don't know if I uderstand your problem properly. But I think SQL Server MERGE would be very useful here. And it's super-efficent.
More info here:
http://technet.microsoft.com/en-us/library/bb510625.aspx
Is there any way to implement a time based trigger in Mysql 5.1
i.e. it must run at 12H05 every day
Edit
A corn job calling an SQL script is currently been used -
but I am looking for a less complicated solution.
To do this with Mysql 5.1.x use the following code:
CREATE EVENT myevent
ON SCHEDULE
EVERY 6 HOUR
COMMENT 'A sample comment.'
DO
UPDATE myschema.mytable SET mycol = mycol + 1;
Here link to the users manual
Unless triggers in MySQL are different than in most other RDBMS, a time based trigger makes no sense. Triggers are fired in reaction to rows being added/inserted/deleted in a table. If you want a time based operation to occur, you should be considering a Job (in SQL Server) or Windows Service.