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,'memb er_deduct' )
END
FETCH currentRow INTO @product_code,@member_dedu ct
END
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,'memb
END
FETCH currentRow INTO @product_code,@member_dedu
END
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
Better to use INSERT INTO ... SELECT construction with correct WHERE clause and not to use cursor.
Cursor is not fast solution.
But the CASE is not code branch statement. Use IF instead.