I have a table_A where i imported a data from .csv file. I want to compare some of the columns from table_A with table_B and accordingly insert a data into a table and update a status to table_A. what should i use While loop or Cursor.. Plz do suggest
DECLARE @A_2 VARCHAR(10), @A_3 VARCHAR(50), @A_4 VARCHAR(100)
DECLARE db_cursor CURSOR FOR
SELECT A_2, A_3, A_4 FROM Temp_table
OPEN db_cursor
FETCH NEXT FROM db_cursor
INTO @A_2, @A_3, @A_4
WHILE @@FETCH_STATUS = 0
BEGIN
If @A_2 <> (SELECT C_2 FROM Table_C where C_2= @A_2)
UPDATE Temp_table SET [Status]='Not Exits in Table_C'
ELSE BEGIN
IF @A_3=(Select B_3 from Table_B where B_3=@A_3) AND @A_2=(SELECT B_2 FROM Table_B where B_2= @A_2)
UPDATE Temp_table SET [Status]='Duplicate Row, Already Exists'
ELSE
IF (@A_4 <>'B_4 '+'B_5')
UPDATE Temp_table SET [Status]=' Format is not accepted '
ELSE
INSERT INTO Table_B(B_2, B_3, B_4) VALUES(@A_2, @A_3, @A_4)
END
CLOSE db_cursor DEALLOCATE db_cursor
FETCH NEXT FROM db_cursor INTO @A_2, @A_3, @A_4
END
Here Temp_table is the table where i will be importing data from .csv file and comparison will be done using columns of Table_B, Table_C with Temp_Table will it be correct way to do?