Solved

Stored Procedure Help in SQL

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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

895 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

19 Experts available now in Live!

Get 1:1 Help Now