HSQL - insert into table with multiple values - hsqldb

There is a large HSQL-import-data.sql scripts. I lists all inserts which are required to fill the database with
INSERT INTO table (name, firstname) VALUES ('john', 'john);
INSERT INTO table (name, firstname) VALUES ('mark', 'mark);
I would prefer to rewrite it like for better readability
INSERT INTO table (name, firstname) VALUES
('john', 'john'),
('mark', 'mark);
Unfortunaletly I do not know to achieve it with hsql. If I put the values all in one line it works, but then the script becomes not more readable. So how i mark that a statement continues on the next line. Just as an example in the bash world this would be the \character.

Related

Postgres UPSERT reuse column values from INSERT on UPDATE

I have a basic upsert query that works ok:
insert into table (id, data) values (1, '<data_string>')
on conflict (id) do update set data='<data_string>';
The only problem is I need to fly lots of these queries over the network so I wonder if there is a way to cut the traffic in half by not having <data_string> listed twice in the query?
Yes, there is the special table EXCLUDED for the purpose:
INSERT INTO tbl (id, data)
VALUES (1, '<data_string>')
ON CONFLICT (id) DO UPDATE SET data = EXCLUDED.data;
Like the manual explains:
Note that the special excluded table is used to reference values originally proposed for insertion:
Works for multi-row INSERTs as well:
INSERT INTO tbl (id, data)
VALUES (1, '<data_string1>')
, (2, '<data_string2>')
, (3, '<data_string3>')
ON CONFLICT (id) DO UPDATE
SET data = EXCLUDED.data;

Populate Oracle table with test data for PL/SQL generator

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.

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)

Error inserting new rows (foreign key issue) in SQL

I am trying to debug some classic ASP code and the app keeps breaking on an insert statement. The backend is in SQL
The statement looks something like this:
insert into tableX (id, fo, ao) values (12, '', 'ab')
in tableX both fo and ao are set to allow null values.
fo is a foreign key pointing to the foTable (look up table)
I guess it's not liking the single quotes? How else do I go about this in classic asp?
The error I'm getting is:
The INSERT statement conflicted with the FOREIGN KEY constraint
"FK_tableX_FO". The conflict occurred in database "tableX", table
"dbo.FIELD_OFFICE", column 'FO'.
I also want to add that this is a process of migrating from Oracle. Does anyone know if Oracle treats '' as nulls, which is why I'm now getting an error in SQL?
An empty string is not a null value. You need to call it like this
insert into tableX (id, fo, ao) values (12, null, 'ab')
Alternatively
insert into tableX (id, ao) values (12, 'ab')
Either of these statements will work.
insert into tableX (id, ao) values (12, 'ab')
or
insert into tableX (id, fo, ao) values (12, NULL, 'ab')
If fo is set to allow nulls and you don't want to violate the FK constraint, then you should provide NULL instead of an empty string for the fo column.