simple sql script within a bash fails - sql

#!/bin/bash
mysql -h 172.17.0.1 -P 13306 -u root -p123<<MYSQL_SCRIPT
USE 1DB;
CREATE TABLE 111 (
ID TEXT,
TEST_CASE TEXT
);
INSERT INTO 111 (ID, TEST_CASE)
VALUES ("111", "111");
MYSQL_SCRIPT
when run this script, bash returns: ERROR 1064 (42000) at line 4: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '111 (
ID TEXT,
TEST_CASE TEXT
)' at line 1

Possibly look at this answer - Are you allowed to use numbers as table names in MySQL? - and more specifically the line 'Identifiers may begin with a digit but unless quoted may not consist solely of digits.'
You could try this:
USE 1DB;
CREATE TABLE '111' (
ID TEXT,
TEST_CASE TEXT
);
INSERT INTO '111' (ID, TEST_CASE)
VALUES ("111", "111");
MYSQL_SCRIPT

Related

Need to insert special character in string as insert script

I wanted to insert records having special character in snowflake.
Having record in source table :
order/date=2022-02-18/hour=12/85b3e2d8-0195-4238-b246-7ed6564ac464.json
I need to extract hour value i.e 12
I am able to extract the value using : cast(replace(substr(METADATA$FILENAME,28,2),'/','') as number)
But I need to create the insert script , I had tried :
'cast(replace(substr(METADATA$FILENAME,28,2),'/,'') as number)'
But getting error : FAILED CODE: 0 STATE: 22018 MESSAGE: Numeric value '5/' is not recognized
I tested your string in select and insert command as below:
select cast(replace(substr('order/date=2022-02-18/hour=12/85b3e2d8-0195-4238-b246-7ed6564ac464.json',28,2),'/','') as integer);
create table t1(c1 number);
insert into t1(c1) select cast(replace(substr('order/date=2022-02-18/hour=12/85b3e2d8-0195-4238-b246-7ed6564ac464.json',28,2),'/','') as integer);
If your issue is different, then share the exact command that you are executing and that's failing.
I got the solution :
Solution Snap shot
I wanted to insert this whole statement as string , I was facing issue due to special characters : / and '' .
Used backslash to resolve it.

How to insert regex with special characters like “&,?.#:;” in oracle database?

I have a regex value that I want to insert in oracle database table column but I have some problems that the value doesn't inserted correctly in the database
this is the value that I want to insert :
INSERT INTO valid_value VALUES (9, 14, 'REGEX[[A-Z.,-\s]+(,\s[A-Za-z.,-\s]+)?]', 1);
the result of this insert is :
valid_value
+--------------------------------------+
REGEX[[A-Z.,-\s]+(,\s[A-Za-z.,-\s]+)]
I lose the "?" character can some one help me on this how to insert regex value in a table column.
I start the script using a batch file
set DB_CREATE_ROOT="%~dp0"
set SQL_INIT_CONF_DIR=%DB_CREATE_ROOT%\Scripts\configuration\
for /r %SQL_INIT_CONF_DIR% %%F in (*init.sql) do (
ECHO %DATE% %TIME% "%%F" >> %LOG_NAME%
sqlplus -L Test_APP/welkom#//localhost:1521/xe #"%%F" >> %LOG_NAME% 2>&1
)
thanks in advance
You can use CHR function for each of the special characters if you find difficulties with direct inserts.
Thanks
For the records, none of the characters mentioned have a special meaning inside SQL strings thus they don't need any special treatment:
SQL> SELECT '&,?.#:;' AS are_we_special
2 FROM DUAL;
ARE_WE_
-------
&,?.#:;
(online demo)
... being & the only possible exception (“SET DEFINE OFF” in Oracle Database) and only in the context of SQL*Plus and SQL Developer—in which case you'd be getting a Enter value for xxxxx prompt.
Whatever problem the OP had, CHR() is just a workaround for his specific undisclosed issue.
This may help you
INSERT INTO valid_value VALUES (9, 14, 'REGEX[[A-Z.,-\s]+(,\s[A-Za-z.,-\s]+)'?']', 1);
Try this:
INSERT INTO valid_value VALUES (9, 14, 'REGEX[[A-Z.,-\s]+(,\s[A-Za-z.,-\s]+) ?]', 1);
See demo:
In SQLPLUS:
SQL> /
COL1
---------------------------------------------
REGEX[[A-Z.,-\s]+(,\s[A-Za-z.,-\s]+) ?]
Now question is which client you are using. In SQLPLUS, its working as shown above.

