Insert MySQL Query with use of concat - mysql

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

Related

How to insert default value if value is Null in insert method of stored procedure in SQL

My thought
I want to insert default value if value supplied is Null in Insert method of Stored procedure.Lets say i want to insert default value for the column Lastname if value supplied is null
My SP
Alter PROCEDURE [dbo].[Any]
#ID int=0,
#Name = varchar(max),
#Lastname = varchar(max),
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRAN
BEGIN TRY
BEGIN
INSERT INTO dbo.ABC
( ID,
Name,
Address)
Values
(
#ID,
#Name,
#Lastname)
code that i tried
I tried using this code shows there is error in case statement
INSERT INTO dbo.ABC
( ID,
Name,
case when Address = NULL then 'N/A' else
Address)
You could phrase your insert as an INSERT INTO ... SELECT:
INSERT INTO dbo.ABC (ID, Name, Address)
SELECT ID, Name, COALESCE(Address, 'N/A');
Or, you could also give the Address column a default value of N/A in your table definition.

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);

How to insert more variable values into single column in a table?

v_fname='Hari';
v_lastname='Anil'
create table name_tab(fullname varchar(200));
how to insert v_fname and v_lastname into fullname column.
i tried like below not work out.
insert into name_tab(fullname) values('first name is :'||v_fname||'Last name is '||v_lastname);
Please help on this
Thanks in advance.
try this:
insert into name_tab(fullname)
select ws_concat("||","first name is:",v_fname, "Last name is:", v_lastname)
from <other-table>;
The <other-table> is the table where your v_fname and v_lastname are stored.
Try this:
insert into name_tab(fullname)
values( concat( 'First Name is : ', v_fname, ', Last Name is: ', v_lastname );
You have to use variable identifier like $v_fname and $v_lastname (this is PHP style) in the statement.
I suggest you to redefine the table structure to accommodate first_name and last_names as separate columns. So that you can store values from variables directly into respective columns.
Example:
v_fname='Hari';
v_lastname='Anil'
create table name_tab( first_name varchar(200), last_name varchar(200) );
Now you can prepare your insert statement as below:
insert into name_tab( first_name, last_name )
values( ?, ? );
Use prepared statement from you scripting language to set values for each of the parameters reading from variables.
simple way is
$v_fname='Hari';
$v_lastname='Anil';
$v_fullname= 'first name is :' + $v_fname + ' Last name is ' + $v_lastname;
insert into name_tab(fullname) values('$v_fullname');
otherwise use update query like
update name_tab SET fullname=CONCAT('first name is :', '$v_fname', ' Last name is ', '$v_lastname');
i think it will also add a record...

SQL INSERT into MySQL table where column name includes an apostrophe

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

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'