insert where is not in

hi you experts
how can i achive this query

i mean, insert the element when is not already in


insert into materiales(tag,unidad,descripcion,proveedor,tipmater)
values ( 'HV9900', 'PZA', 'VALVULAS DE CONTROL ON-OFF', 'ADOBE SYSTEMS INCORPORATED', 'INSTRUMENTACION')
where materiales.tag <>'HV9900')

tsm
ErnestoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dsackerContract ERP Admin/ConsultantCommented:
Try this:

if not exists (select 1 from materiales where tag = 'HV9900')
    insert into materiales (tag, unidad, descripcion, proveedor, tipmater)
    values ( 'HV9900', 'PZA', 'VALVULAS DE CONTROL ON-OFF', 'ADOBE SYSTEMS INCORPORATED', 'INSTRUMENTACION')

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
winheimCommented:
One way to do this would be this:

CREATE Table materiales
(
 tag nvarchar(100) , /* unique?*/
 unidad nvarchar(100),
 descripcion nvarchar(255),
 proveedor nvarchar(255),
 tipmater nvarchar(255),
)

/*
* Add anything
*/
INSERT INTO materiales(tag,unidad,descripcion,proveedor,tipmater)
values ( 'HV9900', 'PZA', 'VALVULAS DE CONTROL ON-OFF', 'ADOBE SYSTEMS INCORPORATED', 'INSTRUMENTACION')


/*
* You can use Left Join and a sub query
*/

-- THIS query does not add a new row
insert into materiales(tag,unidad,descripcion,proveedor,tipmater)

SELECT * FROM
(
      SELECT 'HV9900' AS tag, 'PZA' AS unidad, 'VALVULAS DE CONTROL ON-OFF' AS descripcion, 'ADOBE SYSTEMS INCORPORATED' AS proveedor, 'INSTRUMENTACION' AS tipmater      
) to_insert
LEFT JOIN materiales m
ON to_insert.tag = m.tag
WHERE m.tag IS NULL /* if returns NULL then you know that the line you'd like to add is not already present in the table materiales */



-- THIS query adds a new row
insert into materiales(tag,unidad,descripcion,proveedor,tipmater)

SELECT * FROM
(
      SELECT 'HV9901' AS tag, 'PZA' AS unidad, 'VALVULAS DE CONTROL ON-OFF' AS descripcion, 'ADOBE SYSTEMS INCORPORATED' AS proveedor, 'INSTRUMENTACION' AS tipmater      
) to_insert
LEFT JOIN materiales m
ON to_insert.tag = m.tag
WHERE m.tag IS NULL /* if returns NULL then you know that the line you'd like to add is not already present in the table materiales */
0
sameer2010Commented:
You can also use MERGE. This makes code easy to understand
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.