Sql server instead of trigger insert statement with variables and values from inserted?

INSERT INTO myTable ( field1, field2, field3 ) SELECT @someLocalVariable, field2, field3 FROM INSERTED Just remember that if this was triggered by a batch insert (i.e. INSERTED has more than one record), all records will also be inserted into the target table (Which is exactly what it should do IMHO, but for some reason people usually forget about that. ) Edit The OP has forgotten to say what his question was really about: generating unique primary key values.So, in that case, one way of doing that would be something like this: CREATE TRIGGER dbo.

InsteadOfInsertMyTable ON MyTable INSTEAD OF INSERT AS BEGIN INSERT INTO myTable ( primaryKeyField, field2, field3 ) SELECT dbo. SomeUniqueValueGeneratorUDF(), field2, field3 FROM INSERTED END GO.

INSERT INTO myTable ( field1, field2, field3 ) SELECT @someLocalVariable, field2, field3 FROM INSERTED Just remember that if this was triggered by a batch insert (i.e. INSERTED has more than one record), all records will also be inserted into the target table. (Which is exactly what it should do IMHO, but for some reason people usually forget about that.) Edit The OP has forgotten to say what his question was really about: generating unique primary key values.

So, in that case, one way of doing that would be something like this: CREATE TRIGGER dbo. InsteadOfInsertMyTable ON MyTable INSTEAD OF INSERT AS BEGIN INSERT INTO myTable ( primaryKeyField, field2, field3 ) SELECT dbo. SomeUniqueValueGeneratorUDF(), field2, field3 FROM INSERTED END GO.

2 +1 Not sure why the OP doesn't just use a default for this though. – Martin Smith Jan 26 at 17:11 @Martin: sure thing. – rsenna Jan 26 at 17:11 my udf is incrementing a string primary key.So you are correct it will not work for bulk insert.

Is there a way to update INSERTED with the udf values and then insert all of inserted? – tim Jan 27 at 12:39 @tim: ah, got it. Not a problem - just call the function directly from the insert statement.

I'll edit my answer. – rsenna Jan 27 at 19:32 thanks rsenna. Yes I was trying to post a fragment of my overall problem.

I will mark as answered and repost with my overall issue. But thanks for answering my questions and pointing out the issues with my approach. – tim Jan 27 at 20:43.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions