|
Guys, I have a while loop I run to loop through an insert statement. I insert records as long as the integer value inside one of the columns (COMPONENT_QTY) is greater than > 1. Problem im having is that im using a left join to insert only records into the table whcih dont already exist. Now when I loop through the first time, it inserts the first record, but it doesnt insert the second (3rd, 4th, 5th, 6th, so on....) time because it detects the left join and says oh this record already exists so no more insert. How do I work around this? TSQL Below:
(comments are locked)
|
|
It seems to me that you either do want to insert records that already exist, or you don't. Decide which is which, then remove / keep the However, have you considered doing this with a Tally table rather than a while loop? It's a bit difficult to see the logic going on, but you could join to a tally table where the number was between 1 and And that would presumably make the problem you're asking come out in the wash... You are right, it does seem like I want to both insert existing and not at the same time. Its kind of confusing. The way the table is designed is very wierd. Anyhow, I think I might have a fix using temporary tables. I will post the solution here once I have it test. Thanks for your input.
Feb 11 '10 at 01:57 PM
Slick84
(comments are locked)
|
|
I'm struggling to picture the database structure in my head. Would it be possible to provide a structure and then I could help you more. I have the solution. I was able to use a temporary table to isolate all non-existing records first. Then just use the above while loop to insert all of them in there using an inner join. Thanks for your help everyone.
Mar 15 '10 at 01:32 PM
Slick84
(comments are locked)
|

