SQL returning error when selecting non-existent field

Asked

Viewed 252 times

0

It has a system that several companies use, but changes in bank tables are not informed.

You could create a select that does not return an error by not finding a field recently removed from a table?

Code:

SELECT 
    cd_cliente,
    nm_cliente,
    dt_imp_banco 
FROM 
    dbamv.cliente
WHERE 
    dt_imp_banco IS NOT EXISTS;

Error message:

ERROR: column "dt_imp_banco" does not exist LINE 4: dt_imp_banco SQL state: 42703 Character: 44

  • Not that I know.For the interpreter is called and checks the syntax.

  • My goal is to create a select that ignores fields that do not exist, in case the field: dt_imp_bank does not exist.

1 answer

1


Based in that question of SOEN, is possible, but not directly. It suggests checking if the column exists and only then run the select:

DECLARE
  v_column_exists number := 0;  

BEGIN
  SELECT count(*) INTO v_column_exists
  FROM user_tab_cols
  WHERE column_name = 'dt_imp_banco' AND table_name = 'cliente';

  IF (v_column_exists > 0) THEN
      SELECT cd_cliente, nm_cliente, dt_imp_banco FROM dbamv.cliente;
  END IF;
END;

edited

After the comments in this answer the idea follows the same, check if the column exists before the query. However, since the need is for select to be done regardless of the existing column (in case the query is not made by the other fields), the suggestion would be to create individual validations per column that may exist.

DECLARE
  exist_dt_imp_banco number := 0;  

BEGIN
  SELECT count(*) INTO exist_dt_imp_banco
  FROM user_tab_cols
  WHERE column_name = 'dt_imp_banco' AND table_name = 'cliente';

  SELECT
    cd_cliente,
    nm_cliente,
    CASE exist_dt_imp_banco WHEN 0 THEN null ELSE dt_imp_banco END
  FROM dbamv.cliente;
END;

Thus, if the column exists (in the example, exist_dt_imp_banco > 0), the value of the column in the bank would be consulted; otherwise it would bring null.

DETAIL: the point is that in cases where it is necessary to validate many columns, the code would be very large (and perhaps costly).

Here is an example with three columns to be validated.

DECLARE
  exist_dt_imp_banco number := 0;  
  exist_dt_imp_banco_2 number := 0;  
  exist_dt_imp_banco_3 number := 0;  

BEGIN
  SELECT count(*) INTO exist_dt_imp_banco
  FROM user_tab_cols
  WHERE column_name = 'dt_imp_banco' AND table_name = 'cliente';

  SELECT count(*) INTO exist_dt_imp_banco_2
  FROM user_tab_cols
  WHERE column_name = 'ADD_TMSdt_imp_banco_2' AND table_name = 'cliente';

  SELECT count(*) INTO exist_dt_imp_banco_3
  FROM user_tab_cols
  WHERE column_name = 'dt_imp_banco_3' AND table_name = 'cliente';

  SELECT
    cd_cliente,
    nm_cliente,
    CASE exist_dt_imp_banco WHEN 0 THEN null ELSE dt_imp_banco END,
    CASE exist_dt_imp_banco_2 WHEN 0 THEN null ELSE dt_imp_banco_2 END,
    CASE exist_dt_imp_banco_3 WHEN 0 THEN null ELSE dt_imp_banco_3 END
  FROM dbamv.cliente;
END;
  • Now that I understand the question !

  • mass.. then if you know any form "direct", already answers :P

  • Direct in the query I think does not exist. Your answer I think is on the right track. : D

  • See well.. the table exists... but the field does not!

  • My goal is to create a select that ignores fields that do not exist

  • Are there many validations? Because an alternative would be to use one marry in the select itself..

  • how would that be? Could you show some example ?

  • SQL would still have to be dynamic because otherwise it would give build error in plsql.

  • OK. As for the example, could it inform some ?

  • I wouldn’t make this solution that you want because it is dangerous the column can be important for the application.would make a good old version control. In which your system has been implemented ?

  • Poes is not an effective version control. Tables are changed without warning. what screws the systems

  • We have to fix each client because select gives error if one of the columns is not in the list.

  • My purpose is to create a select that brings the existing fields and ignore the missing fields

  • So you don’t have the "candidates"? If not, I think the question is a little different than what we’re trying so far.. there would be an easier approach, like select * from dbamv.cliente

  • I had already thought... But the system would bring everything. Imagine you a system that brings 100,000 customers

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.