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 7848611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:16:03+00:00 2026-06-02T18:16:03+00:00

I want to query PostgreSQL database, which contains lists of info, for a simple

  • 0

I want to query PostgreSQL database, which contains lists of info, for a simple count of current records from within MSSQL. The PG server is a linked server in MSSQL.

I know very little about Postgre but have enough to be dangerous. I created a simple query to reside inside a function as follows:

CREATE FUNCTION get_count(id text, division text, company integer, INOUT recCount integer) AS $$
BEGIN
recCount := (SELECT COUNT(0)
               FROM test.inventory
               WHERE inv_id = $1 AND div_code = $2 AND cmpy_no = $3);
END;
$$ LANGUAGE plpgsql;

I can query it all day long like this:

SET @Query = 'SELECT test.get_count(''' + @Id+ ''', ''' + @Div + ''', ' + CAST(@Company AS VARCHAR) +', ?);'
EXEC (@Query) AT POSTGRESQLTEST;

But I can’t assign it to a variable like this:

SET @i = EXEC (@Query) AT POSTGRESQLPROD;
-- Error: Incorrect syntax near the keyword 'EXEC'.

Office mates say store the value in a temp table, query it, then drop it. Not very efficent IMHO but it will get the job done.

So what else can I do? How can I leverage the INOUT parameter recCount from the PG server?

  • 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-02T18:16:05+00:00Added an answer on June 2, 2026 at 6:16 pm

    Edited with feedback from comments below.
    I think this should work with OpenQuery:

    SELECT * FROM OpenQuery(POSTGRESQLTEST,
                    'SELECT test.get_count(''some_inv_id'', ''some_div_code'', 123')
    

    As @Andriy pointed out, OpenQuery does not accept variables. For quasi-dynamic SQL you have to assemble the whole query string before you can execute it. You’ll have to double up single quotes around strings for that – twice!

    Here is an example on .databasejournal.com how it can be done.
    And @sighlent provided his solution below.


    As an aside, concerning the plpgsql function:

    You don’t seem to need an input value for recCount. To simplify things a bit make that an OUT parameter instead of INOUT and you’ll have one less input parameter:

    CREATE OR REPLACE FUNCTION x.get_count(id text, division text, company integer
                                                           , OUT recCount integer) AS
    $BODY$
    BEGIN
        recCount := (
           SELECT COUNT(*)
           FROM   test.inventory
           WHERE  inv_id   = $1
           AND    div_code = $2
           AND    cmpy_no  = $3);
    END;
    $BODY$ LANGUAGE plpgsql;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to query Active Directory from Windows PE 2.0, which is not supported
I want to vacuum a PostgreSQL database from PHP. I have tried this: pg_query($conn,vacuum
I want to get the contents from a row in the Postgresql database and
I am using PostgreSQL Database. I want to get a column values from a
I want to remove duplicate rows return from a SELECT Query in Postgres I
I'm accepting dates from a wicket form - now I want query my DAO
I am using a PostgreSQL database. I have one select query: select userid, name,
I have a Spring application which uses Hibernate on a PostgreSQL database. I'm trying
I have some columns in PostgreSQL database that are array. I want to add
I have a database of records that I want to select the most recent

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.