Solved

Stored Procedure Help in SQL

Posted on 2014-01-14
3
135 Views
Last Modified: 2014-02-20
I need help with a stored procedure that handle insert/update in a SQL table. SQL table on server A and converted data on server B (linked Server).

I have provided columns from both tables and attached a screen shot.



-- Stored procedure to insert/update a table from a linked server.

------- Table One columns in server A

Insert  tablename
            ID
            FormID,
            StateProvinceCode,
            StatusCodeID,
            EffectiveDate,
            ExpirationDate,
            ApprovedBy,
            ApprovalDateTime


---Table two columns coming from a linked server B

Select
      cc
      state
      efdt
      expdt
      crby
      crdt
      mtby
      mtdt
      exc
      cnrownumber
      cnxSyncDate ---  (optional column)
 
-----  Note
cc column should be inserted/update ---->  StatusCodeID
state column should be inserted/update  --->  StateProvinceCode
cnrownumber column should be  inserted/update  ---->  ID

FormID numbers to stay the same.
Screen-shot-of-the-result-sets-f.docx
0
Comment
Question by:Favorable
  • 2
3 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 39780917
You can use MERGE for this:
    MERGE tablename AS target
    USING (SELECT Select
      cc,
      state,
      cnrownumber FROM LinkedServer.DbName.TableName) AS source 
(StatusCodeID, StateProvinceCode, ID)
    ON (target.ID = source.ID)
    WHEN MATCHED THEN 
        UPDATE SET StatusCodeID = source.StatusCodeID, 
              StateProvinceCode = source.StateProvinceCode
	WHEN NOT MATCHED THEN	
	    INSERT (StatusCodeID, StateProvinceCode, ID)
	    VALUES (source.StatusCodeID, source.StateProvinceCode, source.ID)

Open in new window

It is not clear where FormID comes from, and what is default value when inserting new rows
0
 

Author Comment

by:Favorable
ID: 39780934
Formid is a default number already assign, so we will ignore that column.
0
 

Author Closing Comment

by:Favorable
ID: 39873581
Thank you so much
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question