Insert Statement with Condition... how?

pasensyoso_manigbas

Well-known member
Joined
May 19, 2006
Messages
64
Programming Experience
Beginner
hi mods,

im not sure where to post this... but anyone of you out there knows how to have an insert statement with condition in sql...?:confused:
 
Logic would dictate that it would go in a forum dedicated to Data Access.

How can you have an INSERT statement with a condition? Either you want to insert the row or you don't. WHERE clauses are intended to filter the rows affected by a SELECT, UPDATE or DELETE statement because they can all affect zero, one or more than one record. That doesn't apply to an INSERT statement. You have values and you want to insert them. This creates one new row and inserts the values into that row. There's no way you can apply a condition to that.
 
Well actually there is a way you can apply condition to insert statement. That's the EXISTS condition (to identify missing data). Maybe not in this context but anyway there is a way to combine Insert statement with the condition.

This is an example of an insert statement that utilizes the EXISTS condition:

VB.NET:
INSERT INTO table1 (id, field1) SELECT field1Tbl2, field2Tbl2 FROM table2 WHERE exists (select * from table3 Where table1.id = table2.id)

Regards ;)
 
That's an INSERT INTO statement though, which is something a bit different to a vanilla INSERT. The WHERE clause in that code is actually part of the subquery. You can remove the "INSERT INTO table1 (id, field1)" part and it still makes sense because the WHERE clause is part of the query, not the INSERT.
 
That's an INSERT INTO statement though, which is something a bit different to a vanilla INSERT. The WHERE clause in that code is actually part of the subquery. You can remove the "INSERT INTO table1 (id, field1)" part and it still makes sense because the WHERE clause is part of the query, not the INSERT.

Well, the point was only that there is a way to utilize condition clause ... nothing personal ;)
 
Thanx people for clarifying the issue... I already got the logic how am i going to do it... I just ask if it could be possible... In my sense I know it can't be but theres no harm if i ask, who knows the possibility, a lot of you out there are skillful and more experience than I do... geez...

Till then...


Best Regards,
 
Back
Top