Link to home
Start Free TrialLog in
Avatar of singhch
singhch

asked on

SQL Syntax Error

Hi there,

I am trying to iterate thru an existing table using CURSOR FOR and checking if the current row being processed has a similar row in another newly created table.  If there isnt a similar row in the new table, then i would want to insert that row which is being processed into the new table.  The following is a sample of my code.

transaction_unique is the name of my new table.

I am getting syntax error with my CASE statement.  Could someone please point out the correct syntax? thks

WHILE @@FETCH_STATUS = 0
BEGIN
 SET @count = @count + 1
 --check if the current transaction exist
 CASE
  WHEN @member_deduct > 0 THEN
    IF NOT EXISTS (SELECT TOP 1 * FROM transaction_unique WHERE product_code = @product_code   member_deduct > 0) THEN
INSERT INTO transaction_unique VALUES(@product_code,'member_deduct')
END

FETCH currentRow INTO @product_code,@member_deduct
END
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You not posted on which SQL server is this running.

But the CASE is not code branch statement. Use IF instead.
But generally this aproach is nto good for performance.
Better to use INSERT INTO ... SELECT construction with correct WHERE clause and not to use cursor.
Cursor is not fast solution.