SQL INSERT into MySQL table where column name includes an apostrophe - mysql

Does anyone know how I can perform an SQL INSERT operation into a MySQL table where column names include apostrophes, for example:
INSERT INTO MYTABLE (`id`, `Column'1`, `Column'2`) VALUES...
I have tried things like this but to no avail:
INSERT INTO MYTABLE (`id`, `Column''1`, `Column''2`) VALUES...
INSERT INTO MYTABLE (`id`, `Column\'1`, `Column\'2`) VALUES...

You should be able to just place backticks around the column name:
INSERT INTO MYTABLE (`id`, `Column'1`, `Column'2`) VALUES...
see SQL Fiddle with Demo
create table yourtable
(
id int,
col1 varchar(10),
`col'2` varchar(10)
);
insert into yourtable (id, col1, `col'2`) values
(1, 'test', 'sdfsd'),
(1, 'test', 'gtet')

Just put the name into a variable and use the character 39 for the apostrophe.
Example :
L'oiseau becomes :
Loiseau = 'L' + chr(39) + 'oiseau'
print(Loiseau)
=> L'oiseau

Related

Is it possible to insert in mysql a combo of words and variable?

Trigger definition:
BEGIN
INSERT INTO `user` (`ID`, `Username`, `Password`, `Online`) VALUES
(NEW.ID, "store_"NEW.ID , 'blablabla', 0);
END
In this trigger I have to insert as username something like store_(id of the new store).
Is it possible to do this and if yes how?
There are 2 ways to concat/append string in SQL
a. Use + operator to join the string
b. Use Concat function.
By using + operator the query would be like below.
BEGIN
INSERT INTO `user` (`ID`, `Username`, `Password`, `Online`) VALUES
(NEW.ID, "store_" + NEW.ID , 'blablabla', 0);
END
By using Concat() function the query would be like below.
BEGIN
INSERT INTO `user` (`ID`, `Username`, `Password`, `Online`) VALUES
(NEW.ID, Concat("store_",NEW.ID) , 'blablabla', 0);
END
Recommended option is use 'Concat' function as syntax is easier to follow, and the code looks cleaner
Use concat function :
BEGIN
INSERT INTO user (ID, Username, Password, Online)
VALUES
(NEW.ID, CONCAT('store_',NEW.ID), 'blablabla', 0);
END

Insert MySQL Query with use of concat

