Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

conditional insert 2

Status
Not open for further replies.

russland

Programmer
Jan 9, 2003
315
CH
hi,

i check a record set in a table for a condition to be true. if the condition for the current record results true, i want to do an insert statment. hmm...

could somebody give me a short example on how to syntactically write this? i'd appreciate a lot. cheers
 
Hi

Not sure exactly what you after, but basically
if you enter your condition and then then the
insert statement it will only be inserted if your condtion
is true

IF [condition test = true] [INSERT statement here]

if this doesn't help post a little more details
 
you're close. but what if i want to do this check for each single record? kind of ... looping a table and check each record for this condition. if true, insert a rc into another table.

ehh...was i any clearer? sorry if i'm so unclear.
 
No don't loop whatever you do don't loop! Looping is bad, never think of looping!

Depends on what the condition is how you would do it.

If you want to check just one value that is not related to a particular if statement (such as an input variable), use if. Example
If @Count >1
Insert table1 (col1, col2)
Values (@var1, @var2)

If you want to check the value of a particular field in another table and create records in say an audit table based on its value use an insert statement with the select clause. Example:

Insert table1 (col1, col2, col3)
Select @var1, col2, col3 from table2
where Col4 = 'Complete'

this last statement would insert as many records as met the where clause.

 
Hey, thats exactly it (it's in fact an audit table). Your last statement is just what I'm looking for.

thanks a lot. i appreciate your time invested.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top