-1

I have a foreach loop,After the foreach trigger fired.So the thing is i need to fire this trigger after the whole foreach ended.(In here the issue is when first item hits to the table then trigger immediately fired.

foreach (var item in NewsTypes.nIDs)
{
    String query2 = "INSERT INTO TBL_NEWS_TYPE_REGIONS (ID, RID, ISACTIVE) VALUES (id.currval, :RID, :ISACTIVE)";
    cmd = db.GetSqlStringCommand(query2);
    db.AddInParameter(cmd, "ID", DbType.Int32, item);
    db.AddInParameter(cmd, "ISACTIVE", DbType.String, "1");
    db.ExecuteNonQuery(cmd);
} 
//db.ExecuteNonQuery(cmd); <-- When i put this here only the last item inserted to the table.

Trigger Starts with:

CREATE OR REPLACE TRIGGER NEWSCHK.NEWSTRG
AFTER INSERT
ON NEWSCHK.TBL_NEWS_TYPE_REGIONS 
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
TechGuy
  • 4,298
  • 15
  • 56
  • 87

1 Answers1

0

The issue here is that when you take the execute outside the foreach loop it will only process the last sql query command, this is why you will only get the last record inserted because your insert statement will only contain this one record value.

If you really want to do it this way you'll need to build up the SQL Insert statement.

This has been answered here: https://stackoverflow.com/a/21858031/1600129

Be aware of the pros and cons of doing it within and outside the foreach loop which is answered here: https://stackoverflow.com/a/2973097/1600129

Community
  • 1
  • 1
jaymarvels
  • 436
  • 1
  • 8
  • 21