H2 database, insert by selecting results from CSVREAD - sql

I have a CSV file like
1,hello,13
2,world,14
3,ciao,26
I'm trying to use CSVREAD function to read this file into database, like this
insert into my_table( id, message, code ) values (
select convert( "id",bigint ), "message", convert( "code", bigint)
from CSVREAD( 'myfile.csv', 'id,message,code', null )
);
For some reason I keep on getting SQL error stating that the column count does not match.
The table is created with Hibernate/GORM and contains the fields I try to insert into.
The select itself seems to work, or at least it does not cause any errors when executed alone. What's wrong with my statement?

You have used
insert into my_table(...) values (select ...)
but you should use, as documented in the SQL railroad diagrams,
insert into my_table(...) select ...
Actually, for H2, it is a bit faster if you create the table as follows, but I understand it is not always possible:
create table my_table(...) as select ...

Related

Inserting a SQL statement as a record value into a PostgreSQL column

I work with an application that uses PostgreSQL 10.7. Part of the application allows you to bundle a group of of database objects and SQL statements into a package that you can later run when creating a Dev environment.
Each object and SQL statement has its own record in the database. I have to create over 1000 records so I am trying to create a script that will insert the SQL statements into the database for me.
I created my script but I am getting an error once Postgres sees the second "Value" command that is part of the record I am trying to insert.
Here is an example of what I am trying to do:
````insert into MY_TABLE
( NAME,
SQL_STMT,
ADDED_BY,
DATE_ADDED )
values
( 'package_1',
'INSERT INTO TABLE_1(NAME, OBJECT_ID, ORDER_NUMBER) VALUES
'LCMSMS','PEST',1);'
'CJONES',
'9/11/2019' );````
I am expecting it to be inserted but I am getting the following error. Can anyone guide me on how to "insert my insert statement"?
LINE 8: ...NAME,SQL_STMT,ADDED_BY,DATE_ADDED) VALUES ('LCMSMS...````
Your SQL statement contains emmbedded quotes, that clash with the surrounding quotes. You would need to double these quotes, like:
````insert into MY_TABLE
( NAME,
SQL_STMT,
ADDED_BY,
DATE_ADDED )
values
( 'package_1',
'INSERT INTO TABLE_1(NAME, OBJECT_ID, ORDER_NUMBER) VALUES (''LCMSMS'', ''PEST'', 1);'
'CJONES',
'9/11/2019' );````
As commented by mu is too short, another solution would be to use Postgres dollar quoting syntax. This saves you the effort of double quoting each and every embedded quote:
````insert into MY_TABLE
( NAME,
SQL_STMT,
ADDED_BY,
DATE_ADDED )
values
( 'package_1',
$$INSERT INTO TABLE_1(NAME, OBJECT_ID, ORDER_NUMBER) VALUES ('LCMSMS', 'PEST', 1);$$
'CJONES',
'9/11/2019' );````

Can't insert into table after CAST(SUBSTR(column,8,6) as number(6))

I have a table where I inserted my fixed file data to single varchar2 column and called this table tmp_table
CREATE TABLE "tmp_table"
( "COLUMN1" VARCHAR2(256 BYTE)
)
Now I am trying to transform and insert data from this tmp_table to final table where I am breaking the data using SUBSTR function
Now I created my next table where I will insert these columns
CREATE TABLE "TABLE"
(
"COLUMN A" VARCHAR2(4),
"CODE" NUMBER(6,0),
"DATE_LOADED_TIMESTAMP" DATE
)
Now I run a select query to check if I can get right data from my tmp_table that I want to insert to final TABLE
So I run the query
SELECT Cast(SUBSTR(COLUMN1,8,6)as number(6)) as Code
from TMP_TABLE;
The results I see were good and what I want to insert into CODE column.
Now I try to run final query:
insert into TABLE(CODE)
SELECT Cast(SUBSTR(COLUMN1,8,6)as number(6)) as Code
from TMP_TABLE;
Now it gives me error
Error report -
ORA-01722: invalid number
Why am I not able to insert number values to a 6 digit number column?
Ok I found what happened I was using SQL Developer and the query I ran below was returning on some results in my tool and the few results it was showing there was no characters in those fields.
SELECT Cast(SUBSTR(COLUMN1,8,6)as number(6)) as Code
from TMP_TABLE;
I ran following query to search if there are characters in the records I am looking at:
SELECT SUBSTR(COLUMN1,8,6)
from TMP_TABLE
where REGEXP_LIKE(SUBSTR(COLUMN1,8,6),'[A-Za-z]');
and here I saw characters now I know not to trust SQL developer blindly :)

Difference between INSERT INTO and SELECT INTO when calling OPENROWSET

