Solved

Stored Procedure Help in SQL

Posted on 2014-01-14
3
123 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now