-1

I'm working with netezza database and have a requirement to insert a Y flag for stores in California. I wrote the below procedure

CREATE OR REPLACE PROCEDURE MY_NEW_PROCEDURE() RETURNS BOOL
EXECUTE AS OWNER LANGUAGE NZPLSQL AS
BEGIN_PROC
DECLARE
    rec RECORD;
BEGIN
FOR rec in SELECT * from test_table 
LOOP
if rec.state_code ='CA'
EXECUTE IMMEDIATE 'INSERT INTO test_table (california_stores)' || 'values('y')';
END LOOP;
END;
END_PROC;

when I call the procedure using call MY_NEW_PROCEDURE() I get an error at line EXECUTE IMMEDIATE. I'm not sure what change I need to make here.

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
karthikc
  • 1
  • 1
  • 1

2 Answers2

0

Don't know about netezza, but below is the procedure I used to test this (using SQL developer).

Works fine for me, although it would make more sense to update the row to set california_stores to 'Y'rather than insert a new row with california_stores = 'Y' for each calafornia store that you have.....

CREATE OR REPLACE PROCEDURE "MY_NEW_PROCEDURE" as
    rec test_table2%rowtype;
BEGIN
   FOR rec in (SELECT * from test_table2) LOOP
      if rec.state_code = 'CA' then
         EXECUTE IMMEDIATE 'INSERT INTO test_table2 (california_stores)' || 'values(''y'')';
      end if;
   END LOOP;
END;
Shaun Peterson
  • 1,735
  • 1
  • 14
  • 19
0

You didn't post the second error, but it looks to me like your insert statement isn't going to do what you want anyway. If the rec variable contains the attribute state_code and you're inserting a single value to test_table then the record will simply be empty except for a 'Y' in california_stores.

I'm going to guess that you're getting an error now either because of the spacing in the insert statement insert into test_table (california_stores)values('y') or because you didn't terminate the execute statement with a semicolon. The plsql for that line should be

execute immediate 'insert into test_table (california_stores) values (''y'');';

Jeremy Fortune
  • 2,459
  • 1
  • 18
  • 21