Populate Oracle table with test data for PL/SQL generator - sql

I would like to write PL/SQL generator of dummy data (e.g. names as combination of first and last most popular names) for Oracle 12c.
So I need to populate names and surnames tables with source data first.
I cannot use sqlldr and all I have access to is SQL*Plus and SQLDeveloper.
I can populate my source tables with lots of individual insert statements like:
INSERT INTO names(id, name) VALUES(1, 'Oliver');
INSERT INTO names(id, name) VALUES(2, 'Jack');
⋮
INSERT INTO names(id, name) VALUES(50, 'Aaron');
I wonder whether there are any other (perhaps more elegant) options.
Edit:
I was hitting another issue coming from a fact that value of generated identity column is not incremented during INSERT ALL. There is a nice workaround described in this related answer at SO.

You can do this in oracle:
INSERT INTO names(id, name)
select 1, 'Oliver' from dual union all
select 2, 'Jack' from dual union all
. . .
Then there is INSERT ALL:
INSERT ALL
INTO names (id, name) VALUES (1, 'Oliver')
INTO names (id, name) VALUES (2, 'Jack')
select * from dual;
I like the first one better.

You might also want to check out http://plsql.ninja/npg/package/random_ninja. Morten Egan has been building a very interesting library of utilities and his random_ninja package will generate all sorts of data for you.

Related

need to combine 3 sql-statement into 1

