Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a range of values and then use them to insert data into postgresql database

Tags:

postgresql

Background Information:

I need to auto generate a bunch of records in a table. The only piece of information I have is a start range and an end range. Let's say my table looks like this:

     id 
     widgetnumber

The logic needs to be contained within a .sql file.
I'm running postgresql

Code

This is what I have so far... as a test... and it seems to be working:

DO $$
DECLARE widgetnum text;
BEGIN
    SELECT 5 INTO widgetnum;

    INSERT INTO widgets VALUES(DEFAULT, widgetnum);
END $$;

And then to run it, I do this from a command line on my database server:

testbox:/tmp# psql -U myuser -d widgets -f addwidgets.sql 
DO

Questions

  1. How would I modify this code to loop through a range of widget numbers and insert them all? for example, I would be provided with a start range and an end range (100 to 150 let's say)

  2. Can you point me to a good online resource to learn the syntax i should be using?

Thanks.

like image 252
Happydevdays Avatar asked Dec 23 '16 16:12

Happydevdays


People also ask

How do I add multiple values in PostgreSQL?

PostgreSQL INSERT Multiple Rows First, specify the name of the table that you want to insert data after the INSERT INTO keywords. Second, list the required columns or all columns of the table in parentheses that follow the table name. Third, supply a comma-separated list of rows after the VALUES keyword.

How do I create a data script in PostgreSQL?

To generate SQL scripts from your PostgreSQL project click the SQL Script icon on the main toolbar. New modal opens. Click Save Script and select a location where the file should be stored. Option Overwrite existing files allows you to ignore existing scripts and overwrite them without getting a warning.

What is the best way to transfer the data in a PostgreSQL database?

Data export with pg_dump From the PostgreSQL docs: The idea behind this dump method is to generate a file with SQL commands that, when fed back to the server, will recreate the database in the same state as it was at the time of the dump. PostgreSQL provides the utility program pg_dump for this purpose.

Which SQL statement is used to insert new data in a database in PostgreSQL?

In PostgreSQL, the INSERT statement is used to add new rows to a database table. As one creates a new database, it has no data initially. PostgreSQL provides the INSERT statement to insert data into the database. Syntax: INSERT INTO table(column1, column2, …) VALUES (value1, value2, …);


1 Answers

How would I modify this code to loop through a range of widget numbers and insert them all?

You can use generate_series() for that.

insert into widgets (widgetnumber)
select i
from generate_series(100, 150) as t(i);

Can you point me to a good online resource to learn the syntax i should be using?

https://www.postgresql.org/docs/current/static/index.html

like image 190
a_horse_with_no_name Avatar answered Sep 22 '22 14:09

a_horse_with_no_name