Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HI,
i want to compare a value for two columns in the database.

like i have a value called vehicleid. before insert into dataabse i want to compare that with two columns vehicleidin / vehicleidout. if that value is not present with any of the row for these two columns than and only it will allow to save this to database.


for one column i know we can write something like

SQL
if vehicleid not in (vehicleidin) than 
begin
insert into ...
end 


but dont know what to with two columns .
Posted

1 solution

Hi Try it out....

SQL
Insert into orders (orders_id, customers_id, customers_cid, customers_vat_id, customers_name, customers_email_address)
select
    o.*
from
    Test.dbo.orders o
where
    not exists (
        select 1
        from
            CobraDemoData.dbo.Data a
        where
            a.email0 = o.customers_email_address
    )



Also, you probably want to specify the columns in the select statement, just to make sure the right columns are transposed.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900