How to add more values into a declared variable? - mysql

I wanted to insert a values into a declared variable.
This is how it is so far :
INSERT INTO DP_Transaction(Trans_Reference, Trans_Action, Trans_Name,
Trans_Date, Trans_Comment)
VALUES(#formId, 'Cancelled by', 'Workflow Manager', '2021-03-29 09:52:28.257', 'test')
And the values I want to add inside #formid is something like below:
'12323985',
'39864345',
'89426596',
'97070302',
'56746838'
And my expected result is all the values inside the statement will be insert into these #formId rows:
'12323985',
'39864345',
'89426596',
'97070302',
'56746838'
Can anyone help?

You could place the form ID values into a subquery and then rephrase the insert as an INSERT INTO ... SELECT:
INSERT INTO DP_Transaction (Trans_Reference, Trans_Action,
Trans_Name, Trans_Date, Trans_Comment)
SELECT
formid,
'Cancelled by',
'Workflow Manager',
'2021-03-29 09:52:28.257',
'test'
FROM
(
SELECT '12323985' AS formid UNION ALL
SELECT '39864345' UNION ALL
SELECT '89426596' UNION ALL
SELECT '97070302' UNION ALL
SELECT '56746838'
) t;
If you were using a different database, e.g. SQL Server, then you could have used APPLY along with a string splitting function. But this is not an option in MySQL to my knowledge.

Related

Mysql: How to get custom resultset as select statement output

I have below Mysql query -
Select ('GOOGLE.COM',
'MSN.COM',
'YAHOO.COM',
'YOUTUBE.COM',
'BING.COM',
'FACEBOOK.COM',
'LIVE.COM',
'MICROSOFT.COM',
'WIKIPEDIA.ORG',
'LINKEDIN.COM') as domain from dual;
I'm expecting the below resultset.
'GOOGLE.COM'
'MSN.COM'
'YAHOO.COM'
'YOUTUBE.COM'
'BING.COM'
'FACEBOOK.COM'
'LIVE.COM'
'MICROSOFT.COM'
'WIKIPEDIA.ORG'
'LINKEDIN.COM'
But I'm getting error Operand should contain 1 column(s).
How can I fix my query so I can get correct results?
If you want one record per value, you can use union all:
select 'GOOGLE.COM' domain
union all select 'MSN.COM'
union all select 'YAHOO.COM'
union all select 'YOUTUBE.COM'
union all select 'BING.COM'
union all select 'FACEBOOK.COM'
union all select 'LIVE.COM'
union all select 'MICROSOFT.COM'
union all select 'WIKIPEDIA.ORG'
union all select 'LINKEDIN.COM'
In other RDMBS, such as Postgres or SQL Server, this would have been as simple as:
SELECT domain
FROM ( VALUES
('GOOGLE.COM'),
('MSN.COM'),
('YAHOO.COM'),
('YOUTUBE.COM'),
('BING.COM'),
('FACEBOOK.COM'),
('LIVE.COM'),
('MICROSOFT.COM'),
('WIKIPEDIA.ORG'),
('LINKEDIN.COM')
) AS t(domain);
But MySQL does not support this syntax. A workaround is to to create a table, that you can then link in your queries:
CREATE TABLE tmp (domain VARCHAR(50));
INSERT INTO tmp(domain) VALUES
('GOOGLE.COM'),
('MSN.COM'),
('YAHOO.COM'),
('YOUTUBE.COM'),
('BING.COM'),
('FACEBOOK.COM'),
('LIVE.COM'),
('MICROSOFT.COM'),
('WIKIPEDIA.ORG'),
('LINKEDIN.COM')
;

sqlite query from list of value

This code is working within MS SQL Server
SELECT DISTINCT *
FROM (VALUES ('1234'), ('5678'), ('8888'))
AS Table_Staff_No(Staff_No)
WHERE Staff_No
NOT IN (
SELECT Staff_No
FROM Table_Staff_No
WHERE (Staff_No IN ('1234', '5678'. '8888'))
How should I go about it in SQLite?
My table will be have value 1234, so I am passing my list (1234,5678,8888) to check which value is not exist in my table.
The result should be show 5678, 8888.
Translating this query to SQLite requires a common table expression, which is available since SQLite 3.8.3:
WITH Table_Staff_No(Staff_No) AS (
VALUES ('1234'), ('5678'), ('8888')
)
SELECT DISTINCT *
FROM Table_Staff_No
WHERE Staff_No NOT IN (
SELECT Staff_No
FROM Table_Staff_No
WHERE Staff_No IN ('1234', '5678', '8888'));

MySQL insert into table with a set of values

I need to insert a table with some values (eg: 'NDA' in this case). This seems to work well if I have just one value to be inserted. I have around a dozen of similar values, is there a was i can tweak this query to insert say { 'NDA', 'SDM', 'APM' } values. Was curious to know if it can be done without a stored procedure or copy pasting the same statements over and changing the values.
INSERT IGNORE INTO customer_feature (customer_id, feature)
SELECT c.id, 'NDA' FROM
customer as c
where c.edition = 'FREE_TRIAL';
Reference: mysql -> insert into tbl (select from another table) and some default values
Is this what you want?
INSERT IGNORE INTO customer_feature(customer_id, feature)
select c.id, f.feature
from customer c cross join
(select 'NDA' as feature union all select 'SDM' union all select 'APM'
) f
where c.edition = 'FREE_TRIAL';

return multiple column from sub query and insert into new table

I have a query for insert into table using sub query prob is that in sub query there
is 2 columns and with where condition and group by clause.
sub query is running well can any one help me plz
Query : Account_name is type text
insert into trial_bal (Account_name,Debit) values (
select convert(text,convert(varchar(max),Accounts)),SUM(ISNULL( Debit,0))-SUM( ISNULL(Credit,0))
from general
where Acount_Type='Assets'
group by convert(varchar(max),Accounts)
);
Instead of using INSERT INTO ..VALUES you should use INSERT INTO .. SELECT..FROM to return the values from the select statement:
insert into trial_bal (Account_name,Debit)
select convert(text,convert(varchar(max),Accounts)),
SUM(ISNULL( Debit,0))-SUM( ISNULL(Credit,0))
from general
where Acount_Type='Assets'
group by convert(varchar(max),Accounts)

INSERT SELECT with condition

I'm trying to do a INSERT SELECT with condition but smthg seems to be wrong. I get an 1064 error for wrong syntax.
Here is the query :
INSERT INTO `db1`.`table`.`field` (
SELECT a.`field1` , a.`field2`
FROM `db2`.`table1` a, `db2`.`table2` b
WHERE a.`field1` = b.`field1`
AND b.`field2` = 'value'
)
WHERE a.`field1` = `db1`.`table1`.`field1`
Thank in advance for any suggestions
Syntax for the insert select is :
INSERT california_authors (au_id, au_lname, au_fname)
SELECT au_id, au_lname, au_fname
FROM authors
WHERE State = 'CA'
so in you case it like
INSERT INTO `db1`.`table` (.... field list ...)
select
... col for select .....
from table
where ... where condition ....
The syntax for INSERT INTO is usually
INSERT INTO myTable (field1,field2,...,fieldN)
SELECT field1,field2,etc FROM myTable WHERE condition
You need to simplify your queries and think through what you're trying to do.
My question was bad. In this case it is not Insert but an Update query which is needed