Hi, I need to write code that uses temp tables to check data and transfer it to another table. As I understand, data has to be transfered to temp table, and then a loop will check each row for data integrity and move the valid rows into a new table. The code looks something like this. The problem is with this code, the destination table (table 3) contains only one record instead of 375. What am I doing wrong
Select CustomerID, StaffID, Sequence, Bldg, Floor
into #temptable
from [06w_manukyan]..[Table1]
declare @CustomerID char(10),
@StaffID char(6),
@Sequence int,
@Bldg char(3),
@Floor char(2)
set rowcount 1
Select @CustomerID=CustomerID, @StaffID=StaffID, @Sequence=Sequence, @Bldg=Bldg, @Floor=Floor
from #temptable
While @@rowcount = 1
--data integrity check goes here
Begin
--insert checked data into destination table
Insert into [06w_manukyan]..[Table3]
( CustomerID, StaffID, Sequence, Bldg, Floor)
values
(@CustomerID, @StaffID, @Sequence, @Bldg, @Floor)
delete from #temptable
Select @CustomerID=CustomerID, @StaffID=StaffID, @Sequence=Sequence, @Bldg=Bldg, @Floor=Floor
from #temptable
End
select * from [06w_manukyan]..[Table3]

Transfering and verifying data using temp tables
Nikolaj Berg Amondsen
pfpgroup
It seems you missed out to add where criteria in delete statement.
delete from #temptable Where CustomerID=@CustomerID AND StaffID=@StaffID AND [Sequence]=@Sequence AND Bldg=@Bldg AND [Floor]=@Floor
luis figueredo
Be sure to set the rowcount back to 0 after setting it to 1. Otherwise, all subsequent queries will only work with 1 record. This could be why your insert is only getting 1 result.
Is it really necessary to perform the validity checks on a row, by row basis Can the checks be turned into a series of set based queries that check different aspects of the rows
It is kind of odd to use simulated cursors, these days, when doing row at a time processing. If sets cannot be used, then use the cursors.
SSEGARANE
Nope, if I move delete out of loop, the loop itself never ends. The rowcount should be depreciated, that's how the loop was supposed to work, i.e it would loop as long as there is one row left in the original table.
marlin1
hi there,
You can use the identity with your select into temptable
like this
and the do the cursor less processing like this
Vegethalia
I would suggest, using a cursor with @@fetch_status. The sample code is :
DECLARE @MyInsertCursor CURSOR
-- Cursor Definition
SET @MyInsertCursor = CURSOR FAST_FORWARD
FOR
SELECT CustomerID, StaffID, Sequence, Bldg, Floor
from #temptable
OPEN @MyInsertCursor
FETCH NEXT FROM @MyInsertCursor INTO @CustomerID, @StaffID, @Sequence, @Bldg, @Floor
--LOOP all records from the inserted table
WHILE @@FETCH_STATUS = 0
Begin
--insert checked data into destination table
Insert into [06w_manukyan]..[Table3]
( CustomerID, StaffID, Sequence, Bldg, Floor)
values
(@CustomerID, @StaffID, @Sequence, @Bldg, @Floor)
FETCH NEXT FROM @MyInsertCursor INTO @CustomerID, @StaffID, @Sequence, @Bldg, @Floor
END
CLOSE @MyInsertCursor
DEALLOCATE @MyInsertCursor
n y n--d8bn pr95y6
maxcredible
I guess the problem is with the delete statement. you need to keep it outside the loop.
I still doubt you will get the correct records. I mean I am not sure how do you move to the next record after you insert the first.
Also ROWCOUNT is going to get deprecated.
Euclidez