I have two tables with 1 to many relationship. Here are they:
When inserting into ActivityAttribute I would like to use generated idAA for inserting many records to PersonActivityAttribute - one for each existing value of PersonActivityAttribute.idPA. It goes like this: when I insert a new record to ActivityAttribute with idAA = 5 I want the following result
The question is - how can I achive it using trigger? Is it possible?
You can make use of the below query
CREATE TRIGGER trgAfterInsert on ActivityAttribute
FOR INSERT
AS DECLARE #idAA INT;
SELECT #idAA=i.idAA FROM inserted i;
INSERT INTO PersonActivityAttribute(idPA,idAA,value)
SELECT DISTINCT idPA, #idAA, NULL value
FROM PersonActivityAttribute;
Hope this should help you out.
Related
I'm trying to write a trigger that get a QuoteNumber from table 1 and insert it into table 2 where ShippingIdentity matches the records in both tables. The problem is the QuoteNumber is being inserted into it's own row(record) Can anyone please let me know why? Thanks.
ALTER TRIGGER InsertQuoteNumber
ON AccountInfo
FOR INSERT
AS
BEGIN
INSERT INTO ShippingInfo (QuoteNumber)
SELECT a.QuoteNumber
FROM AccountInfo a
inner join inserted i on a.ShippingIdentity = i.ShippingIdentity
END
You use the word "insert" in your description, but I think what you mean is "update". That is, you want to modify an existing row in ShippingInfo to reflect the new quote number.
The problem in your code is as simple as your choice of words. You are using an INSERT command, which inserts a new row. If you want to update an existing row, use an UPDATE.
I am trying to create a trigger on update of a column and insert rows into other tables. The data is coming from a SELECT with multiple joins and needs to be inserted into multiple tables.
My question is, is it best practice to insert the values into variables before the insert? I have never created a trigger like this before.
CREATE TRIGGER ship_trigger ON dbo.Orders
FOR UPDATE
AS
SET NOCOUNT ON
IF ( UPDATE(OrderStatusId) AND OrderStatusId == 1)
BEGIN
SELECT
FROM
JOIN
JOIN
WHERE
I am just looking to understand the most efficient way to insert the data into the other tables
Thanks!
Assuming SQL Server (the syntax suggests so), it isn't a best practice to insert the values into variables first, what would be the point of doing that?. And the only way that you could actually do that would be on table variables or if you update only one row, since the triggers gets executed once for all the rows that got affected by your UPDATE, and those rows are available to you on the INSERTED pseudo table. So, your trigger would look something like this:
CREATE TRIGGER ship_trigger ON dbo.Orders
FOR UPDATE
AS
SET NOCOUNT ON
IF ( UPDATE(OrderStatusId) AND OrderStatusId = 1)
BEGIN
INSERT INTO SomeTable(Col1, Col2)
SELECT Col1, Col2
FROM INSERTED
END
This is my first time using triggers.
My trigger is not being triggered please help.
CREATE TRIGGER sbhack_autoban
ON LOG_CONNECT201211
FOR INSERT
AS
BEGIN
/* query to run if single or multiple data is
inserted into LOG_CONNECT201211 table */
UPDATE login.dbo.USER_CHECK_LOGIN
SET login.dbo.USER_CHECK_LOGIN.CHECKLOGIN = 2
WHERE login.dbo.USER_CHECK_LOGIN.USER_KEY IN
(SELECT e.USER_KEY
FROM game.dbo.CHAR_DATA0 AS e
INNER JOIN gamelogs.dbo.LOG_USING_DEPOT201211 AS p
ON e.CHAR_KEY = p.CHAR_KEY
WHERE p.GATENUM = 150)
AND login.dbo.USER_CHECK_LOGIN.CHECKLOGIN = 0
AND login.dbo.USER_CHECK_LOGIN.USER_KEY != 51;
END
This is suppose to run the query inside the BEGIN : END if an entry is inserted into the LOG_CONNECT201211 table. But nothing is happening even when I have inserted multiple data into LOG_CONNECT201211.
When your INSERT trigger fires - then at least one new row has been inserted! That's a fact.
Now the question is: given that a single or multiple new rows have been inserted - what do you want to do with this knowledge??
Typically, you could e.g. set a column to a value you cannot specify as a default constraint - or you could insert the fact that the row has been inserted into an audit table or something....
So you'd have something like this:
CREATE TRIGGER sbhack_autoban
ON LOG_CONNECT201211
FOR INSERT
AS
INSERT INTO LogAudit(InsertedDate, UserKey)
SELECT
GETDATE(), i.User_Key
FROM
Inserted i
or something like that....
Update: ok, so you want to run that UPDATE statement when the rows have been inserted - not 100% clear, what columns/values from the inserted rows you want to use - looks like the e.UserKey column only - correct?
Then the UPDATE would be:
UPDATE login.dbo.USER_CHECK_LOGIN
SET login.dbo.USER_CHECK_LOGIN.CHECKLOGIN = 2
WHERE
login.dbo.USER_CHECK_LOGIN.USER_KEY IN
(SELECT USER_KEY FROM Inserted)
AND login.dbo.USER_CHECK_LOGIN.CHECKLOGIN = 0
AND login.dbo.USER_CHECK_LOGIN.USER_KEY != 51;
Update #2:
The point I still don't understand is : why do you want to run an update that uses the USER_CHECK_LOGIN, CHAR_DATA0 and LOG_USING_DEPOT201211 tables, when some rows are getting inserted into a totally separate, unrelated table LOG_CONNECT201211 ??
A trigger is used when you want to do something because rows have been inserted into that table - but in that case, you typically want to do something with the rows and their values that have been inserted...
I just don't see any connection between the rows being inserted into LOG_CONNECT201211 event, and the tables you are then querying from and updating. Where's the link?? WHY do you need to run *this UPDATE when data is inserted into LOG_CONNECT201211 ?? It would make sense if data where inserted into one of the tables involved in the UPDATE - but like this, it just totally doesn't make any sense .....
I've never created a trigger before and I'm trying to read online but am a little confused.
I want to create a trigger on a table that on insert, it will grab some data from different columns and insert it into a few different other tables.
I'm not sure how to write the T-SQL to get the data from the columns..
insert into [othetTable]
values ([col1 from row that was inserted], [col5 from row that was inserted])
What would the syntax be to get those values?
thanks
Use the inserted virtual table that is available to triggers. Note that there could be multiple rows in this table - your trigger could be processing multiple inserts at once.
Therefore, you need to use something like the following syntax:
insert into othertable
select col1, col5
from inserted
This will insert a row into othertable for each inserted row.
I need to insert a data into a DB using query like this one:
INSERT INTO Table1
(aID)
VALUES
(select aID from Table2 where aID="aID"))
The problem is that if Table2 doesn’t have the aID I need to update it with it and extract the newly created ID. Is there a SQL command that automatically does it?
Your question is kinda all over the map...
The problem is that if Table2 doesn’t have the aID I need to update it with it - what do mean by this?
If it doesn't exist you got to insert it ...rght?
Having said that, you need to write a stored-procedure to achieve your objective...and also set "aID"...to be an auto-incremental,seeded Identifier column type
Let me know if you need elaboration
Presumably, you have more columns to add in the real query, as it doesn't make any sense at the moment, you just have one column in the whole thing.
Presuming that you want to insert some other columns from table 2 into table 1 and then update into table 2 the new id that comes from the table 1, there's no one command that will do this for you.
Something like (in SQL Server)
DECLARE #newId int
insert into table1 (.. columns..) values select (..columns..) from Table2 where aID="aID")
select #newId=scope_identity()
update table2 set foreign_key_column_to_table_1=#newId where aID="aID"