I have 3 SQL-Statements that I would like to combine into just one so I dont have to make multiple requests to my database from my programm (java).
My DB is PostgreSQL 9.4
First one creates a new user in umgmt_users
INSERT INTO umgmt_users ("user") VALUES ('test1')
Second one gets the id of that user (db is postgres and id data type is serial, so it get assigned automatically with me/the programm not knowing what id the user will get
SELECT umgmt_users.id
FROM umgmt_users
WHERE umgmt_users.user = 'test1'
Thrird is to add the just created user with his id (which I need the second statement for) and some other values into a different table
INSERT INTO
umgmt_user_oe_fac_role ("user_id", "oe_id", "fac_id", "role_id")
VALUES ('ID OF USER test1 created in first statement', '1', '2', '1');
Is there a way to get all three Statements into one?
create user
look up the ID he got assigned
insert his ID + other values into a different table
I'm not that good at SQL, I tried to put brackets around the select and put it into the insert & also looked at UNION and WITH but can not get it to work...
EDIT: Ended up using this solution from a_horse_with_no_name
with new_user as (
INSERT INTO umgmt_users ("user") VALUES ('test1')
returning id
)
INSERT INTO umgmt_user_oe_fac_role (user_id, oe_id, fac_id, role_id)
SELECT id, 1, 2, 1
FROM new_user;
All you need is two inserts:
INSERT INTO umgmt_users ("user") VALUES ('test1');
INSERT INTO umgmt_user_oe_fac_role (user_id, oe_id, fac_id, role_id)
VALUES (lastval(), 1, 2, 1);
In order for lastval() to work correctly there must be no other statement between the two inserts and the have to be run in a single transaction (so autocommit needs to be turned off)
Alternatively you can use a data modifying CTE which is then executed as a single statement:
with new_user as (
INSERT INTO umgmt_users ("user") VALUES ('test1')
returning id
)
INSERT INTO umgmt_user_oe_fac_role (user_id, oe_id, fac_id, role_id)
SELECT id, 1, 2, 1
FROM new_user;
Please don't put numbers in single quotes.
The answer to this is : It's impossible to combine these into a single plain vanilla ANSI SQL statement.
The first and third ones talk about two different tables altogether.
The second one is a Select Statement which is a different type of statement from the other two.

SQL: Insert multiple row with common columns

I have a table into which i will insert and it has 4 columns.
While inserting 3 columns will be same and the other one column will be different for each and it will be taken from another table.
How could i do that?
For exmaple;
INSERT INTO sendMsg (Type,Name,SenderName,Message) values(4, 'john','Mike','Hi, blabla')
i will insert same message also for Bob, instead of john.
and the names which i will send are contained in Names table.
Thank you.
Use a select statement to build up your insert. Something like this could work (as you didn't provide more details):
INSERT INTO sendMsg (Type,Name,SenderName,Message)
SELECT 4, "name" ,'Mike','Hi, blabla' FROM anothertable
-- WHERE ....
Column names are in ", so don't be confused. It's to ensure difference between string and database object.
Inside optinal WHERE you could do maybe something like
WHERE name in ('Bob', 'John', ...)
or whichever algorithm you need to determine the names.

Using the Insert Into command correctly in SQL Server 2008

I just started to learn some SQL syntaxes and now i stuck already when I try to insert data from a Query into a specific database.
I have 5 DB's
db1,db2,db3,db4,db5
in every db there are the same tables lets say dbo.fruits
when I now select db2 and open a query I can easly enter new fruits into my db2 by using :
Insert into dbo.Fruits (Name, Description)
values ('banana', 'yummy');
But if I don't select db2 before. How do I enter my bananas into dbo.fruits from db2 ?
I thought
Insert into [db2] dbo.Fruits (Name, Description)
values ('banana', 'yummy');
but it's not working :/, of course I googled, but didnt find the right answer.
Insert into [db2].dbo.Fruits (Name, Description)
values ('banana', 'yummy');
or if your user is dbowner you can do
Insert into [db2].Fruits (Name, Description)
values ('banana', 'yummy');
The easiest will be to remember to put the database name first for every SQL statement/query.
Something like: SELECT * FROM [db1].[dbo].[Fruits]
ie. ...[DatabaseName].[SchemaName].[TableName]

SQL Server Insert Example

I switch between Oracle and SQL Server occasionally, and often forget how to do some of the most trivial tasks in SQL Server. I want to manually insert a row of data into a SQL Server database table using SQL. What is the easiest way to do that?
For example, if I have a USERS table, with the columns of ID (number), FIRST_NAME, and LAST_NAME, what query do I use to insert a row into that table?
Also, what syntax do I use if I want to insert multiple rows at a time?
To insert a single row of data:
INSERT INTO USERS
VALUES (1, 'Mike', 'Jones');
To do an insert on specific columns (as opposed to all of them) you must specify the columns you want to update.
INSERT INTO USERS (FIRST_NAME, LAST_NAME)
VALUES ('Stephen', 'Jiang');
To insert multiple rows of data in SQL Server 2008 or later:
INSERT INTO USERS VALUES
(2, 'Michael', 'Blythe'),
(3, 'Linda', 'Mitchell'),
(4, 'Jillian', 'Carson'),
(5, 'Garrett', 'Vargas');
To insert multiple rows of data in earlier versions of SQL Server, use "UNION ALL" like so:
INSERT INTO USERS (FIRST_NAME, LAST_NAME)
SELECT 'James', 'Bond' UNION ALL
SELECT 'Miss', 'Moneypenny' UNION ALL
SELECT 'Raoul', 'Silva'
Note, the "INTO" keyword is optional in INSERT queries. Source and more advanced querying can be found here.
Here are 4 ways to insert data into a table.
Simple insertion when the table column sequence is known.
INSERT INTO Table1 VALUES (1,2,...)
Simple insertion into specified columns of the table.
INSERT INTO Table1(col2,col4) VALUES (1,2)
Bulk insertion when...
You wish to insert every column of Table2 into Table1
You know the column sequence of Table2
You are certain that the column sequence of Table2 won't change while this statement is being used (perhaps you the statement will only be used once).
INSERT INTO Table1 {Column sequence} SELECT * FROM Table2
Bulk insertion of selected data into specified columns of Table2.
.
INSERT INTO Table1 (Column1,Column2 ....)
SELECT Column1,Column2...
FROM Table2
I hope this will help you
Create table :
create table users (id int,first_name varchar(10),last_name varchar(10));
Insert values into the table :
insert into users (id,first_name,last_name) values(1,'Abhishek','Anand');
For example, "person" table has "id" IDENTITY column as shown below:
CREATE TABLE person (
id INT IDENTITY, -- Here
name NVARCHAR(50),
age INT,
PRIMARY KEY(id)
)
Then, we don't need to manually put a value to "id" IDENTITY column when inserting a row:
INSERT INTO person VALUES ('John', 27) -- The value for "id" is not needed
And, we can also insert multiple rows without the values for "id" IDENTITY column:
INSERT INTO person VALUES ('John', 27), ('Tom', 18)

How might I insert an array of data into a database table?

I am developing an attendance management program, used to maintain the absence record of a student. Users of this software will need to enter various dates, updated once in a month: for instance, a list of dates on which a student was absent for that particular month would be entered, and my program must then store them into a database with each date added as a new row in the appropriate table.
I have the dates stored using arrays internally, how might I transfer these into the database? How should I proceed?
You have not mentioned the database system being used, so my reply is general in nature. The usual way to do this is to run multiple insert statements one after another:
INSERT INTO Table1 (FirstColumn, SecondColumn)
VALUES ('a', 'b');
INSERT INTO Table1 (FirstColumn, SecondColumn)
VALUES ('c', 'd');
INSERT INTO Table1 (FirstColumn, SecondColumn)
VALUES ('e', 'f');
GO
The trick way to do this is to use the UNION ALL statement:
INSERT INTO Table1 (FirstColumn, SecondColumn)
SELECT 'a', 'b'
UNION ALL
SELECT 'c', 'd'
UNION ALL
SELECT 'e', 'f'
GO
Versions of SQL Server prior to 2008 support only these methods. But SQL 2008 and MySQL 3.22 and above support the Row construction method as well:
INSERT INTO Table1 (FirstColumn, SecondColumn)
VALUES ('a', 'b'),
VALUES ('c', 'd'),
VALUES ('e', 'f')
GO
Now you can use any of the above methods to iterate through your array and add individual attendance rows to the database.
foreach($arrayName as $arrayValue) {
// run your query here!
}
for example:
$myArray = array('apple','orange','grape');
foreach($myArray as $arrayFruit) {
$query = "INSERT INTO `Fruits` (`FruitName`) VALUES ('" . $arrayFruit . "')";
mysql_query($query, $connection);
}
does that makes sense / fit what you were thinking?
Do you want to store the dates seperately so you can juggle with them, query them, etc.?
Or do you just want to store the array as is?
If you want to store the dates separately you may want to create a table with an FK to students, a column for date and a column for the nature of the date, like absence, late, ...
Then you would indeed store the single dates into that table. If you must, by iterating but if you can with one of Cerbrus' solutions!. It is not recommended to have db-queries within loops.
If you just need to store that array somewhere, you can serialize it and store the serialized string in a text or varchar column.
What language and what type of database are you using? Is this a web application? A desktop application? We can't help you without more information.
Depending on your situation, any of the above solutions could work. Or you could even load all of the attendance records at once as a CSV or XML document. Perhaps a little more research on your part will help you ask a more useful question?
Iterate over the array and execute insert SQL for each date.