insert multiple values from sub query to column of table - sql

I want to insert value from my sub query
my query is like that..
insert into T_Scanned(F_Asset_Code) values (select F_Barcode from T_Assets where F_Barcode
in( select Barcode as barcoade from [T_NEWASSET]))
but this is not working...

You don't need to use VALUES when doing an INSERT INTO ... SELECT:
insert into T_Scanned(F_Asset_Code)
select F_Barcode from T_Assets where F_Barcode
in( select Barcode as barcoade from [T_NEWASSET])

Related

The select list for the INSERT statement contains fewer items than the insert list (but is identical)

I am trying to develop a procedure that has this basic structure:
select a.*
into #temp1
from OPENQUERY(otherDB,'SELECT ... FROM ...')a
INSERT INTO [dbo].[Data]
(....)
select *
from #temp1
DROP TABLE #temp1
The amount of columns in the results from the OPENQUERY is identical to the INSERT columns
How could I be catching this error :
The select list for the INSERT statement contains fewer items than the insert list. The number of SELECT values must match the number of INSERT columns.
What if you try to make more specific the select? Example:
insert into dbo.data (col1,col2) select col1,col2.....

Inserting data into Oracle table (SQL)

I already have a table built in oracle.
Im trying to insert some data like this:
INSERT INTO movies_actor('name','id')
VALUES ('Nuno','2'), ('Pedro','3'), ('Jose','1');
select * from movies_actor;
I always get this error
ORA-00928: missing SELECT keyword
What am I doing wrong?
I don't think you need the single quote around your field names.
You need to do:
INSERT INTO TableName(Column1, Column2)
VALUES('Nuno', '2');
In your example, it would be:
INSERT INTO movies_actor(name, id)
VALUES ('Nuno','2');
INSERT INTO movies_actor(name, id)
VALUES ('Pedro','3');
INSERT INTO movies_actor(name, id)
VALUES ('Jose','1');
select * from movies_actor;
Another way.
insert into table
(field1, field2)
select value1, value2
from dual
union
select value3, value4
from dual
etc
You cannot insert multiple records in one statement using VALUES. You can either use Tenzin's solution or use INSERT ALL :
INSERT ALL
INTO movies_actor(name, id) VALUES ('Nuno', '2')
INTO movies_actor(name, id) VALUES ('Pedro', '3')
INTO movies_actor(name, id) VALUES ('Jose', '1')
SELECT * FROM dual;

Sql insert multiple rows if not exists

I have a sql table that has two columns id and name. I have list of names about 20 and I need to write a query that checks if name exists before insert.
Is there a better way of doing this rather then just having the below query 20 times but with different names (I need do this in t-sql):
IF NOT EXISTS(SELECT*
FROM mytable
WHERE name = 'Dan')
BEGIN
INSERT INTO mytable
(name)
VALUES ('dan')
END
INSERT INTO MyTable (Name)
SELECT NewNames.Name
FROM ( VALUES ('Name1'), ('Name2'), ('Name3') ) AS NewNames (Name)
WHERE NOT EXISTS ( SELECT 1
FROM MyTable AS MT
WHERE MT.Name = NewNames.Name );
I think you could use a merge statement:
MERGE INTO myTable AS Target
USING (VALUES ('name1'),('name2'),('...')) AS source (NAME)
ON Target.NAME = Source.NAME
WHEN NOT MATCHED BY TARGET THEN
INSERT (NAME) VALUES (name)
You can filter values with NOT EXISTS
INSERT INTO myTable (
Name
)
SELECT DISTINCT
Name
FROM (
VALUES ('Name 1'),
('Name 2')
) AS NewNames(Name)
WHERE
NOT EXISTS (SELECT 1 FROM TargetTable WHERE myTable.Name = NewNames.Name)
If your new names are in another table, you can change the select query in the above one.
Please note, that the DISTINCT keyword is necessary to filter out the duplications in the source data.
I would do this using insert:
with names as (
select 'Dan' as name union all
select 'name2' union all
. . .
)
insert into myTable(name)
select distinct name
from myTable
where not exists (select 1 from mytable t2 where t2.name = t.name);
Note: you may want to create a unique index on mytable(name) so the database does the checking for duplicates.
untested so there might be some minor errors:
merge into mytable x
using (
values ('name1')
, ('name2')
, ...
, ('namen')
) as y (name)
on x.name = y.name
when not matched then
insert (name)
values (y.name)
INSERT INTO MyTable (Name)
SELECT Name FROM
(
VALUES ('Name 1'),
('Name 2')
) AS Names(Name)
WHERE Name NOT IN
(
SELECT Name FROM MyTable
)
INSERT IGNORE INTO myTable (column1, column2) VALUES (val1, val2),(val3,val4),(val5,val6);
INSERT IGNORE will allow skip on duplicate values

SQL Server return wrong result when I use LIKE

I created a new DB "TEST", add a new table "tblTest" and 1 column "Name" in it.
Insert some records: Minh, Tinh, Justin
create database TEST
go
use test
go
create table tblTest (Name varchar(50))
go
insert tblTest values ('Minh')
insert tblTest values ('Tinh')
insert tblTest values ('Justin')
Then I run this query
Select * from tblTest where Name like '%in%'
The result is: Justin. (Minh and Tinh are not display)
Anyone can tell me how to fix this?
Check this example, its give me your result as you want. First % is used for preceed and Last % used for succeeded.
declare #t table (name varchar(50))
insert into #t values('Minh'),('Tinh'),('Justin')
Select * from #t where Name like '%in%'
Result will be
name
Minh
Tinh
Justin
See if this query is working and providing the necessary output.
with tblTest as
(
Select 'Minh' name from dual union
Select 'Tinh' name from dual union
Select 'Justin' name from dual
)
Select * from tblTest where Name like '%in%'
Else, delete all records from tblTest using:
DELETE from tblTest;
Then, insert records using:
INSERT INTO TBLTEST (NAME) VALUES ('Minh');
INSERT INTO TBLTEST (NAME) VALUES ('Tinh');
INSERT INTO TBLTEST (NAME) VALUES ('Justin');
Now try executing your SELECT query again.
As #podiluska pointed out, there could be some foreign junk characters.

ms sql insert into value from variable and select from table

I want to insert into a table an value from variable and an select result.
declare #actual Date
set #actual = getdate()
This is my try:
insert into test (ID, Date) (select ID, #actual from Table)
I get an error Message!!
use INSERT INTO..SELECT statement,
DECLARE #actual Date
SET #actual = GETDATE()
INSERT INTO test (ID, Date)
SELECT ID, #actual
FROM Tablename
You can try like this for using a select clause for Insert
insert into test(id,date) select col1,col2 from table;
INSERT INTO TEST
(ID,DATE)
SELECT COLUMN1,COLUMN2 FROM TABLE