Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8733225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:36:28+00:00 2026-06-13T09:36:28+00:00

For instance I’ve got a stored procedure to import data from csv files and

  • 0

For instance I’ve got a stored procedure to import data from csv files and write the read data into a SQL table.
I’ve got a table defined as below:

CREATE TABLE person (id int, name text, age int, married boolean); 

First I check if the record exist already, if exists I do update, if doesn’t – insert.
Each field of record may have a different type, so the result of a SQL command are assigned to a list of scalar variables:

SELECT name, age, married INTO v_name, v_age, v_married [..]

Let’s assume every column is declared as optional (NULL allowed). What’s the best way then to check which variable (v_name, v_age, v_married) is not NULL and can be processed?

I’ve found many solutions:

  • IF NOT FOUND THEN
  • WHEN NO_DATA_FOUND THEN
  • IF v_age IS NOT NULL THEN […]

or dynamic solution I’m using now using the last way I’ve mentioned above, when I have to check multiple columns (col):

list_of_columns := ARRAY['name','age','married'];
FOREACH x IN ARRAY list_of_columns LOOP
   EXECUTE 'SELECT ' || x
       || ' FROM person
            WHERE id = ' || quote_literal(v_id)
            INTO y;

   IF  x = 'name' AND (y != v_name OR y IS NULL) THEN
     UPDATE person
     SET    name = v_name
     WHERE  id = v_id;

   ELSIF x = 'age' AND (y != v_age OR y IS NULL) THEN
     UPDATE person
     SET    age = v_age
     WHERE  id = v_id;

   ELSIF x = 'married' AND (y != v_married OR y IS NULL) THEN
     UPDATE person
     SET    married= v_married
     WHERE  id = v_id;
   END IF;
END LOOP;

I’m looking for the best solutions having regard to the best practice and performance.
Any help is appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:36:29+00:00Added an answer on June 13, 2026 at 9:36 am

    I think, you can radically improve the whole procedure along these lines:

    BEGIN;
    
    CREATE TEMP TABLE tmp_p ON COMMIT DROP AS
    SELECT * FROM person LIMIT 0;
    
    COPY tmp_p FROM '/absolute/path/to/file' FORMAT csv;
    
    UPDATE person p
    SET    name    = t.name
          ,age     = t.age
          ,married = t.person
    FROM   tmp_p t
    WHERE  p.id = t.id
    AND   (p.name    IS DISTINCT FROM t.name OR
           p.age     IS DISTINCT FROM t.age  OR
           p.married IS DISTINCT FROM t.married);
    
    INSERT INTO person p(id, name, age, married, ...)
    SELECT id, name, age, married, ...
    FROM   tmp_p t
    WHERE  NOT EXISTS (SELECT 1 FROM person x WHERE x.id = t.id);
    
    COMMIT; -- drops temp table because of ON COMMIT DROP
    

    Explain

    • COPY your CSV file to a temporary table with matching layout. I copied the layout of the target table with CREATE TABLE AS ... LIMIT 0, you may need to adapt …

    • UPDATE existing rows. Avoid empty updates (nothing would change) with the last 3 lines in the WHERE clause.
      If you want to skip NULL values in the UPDATE (do you really?), use expressions like COALESCE(t.name, p.name). That falls back to the existing value in the case of NULL. (May be useful, but that wasn’t actually in your code.)

    • INSERT non-existing rows. Use a NOT EXISTS semi-join for that.

    • All in one transaction so you don’t end up with a half-baked result in the case of a problem along the way. The temp table is dropped at the end of the transaction because I created it that way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For instance, I have a table stores value: select * from myvalue; val -------
For instance say I run an ajax call and the returned data is something
For instance, I have a model class which handles receiving bluetooth messages from other
For instance, here is sample JPEG which I cannot SAVE (!) but can read
For instance what should I add to this expression: xyplot(a~b, groups=abc, data=super, scales=list(x=list(log=TRUE),y=list(log=TRUE)), panel
For instance, 000, 404, and 0523 can be converted into whole numbers in PHP,
for instance if a string is data = success=0 or data = success=1&registration=20; how
For instance can SELECT foo FROM bar WHERE foo BETWEEN 5 AND 10 select
For instance, I read that visual basic .net didn't have operator overloads before, so
For instance, if I have: <div data-bind=visible: viewModel.property1() || viewModel.property2() /> ... if viewModel.property1()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.