select 1 as X,d.* into [TravelData] from OPENROWSET('SQLNCLI','Server=<redacted>',
'exec [OtherDB].[GetTravelData] 1, ''28-Nov-2016 16:00'', ''28-Nov-2016 19:00''') as d
I have this as a way to slurp remote DB into a local table. This syntax appears to work BUT I get error:
There is already an object named 'TravelData' in the database.
Makes sense, SELECT INTO is supposed to create the table. But thinking I'd simply change SELECT to INSERT I then get syntax errors. What should the correct syntax be to get this data into an existing DB table whose structure matches the query output?
It has nothing to do with using OPENROWSET.
INSERT INTO ... requires that the table already exist.
SELECT ... INTO requires that the table not exist. The table will be created by the statement using the columns defined in the SELECT.
Here is the INSERT INTO SELECT syntax
INSERT INTO [TravelData]
(X,
col1,
col2,
...)
SELECT 1 AS X,
d.col1,
d.col2,
.....
FROM OPENROWSET('SQLNCLI',
'Server=<redacted>',
'exec [OtherDB].[GetTravelData] 1, ''28-Nov-2016 16:00'', ''28-Nov-2016 19:00''') AS d
Note : Instead of * in select list add the column list. Also in Insert mention the column list

DB2 storing results from final table clause

The FINAL TABLE clause is great for getting values back from DML in DB2, for example:
SELECT id
FROM FINAL TABLE
(
INSERT INTO mySchema.myTable (val)
VALUES ('data')
)
However, there doesn't seem to be a way to store the results of this query into another table, persisting the contents somewhere. For example, both of the following fail with the error "Data change table reference not allowed where specified." (I am running DB2 for i v7.1):
CREATE TABLE mySchema.otherTable AS (
SELECT id
FROM FINAL TABLE
(
INSERT INTO mySchema.myTable (val)
VALUES ('data')
)
) WITH DATA
After creating mySchema.otherTable in a separate CREATE TABLE statement, this also fails:
INSERT INTO mySchema.otherTable (ID)
SELECT id
FROM FINAL TABLE
(
INSERT INTO mySchema.myTable (val)
VALUES ('data')
)
Not sure if this works on i Series, but DB2 for LUW allows you to do this:
with i1 (id) as (
SELECT id
FROM FINAL TABLE
(
INSERT INTO mySchema.myTable (val)
VALUES ('data')
)
)
select * from new table (
INSERT INTO mySchema.otherTable (ID)
select id from i1
)
I tried to use the FINAL TABLE technique today on an IBM i at OS V7R1 and it wouldn't work as described on DB2 for LUW, when attempting to feed the identity column value to a second insert. I anticipate we'll get this ability eventually.
As an alternative, I was able to route the assigned identity column value to an SQL Global Variable using a SET command, and then use that Global Variable to assign the same identity column value to 2 subsequent inserts to 2 related association tables. For non-compiled SQL scripting, that is a good technique to use for a server side solution until we get the same ability as described on DB2 for LUW. A temp table would work as well.
create variable MY_SCHEMA.MY_TABLE_ID
;
set MY_SCHEMA.MY_TABLE_ID =
( select ID
from final table ( insert into MY_SCHEMA.MY_TABLE values ('data') ) )
;
insert into MY_SCHEMA.MY_OTHER_TABLE ( ID, DATA )
values( MY_SCHEMA.MY_TABLE_ID, 'more data' )
;
From the V7R1 SQL Reference manual:
Global variables have a session scope. This means that although they are
available to all sessions that are active on the database, their value is private for
each session.
For compiled SQL stored procedures, a variable with a SELECT INTO works fine too.

SQL: Insert multiple sets of values in one statement?

Is it possible to insert multiple sets of values to a SQLite table in one statement?
I was trying:
INSERT INTO the_table VALUES (1,2,'hi'),(2,0,'foo');
with the different ()s representing different insert sets, but I get an error.
Are there only three columns in your table? If not, you could try defining the column names you are setting like so:
INSERT INTO the_table
(column1 ,column2 ,column3)
VALUES (1 ,2 ,'hi' )
,(2 ,0 ,'foo' )
This convention was introduced in SQL Server 2008 known as the Table Value Constructor. See MSDN's INSERT page for a look at the overall syntax. Also, the INSERT statement can be easily formatted for better readability.
You can do
INSERT INTO the_table
SELECT 1,2,'hi'
UNION
SELECT 2,0,'foo';
I was found that syntax in MSDN but after trying I can't do that too, than I note that in the bottom of the page was written that there is an error in the page :) where is link http://msdn.microsoft.com/en-us/library/ms174335.aspx see the bottom How to insert multiple rows