I am trying to convert if exists statement from SQL-Server to PL/SQL but having an error.
I am trying to check if NAME_1
doesn\'t exist in my table_1<
Provided table_2
has no DEFAULT
value for third column you can do it much simpler like this one:
DECLARE
l_count NUMBER;
l_count_2 NUMBER;
BEGIN
SELECT COUNT(*) INTO l_count FROM table_1 WHERE NAME='NAME_1';
IF l_count = 0 THEN
SELECT COUNT(*) INTO l_count_2 FROM dba_tab_cols WHERE TABLE_NAME = 'table_1' AND COLUMN_NAME='NAME_2';
INSERT INTO table_1
SELECT 'value1', MAX(column), CASE WHEN l_count_2 > 0 THEN 20 ELSE NULL end
FROM table_2;
END IF;
END;
There is no reason for dynamic SQL.