1

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.

podiluska
  • 50,950
  • 7
  • 98
  • 104
strange_098
  • 1,261
  • 6
  • 24
  • 44
  • 2
    `new` is only available in a trigger if I'm not mistaken (and it's not a "command" it's a record holding the "new row") 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 tirgger. Read the documentation on the `inserted` virtual table that is available in SQL Server. –  May 21 '14 at 14:28
  • Thanks for reply. I'm read the documenation on the inserted to know... – strange_098 May 21 '14 at 14:32

2 Answers2

1

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

Community
  • 1
  • 1
RandomSeed
  • 29,301
  • 6
  • 52
  • 87
1

In your trigger

INSERT products(idProduct,idReference,date)
SELECT idProduct, idReference, GETDATE()
FROM inserted
podiluska
  • 50,950
  • 7
  • 98
  • 104
  • Very Thanks for reply @podiluska. And this ----> SELECT idReference from produtcs where idProduct=New.idProduct It is possible to replace too? – strange_098 May 21 '14 at 15:10
  • In a trigger on the products table, the inserted table is equivalent to the products table, so the above should work. – podiluska May 21 '14 at 15:23
  • Sorry for my bad explanation. My new doubt is not in the Products table, is another. So it is not possible to do the same, because is in the same trigger. I edited the comment above. Is it possible to help me. I apologize for the inconvenience. Thank you for your attention. – strange_098 May 21 '14 at 15:49
  • SELECT idCategory from category where idProduct=New.idProduct It is possible to replace too? – strange_098 May 21 '14 at 15:50
  • You can still do it. Use a join to join the inserted (products) table to the categories table. – podiluska May 21 '14 at 15:56