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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:38:00+00:00 2026-05-16T12:38:00+00:00

In MS SQL Server, I create my scripts to use customizable variables: DECLARE @somevariable

  • 0

In MS SQL Server, I create my scripts to use customizable variables:

DECLARE @somevariable int  
SELECT @somevariable = -1

INSERT INTO foo VALUES ( @somevariable )

I’ll then change the value of @somevariable at runtime, depending on the value that I want in the particular situation. Since it’s at the top of the script it’s easy to see and remember.

How do I do the same with the PostgreSQL client psql?

  • 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-16T12:38:00+00:00Added an answer on May 16, 2026 at 12:38 pm

    Postgres variables are created through the \set command, for example …

    \set myvariable value
    

    … and can then be substituted, for example, as …

    SELECT * FROM :myvariable.table1;
    

    … or …

    SELECT * FROM table1 WHERE :myvariable IS NULL;
    

    edit: As of psql 9.1, variables can be expanded in quotes as in:

    \set myvariable value 
    
    SELECT * FROM table1 WHERE column1 = :'myvariable';
    

    In older versions of the psql client:

    … If you want to use the variable as the value in a conditional string query, such as …

    SELECT * FROM table1 WHERE column1 = ':myvariable';
    

    … then you need to include the quotes in the variable itself as the above will not work. Instead define your variable as such …

    \set myvariable 'value'
    

    However, if, like me, you ran into a situation in which you wanted to make a string from an existing variable, I found the trick to be this …

    \set quoted_myvariable '\'' :myvariable '\''
    

    Now you have both a quoted and unquoted variable of the same string! And you can do something like this ….

    INSERT INTO :myvariable.table1 SELECT * FROM table2 WHERE column1 = :quoted_myvariable;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I perform a select/Insert query, does SQL Server automatically create an implicit transaction
I have a sql server table: CREATE TABLE [Workflow].[MilestoneDate]( [MilestoneDateId] [int] IDENTITY(1,1) NOT NULL,
SQl Server 2005: Option: 1 CREATE TABLE #test (customerid, orderdate, field1 INT, field2 INT,
I am writing SQL Server deployment scripts which create SQL Server job automatically on
When I create a script of the table in SQL Server (2008), Use [SampleDataBase]
In my SQL Server 2005 server I create databases and logins using Management Studio.
I'm using VB.NET and Microsoft SQL Server 2005 to create an application. I was
I know how to create a job dynamically from this create-sql-server-job-automatically The issue I
I am trying to create an add-in by using the article below http://blogs.microsoft.co.il/blogs/shair/archive/2008/07/28/how-to-create-sql-server-management-studio-addin.aspx I
SQL Server Mgmt Studio is driving me crazy. If I create a view and

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.