Im trying to insert a query to my database using a name in a variable and also using a name in the values. So i want to make it in to 1 name.
Here is an example
SET #name = "This is my";
INSERT INTO mytable (id, name)
VALUES (1, #name + "Test Query");
So, This query should then insert a row with id 1 and name "This is my Test Query" But it gives me an error. Truncated incorrect DOUBLE value: 'Test Query'
You can't use #name + "Test Query" syntax instead you shoud use concat() mysql function see here for more info:http://www.w3resource.com/mysql/string-functions/mysql-concat-function.php
SET #name = "This is my";
INSERT INTO `names` (id, name)
VALUES (1, concat(#name, 'Test Query'));
also make sure that your id is not autoincreament if it is then no need to pass id number in your query like this
INSERT INTO `names` (name)
VALUES (concat(#name, 'Test Query'));
i found no error in sql.. it executed without error..
DECLARE #name as varchar(100)
DECLARE #mytable as table ( id int, [name] varchar(100))
SET #name = 'This is my';
INSERT INTO #mytable (id, name)
VALUES (1, #name + 'Test Query');
Select * from #mytable

MySQL Trigger Error using variables

I'm trying to get the following code to run in a MySQL Trigger but I get error 1064 when I try to save it.
SET #ma = (SELECT modem_alias FROM `play`.`veh` WHERE meid = new.org_a LIMIT 1);
INSERT INTO `play`.`des` (`indx`, `des_a`, `des_b`) VALUES (NULL, new.org_a, SELECT #ma);
The trigger is set to run on 'org' table after an INSERT
Don't use SELECT in the INSERT, just the variable:
INSERT INTO `play`.`des` (`indx`, `des_a`, `des_b`) VALUES (NULL, new.org_a, #ma);
You can also combine the two queries so you don't need a variable:
INSERT INTO play.des (indx, des_a, des_b)
SELECT NULL, new.org_a, modem_alias
FROM play.veh
WHERE meid = new.org_a
LIMIT 1
Your values is wrong: You can't select there, just use the variable itself:
INSERT ... VALUES (..., #ma);

Use sql result to specify table to join

Is there any way how can I use result for specifying table to join?
I'd like to do something like
SELECT id, some_number, ... FROM sometable NATURAL JOIN someothertable_$some_number;
I know that there's nothing like this in relational algebra, so probably I'll not succeed, I just wanted to ask to be sure.
I don't want to use any SQL scripts.
Runnable Example Here: http://sqlfiddle.com/#!2/5e92c/36
Code to setup tables for this example:
create table if not exists someTable
(
someTableId bigint not null auto_increment
, tableId int not null
, someOtherTableId bigint not null
, primary key (someTableId)
, index (tableId, someOtherTableId)
);
create table if not exists someOtherTable_$1
(
someOtherTableId bigint not null auto_increment
, data varchar(128) character set utf8
, primary key (someOtherTableId)
);
create table if not exists someOtherTable_$2
(
someOtherTableId bigint not null auto_increment
, data varchar(128) character set utf8
, primary key (someOtherTableId)
);
insert sometable (tableId, someOtherTableId) values (1, 1);
insert sometable (tableId, someOtherTableId) values (1, 2);
insert sometable (tableId, someOtherTableId) values (2, 2);
insert sometable (tableId, someOtherTableId) values (2, 3);
insert someothertable_$1(data) values ('table 1 row 1');
insert someothertable_$1(data) values ('table 1 row 2');
insert someothertable_$1(data) values ('table 1 row 3');
insert someothertable_$2(data) values ('table 1 row 1');
insert someothertable_$2(data) values ('table 1 row 2');
insert someothertable_$2(data) values ('table 1 row 3');
STATIC SOLUTION
Here's a solution if your tables are fixed (e.g. in the example you only have someOtherTable 1 and 2 / you don't need the code to change automatically as new tables are added):
select st.someTableId
, coalesce(sot1.data, sot2.data)
from someTable st
left outer join someOtherTable_$1 sot1
on st.tableId = 1
and st.someOtherTableId = sot1.someOtherTableId
left outer join someOtherTable_$2 sot2
on st.tableId = 2
and st.someOtherTableId = sot2.someOtherTableId;
DYNAMIC SOLUTION
If the number of tables may change at runtime you'd need to write dynamic SQL. Beware: with every successive table you're going to take a performance hit. I wouldn't recommend this for a production system; but it's a fun challenge. If you can describe your tool set & what you're hoping to achieve we may be able to give you a few pointers on a more suitable way forward.
select group_concat(distinct ' sot' , cast(tableId as char) , '.data ')
into #coalesceCols
from someTable;
select group_concat(distinct ' left outer join someOtherTable_$', cast(tableId as char), ' sot', cast(tableId as char), ' on st.tableId = ', cast(tableId as char), ' and st.someOtherTableId = sot', cast(tableId as char), '.someOtherTableId ' separator '')
into #tableJoins
from someTable;
set #sql = concat('select someTableId, coalesce(', #coalesceCols ,') from someTable st', #tableJoins);
prepare stmt from #sql;
execute stmt;

Incorrect insert syntaxis SQL Server

I have created a Temporal Table:
create table #T_Table(id int,name nvarchar(80),value float);
I am executing dynamic SQL to insert to #T_Table the select result from #table_name like this:
SET #template = 'INSERT #T_Table (id,name,value) VALUES('+Cast(#counterI as nvarchar)+', '+ #fieldFromTableName +', Select [value] = SUM('+#fieldFromTableName+') FROM '+ #table_name +')';
EXEC (#template);
So if I print#template:
INSERT #T_Table(id,name,value) VALUES(1, x1, Select [value] = SUM(x1) FROM Mytable);
What is it wrong?
HereĀ“s the INSERT you should build:
INSERT #T_Table(id,name,value)
SELECT 1, 'x1', SUM(x1) FROM Mytable
You can't nest a SELECT inside a VALUES set of values.
Change VALUES to SELECT and get rid of the brackets. You also need to make sure x1 is wrapped in quotes, e.g.:
INSERT #T_Table(id,name,value)
SELECT 1, 'x1', Select [value] = SUM(x1) FROM Mytable;
To get quotes around x1 you will have to put double quotes in your quoted string to escape them: e.g. SET #quotedString = 'not quoted, ''quoted,'' not quoted'