SQL and DB2 create command

I am going through a tutorial on how to use DB2 which is in a linux environment.
I am supposed to connect to a database, create a table, insert some data under db2 shell:
db2 connect to c3421m
db2
db2 => update command options using z ON Assignment_0.txt
db2 => update command options using v ON
db2 => CREATE TABLE BAND_2015 // gives error I got stuck here
// here is where I get stuck i am supposed to create a table and execute the follwing command under DB2 shell: CREATE TABLE BAND_2015
Code given:
create table band_2015 ( \
band_no integer not null primary key, \
band_name varchar(25) not null, \
band_home varchar(25) not null, \
band_type varchar(10) check (band_type in (‘concert’,’rock’,’jazz’,’military’)), \
b_start_date date not null, \
band_contact varchar(10) not null )
So how do I create this table? I was told to copy it to a text editor(do i save it as band_2015.sql ?). I am completely new to this but i have a lot of experience in other programming languages...
The problem is the terminating character. By default in the carriage return (enter). However, for your tutorial, you should type multi-line commands. For this case, you change the terminator character by defining another
For semi-colon
db2 -t
select *
from table;
For at sign or any other character.
db2 -td#
select *
from table #
For no character:
db2
select * from table
In the DB2 command-line processor by default commands and statements cannot span multiple lines, so it treats CREATE TABLE BAND_2015 as a complete statement, which is of course not the case. In the code given to you those backslashes appear for a reason -- they indicate to the CLP that the statement continues on the next line.
Alternatively, you can start the CLP with the command line option -t, which will designate the semicolon, instead of the new line, as the statement terminator. You can then type the statement as you did, without the backslashes, and terminate it with the ";".

Shell Script to connect from one postgres database to another database using dblink

ERROR: function dblink_connect_u(text, text) does not exist
LINE 3: from dblink_connect_u(cast(varchar 'dbname=test_db...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
Dear all,
I'm having error when execute my shell script to connect from one database to another database in different server using dblink. Above is showing the error i hit. By the way, i have checked my share directory in linux server, i have the function:
/usr/share/postgresql/8.4/contrib/dblink.sql
However, how come i still hitting the error above?
Below is my shell script code:
echo "Start insert records..."
psql cr032 <<THE_END
select t1.*
into test_table
from dblink_connect_u(cast(varchar 'dbname=TEST_DB port=5432 host=10.0.0.10 user=test password=123456' as text),
cast(varchar 'select applicationid, appname, appversion, apppath, appdatetime, description, systemtype from test_table' as text))
as t1(applicationid varchar(36), appname varchar(100), appversion varchar(20), apppath varchar(200), appdatetime timestamp, description text, systemtype smallint);
THE_END
echo "End insert records!"
Please kindly help and your assistance are highly appreciated!
I found the root cause of the error, this is because I didn't load dblink.sql into my postgres database after installed.
Below is the command to load dblink.sql into postgres, execute the command under postgres/bin folder :
psql -d "database_name" < "dblink_location"/dblink.sql
What happens when you omit the cast varchar...as text?
Also thinking along the lines of this below. You use dblink_connect_u to establish the connection, then dblink to execute the query.
Select dblink_connect_u( 'connection1',
'dbname=TEST_DB port=5432 host=10.0.0.10
user=test password=123456')
select t1.*
into test_table
from dblink('connection1',
'select applicationid, appname, appversion, apppath,
appdatetime, description, systemtype from test_table' )
as t1(applicationid varchar(36), appname varchar(100),
appversion varchar(20), apppath varchar(200),
appdatetime timestamp, description text, systemtype smallint);

mysql error 1064 when inserting

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(country,ping,order) VALUES (China,1,1)' at line 1
this is my code
INSERT INTO
(country, ping, order)
VALUES
('China', '1', '1');
You're missing the Table Name. Try:
INSERT INTO MYTABLENAME (country,ping,order) VALUES ('China','1','1');
are ping and order text fields or numeric? if numeric remove the ticks from the 1's
INSERT INTO Tablename (country,ping,order) VALUES ('China',1,1)
could also be reserved word try:
INSERT INTO Tablename (country,`ping`,`order`) VALUES ('China',1,1)
Your insert statement is missing the table name:
INSERT INTO tbl_name (col_name,...) VALUES (expr,...)
you are missing table name. also make sure that those quotes are necessary