Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a requirement where i need to get data from view and insert it into another table but before inserting i have to check the data is existing or not, if not then only oi have to insert .

but the data in view -> one column data will b compared with other table and need to update that column with table data ID , its only for my internal purpose i dont want to update that in view.

i am using "Merge" method for insertion and updation
please check below query "V_CONTRACTS " is view

--Inserting Suppliername into Suppliers table
merge Suppliers as SC
using V_CONTRACTS as TCon On 
(TCon.SUPPLIER_NAME=SC.SupplierName)
When not matched Then
Insert (SupplierName,EntityID,isdeleted,createdon,lastupdatedon,createdby,lastupdatedby) 
Values (TCon.SUPPLIER_NAME,1,1,getdate(),getdate(),1,1);


--Updating Suppliersname with Suppliersid
MERGE V_CONTRACTS AS target
    USING Suppliers as S On
    (S.SupplierName=target.Supplier_Name)   
    WHEN MATCHED THEN 
     UPDATE SET Supplier_Name = S.ID;


now this updated id i hav to insert .

--Inserting View data into Suppliercontract table
merge TR_ICV_SupplierContract as t
using V_CONTRACTS as src On 
(src.contract_no=t.ContractNumber AND
 src.CONTRACT_DESCRIPTION=t.ContractDescription AND
-- src.CONTRACT_TYPE=t.ContarctType AND 
 src.COMMENCEMENT_DATE = t.PlannedStartDate AND
 src.EXPIRY_DATE = t.Replannedenddate AND
 Src.CONTRACT_HOLDER = t.contractHolder AND
 src.CONTRACT_OWNER = t.contractOwner AND
 src.CONTRACT_ENGINEER = t.contractEngineer AND
 src.TEL_NUMBER = t.phoneNo AND
 src.EMAIL_ADDRESS = t.vendorEmail AND
 src.Supplier_name = t.vendorName
 )
When not matched Then
Insert (ContractNumber,ContractDescription,--ContarctType,
PlannedStartDate,plannedenddate,ReplannedEnddate,contractHolder
,contractOwner,contractEngineer,phoneNo,vendorEmail,vendorName,EntityID,LastupdatedOn,Registrationstatus) 
Values (src.contract_no,
src.CONTRACT_DESCRIPTION,
--src.CONTRACT_TYPE,
src.COMMENCEMENT_DATE,
src.EXPIRY_DATE,
src.EXPIRY_DATE,
Src.CONTRACT_HOLDER,
src.CONTRACT_OWNER,
src.CONTRACT_ENGINEER,
src.TEL_NUMBER,src.EMAIL_ADDRESS,src.Supplier_name,1,Getdate(),(select ID from lookup where Lookup_type = 'Registration Status' and lookup_Value = 'Yet to be Registered')
);



only to insert SupplierID in this - i am updating id in view bt i dont want it to override .
Posted
Updated 5-Mar-14 20:32pm
v2

1 solution

View are nothing on their own. The data owner still is the table(s) that was used to create a view. They, table will be updated when data in View is updated. Tell us more on what you are trying and someone can help.
 
Share this answer
 
Comments
kamalsekhar 6-Mar-14 2:32am    
I have updated my question please check

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