Please help me to insert simple sql query into table for example:
insert into table0 ( sql_text) values ( 'select count(*) from table1;');
I am receiving error .
Thanks,
Please try the following:
INSERT INTO tableName (columnName) values ('Select count(*) from tableName; ');
Related
I have a Select Statement that fetches more than 1 data
set #name := select name from users;
Now I want to insert all of them with one Query
for example
insert into users2 (name , created) Values (#name , NOW())
it returns this error
Error Code: 1242
Subquery returns more than 1 row
is there any way to do this without a loop?
The syntax must be like this
insert into users2 (name , created)
select name, NOW() from users
Try this
insert into users2 (name , created)
select name, NOW() from users;
I am trying to insert theses values to my table student but I have an error
insert into student(first_name,last_name,student_number,professor_id)
values(Eden,Yuan,323744573,
select professor_id from PROFESSORS where professor_name = 'Chu ')
I get an error
saying missing expression
you can use this way (assuming that professor_id is the column you need)
insert into student(first_name,last_name,student_number,professor_id)
select 'Eden', 'Eden', 323744573, column_professor_id
from PROFESSORS where professor_name = 'Chu ' ;
(In your query is missing the column in the select )
I'm trying to insert to my table an average of my select query but I am encountering an error
Here is my query:
INSERT INTO tbl_average(student_id, first_avg) VALUES
('100', AVG(SELECT fir_grad FROM tbl_grade
WHERE student_id='100' AND school_year='2015-2016'))
pls help
use INSERT INTO SELECT syntax:
INSERT INTO tbl_average(student_id, first_avg)
SELECT 100, AVG(fir_grad)
FROM tbl_grade
WHERE student_id=100 AND school_year='2015-2016'
I have a SQL syntax error with my IF NOT EXISTS on line 1 when I tried to do this request on MySQL, and I can't figure why.
IF NOT EXISTS (SELECT * FROM `my_table` WHERE first_name = 'Testfn')
BEGIN
INSERT INTO `my_table` (first_name)
VALUES ('Testfn')
END;
I'm trying to insert first_name only if there is no other same first name in my_table.
I also tried this syntax, but I still have the error 1064 "You have an error in your SQL syntax" :
IF NOT EXISTS (SELECT * FROM `my_table` WHERE first_name = 'Testfn') THEN
INSERT INTO `my_table` (first_name)
VALUES ('Testfn')
END IF;
I tried SELECT * FROMmy_tableWHERE first_name = 'Testfn' separately, and it works.
And like this doesn't work too :
INSERT INTO `my_table` (first_name)
VALUES ('Testfn')
WHERE NOT EXISTS (SELECT * FROM `my_table` WHERE first_name = 'Testfn');
EDIT : first_name is UNIQUE in the database.
You have not need to write a column name, without specifying you can try to insert because we already checked condition on above. So basically you can do it using Merge Statement like.
MERGE INTO my_table
USING (
SELECT first_name
) t
ON t.first_name = my_table.first_name
WHEN NOT MATCHED THEN
INSERT (first_name) VALUES (t.first_name);
Hope this help you!
select cons_id,teh_id,local_id,panchayt_id,war_id,ha_id from b...;
select rep_value_id from val;`
i need to get the above values into single column in another table.
how can i solve it by a query or using stored procedure.
You can do something like this in oracle sql .
Insert into VAL_TABLE (ID,COMMON_FIELD) values (VAL_TABLE_ID.nextval, ( SELECT CONS_ID || TECH_ID || LOCAL_ID from TABLE_B));
Or in MySQL
Insert into VAL_TABLE (ID,COMMON_FIELD) values (1, ( SELECT concat( id, type, details) from TABLE_B ) );
Try this...
UPDATE my_table SET col1=
(SELECT CONCAT_WS(',',val.rep_value_id,cons_id,teh_id,local_id,panchayt_id,war_id,ha_id)
FROM b,val);