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

  • Home
  • SEARCH
  • 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 6998479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:25:00+00:00 2026-05-27T20:25:00+00:00

I am not a programmer, I am struggling a bit with this. I have

  • 0

I am not a programmer, I am struggling a bit with this.

I have a batch file connecting to my PostgreSQL server, and then open a sql script. Everything works as expected. My question is how to pass a variable (if possible) from one to the other.

Here is my batch file:

set PGPASSWORD=xxxx
cls
@echo off
C:\Progra~1\PostgreSQL\8.3\bin\psql -d Total -h localhost -p 5432 -U postgres -f C:\TotalProteinImport.sql

And here’s the script:

copy totalprotein from 'c:/TP.csv' DELIMITERS ',' CSV HEADER;

update anagrafica
set pt=(select totalprotein.resultvalue from totalprotein where totalprotein.accessionnbr=anagrafica.id)
where data_analisi = '12/23/2011';

delete from totalprotein;

This is working great, now the question is how could I pass a variable that would carry the date for data_analisi?
Like in the batch file, “Please enter date”, and then the value is passed to the sql script.

  • 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-05-27T20:25:00+00:00Added an answer on May 27, 2026 at 8:25 pm

    You could create a function out of your your SQL script like this:

    CREATE OR REPLACE FUNCTION f_myfunc(date)
      RETURNS void AS
    $BODY$
    
    CREATE TEMP TABLE t_tmp ON COMMIT DROP AS
    SELECT * FROM totalprotein LIMIT 0; -- copy table-structure from table
    
    COPY t_tmp FROM 'c:/TP.csv' DELIMITERS ',' CSV HEADER;
    
    UPDATE anagrafica a
    SET    pt = t.resultvalue
    FROM   t_tmp t
    WHERE  a.data_analisi = $1
    AND    t.accessionnbr = a.id;
    
    -- Temp table is dropped automatically at end of session
    -- In this case (ON COMMIT DROP) after the transaction
    
    $BODY$
      LANGUAGE sql;
    

    You can use language SQL for this kind of simple SQL batch.

    As you can see I have made a couple of modifications to your script that should make it faster, cleaner and safer.

    Major points

    • For reading data into an empty table temporarily, use a temporary table. Saves a lot of disc writes and is much faster.

    • To simplify the process I use your existing table totalprotein as template for the creation of the (empty) temp table.

    • If you want to delete all rows of a table use TRUNCATE instead of DELETE FROM. Much faster. In this particular case, you need neither. The temporary table is dropped automatically. See comments in function.

    • The way you updated anagrafica.pt you would set the column to NULL, if anything goes wrong in the process (date not found, wrong date, id not found …). The way I rewrote the UPDATE, it only happens if matching data are found. I assume that is what you actually want.

    Then ask for user input in your shell script and call the function with the date as parameter. That’s how it could work in a Linux shell (as user postgres, with password-less access (using IDENT method in pg_haba.conf):

    #! /bin/sh
    
    # Ask for date. 'YYYY-MM-DD' = ISO date-format, valid with any postgres locale.
    echo -n "Enter date in the form YYYY-MM-DD and press [ENTER]: "
    read date
    
    # check validity of $date ...
    psql db -p5432 -c "SELECT f_myfunc('$date')"
    

    -c makes psql execute a singe SQL command and then exits. I wrote a lot more on psql and its command line options yesterday in a somewhat related answer.

    The creation of the according Windows batch file remains as exercise for you.


    Call under Windows

    The error message tells you:

    Function tpimport(unknown) does not exist

    Note the lower case letters: tpimport. I suspect you used mixe case letters to create the function. So now you have to enclose the function name in double quotes every time you use it.

    Try this one (edited quotes!):

    C:\Progra~1\PostgreSQL\8.3\bin\psql -d Total -h localhost -p 5432 -U postgres
                                        -c "SELECT ""TPImport""('%dateimport%')"
    

    Note how I use singe and double quotes here. I guess this could work under windows. See here.

    You made it hard for yourself when you chose to use mixed case identifiers in PostgreSQL – a folly which I never tire of warning against. Now you have to double quote the function name "TPImport" every time you use it. While perfectly legit, I would never do that. I use lower case letters for identifiers. Always. This way I never mix up lower / upper case and I never have to use double quotes.

    The ultimate fix would be to recreate the function with a lower case name (just leave away the double quotes and it will be folded to lower case automatically). Then the function name will just work without any quoting.

    Read the basics about identifiers here.
    Also, consider upgrading to a more recent version of PostgreSQL 8.3 is a bit rusty by now.

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

Sidebar

Related Questions

I am not professional programmer so i can not be sure about this.How many
I'm not a Notes programmer, however, for my sins, have been working on some
I'm not a C++ programmer and have difficulty understanding the explanations given on websites.
Hi everyone this is my first question here and im not a programmer. I
1st of all: I'm not programmer, neither Linux guru, just have to work with
I am not a programmer, I just a web designer with a bit of
1st of all: I'm not a programmer, never learnt programming/algorithms. Actually I have to
I have no experience in using vba. I am not a programmer. I have
I'm not a programmer/scripter. I just need to get the following script to write
Disclaimer: I'm not a programmer, never was, never learned algorithms, CS, etc. Just have

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.