how to execute multiple insert statements from HANA at once - hana

I am having a file file.txt which consists of millions of insert commands like below:
"INSERT INTO T (id, serial) VALUES (0, ARRAY (1) );"
"INSERT INTO T (id, serial) VALUES (1, ARRAY (1, 2) );"
"INSERT INTO T (id, serial) VALUES (2, ARRAY (1, 2, 3, 4, 5) );"
"INSERT INTO T (id, serial) VALUES (3, ARRAY (1, 2) ); "
"INSERT INTO T (id, serial) VALUES (4, ARRAY (1, 2, 3) );"
.....
.....
.....
I want to execute them in HANA using hdbsql or Studio. But how to execute them from the file? Any help is appreciated.

Ideally, the commands shouldn't be enclosed in quotation marks.
Then you can either just run the file as-is in HANA studio (only a good idea if your file is not actually MILLIONS of files, since HANA studio parses the SQL commands first and likely will stall, when there are too many), or you run it via hdbsql with the -I option:
hdbsql -U <your secure store logon key here> -I insert.sql
Thinking about it, the latter method would be the one I would likely go for.

Related

SQL Server : newline character

I have a programming language that does not allow me to write queries on multiple lines. It has to be written all in a single line.
I am unable to send a GO command because it has to be in a new line..
So for example, this does not work:
insert into mytable (field1, filed2) values (1, 2), (3, 4); go
as it should be
insert into mytable (field1, filed2) values (1, 2), (3, 4);
go
I've tried multiple things but none worked:
insert into mytable (field1, filed2) values (1,2),(3,4); \r go
insert into mytable (field1, filed2) values (1,2),(3,4); \r\n go
insert into mytable (field1, filed2) values (1,2),(3,4); $r$n go
insert into mytable (field1, filed2) values (1,2),(3,4); char(10) go
insert into mytable (field1, filed2) values (1,2),(3,4); char(13) go
Is there a way to write it inline, and have SQL Server use it as 2 different lines?
GO is not part of the TSQL language. It is used and recognized only by SSMS and sqlcmd to cut your script into parts ("batches"), and then each part is compiled and run separately, one after the other.
The reason that SSMS and sqlcmd work this way this is that it makes it possible to have e.g. a CREATE TABLE statement, followed by INSERT statements for that table. The INSERT-part will only compile if the table already exists, and that will be the case only after the CREATE has been run.
It is OK to combine multiple INSERTs into one statement. When in doubt about where the next statement should start, you can add a semicolon (;) to mark the end of the previous statement.

EXPLAIN Failed. 3706: (-3706)Syntax error: expected something between ')' and ','

I've seen this error mentioned on StackOverflow, but not in the context I am using. I am relatively new to Teradata and this behavior is throwing me for a loop. Here is code that works:
INSERT INTO test_table (this, that) VALUES (1, 2);
Here is code that throws the error:
INSERT INTO test_table (this, that) VALUES (1, 2), (3, 4);
This is super confusing to me because the Teradata docs have the following example:
INSERT INTO cities VALUES (2, 'San Jose'), (3, 'Oakland');
Could someone show me what am I missing here? Thanks!
Teradata only allows you to insert one record with a single values. You can see this in the syntax diagram for insert -- there is no "backloop".
Two inserts is a simple workaround:
INSERT INTO test_table (this, that)
VALUES (1, 2);
INSERT INTO test_table (this, that)
VALUES (3, 4);

How to manually insert a BLOB into a SQLite database

I want to know how to manually insert a BLOB into my SQLite database. By manually I mean, without using a driver feature that will complete the command like setBytes:
Connection con = DriverManager.getConnection("jdbc:sqlite:database.db");
PreparedStatement stmt = con.prepareStatement("INSERT OR REPLACE INTO test (id, aBlobColumn) VALUES (0, ?)";
stmt.setBytes(1, new byte[] {0x37, 0xe7, 0x9f});
stmt.executeUpdate();
Is it possible to use a command like that:
INSERT OR REPLACE INTO test (id, aBlobColumn) VALUES (0, 37e79f);
or like that:
INSERT OR REPLACE INTO test (id, aBlobColumn) VALUES (0, BLOB(37, e7, 9f));
I don't mind if the command includes base64 data or raw data, I don't want to specifically use hexadecimal.
You can use the following :-
INSERT OR REPLACE INTO test (id, aBlobColumn) VALUES (0, x'37e79f');
However, the value has to be a hex string for it to be a BLOB.

Postgres insert into a table

I have a SQL script like this which I run from the command line using psql:
insert into "A"."B" values
(1, 'name=a', 'a#example.com', 'K')
How do I convert it into INSERT command inside a database?
INSERT INTO "A"."B" (first_column, second_c, third_c, fourth_1)
VALUES ('2', 'name=a', 'a#example.com.com', 'K');
Also what does "A"."B" do? I read somewhere that double quotes are needed when table name has Capitals. I seem to get an error with that when I run commands inside the database.
You said that your database name was DB and your table name was B.
You can simply use the table name alone:
INSERT INTO "B" (first_column, second_c, third_c, fourth_1)
VALUES ('2', 'name=a', 'a#example.com.com', 'K');
If you want to include the database name, then use:
INSERT INTO "DB"."B" (first_column, second_c, third_c, fourth_1)
VALUES ('2', 'name=a', 'a#example.com.com', 'K');
The double quotes are only required when the name of any entity (e.g. table, column, etc...) is a reserved word.
You can use this query where A is schema and B is table name.
INSERT INTO "A"."B" (first_column, second_c, third_c, fourth_1)
VALUES ('2', 'name=a', 'a#example.com.com', 'K');

Add file to blob fileld in sqlite db

How can I add image file, for example, with name '001.png' to sqlite3 database using its command prompt?
sqlite3> INSERT INTO test (id, name, blob_field) VALUES (NULL, 'Bob', ?????);
Depending on the OS you're using, you could convert the file into a hexdump and use that to construct an SQL command with a blob literal:
(printf "INSERT INTO test(id, name, blob_field) VALUES(NULL, 'Bob', 0x" ; \
hexdump -v -e '/1 "%02x"' 001.png ; printf ");" ) | sqlite3 my.db
Use hex literal like 0x123456789abcdef
so
INSERT INTO test (id, name, blob_field) VALUES (NULL, 'Bob', 0xB0B1);