1. ผมสร้าง Query ไว้ตัวนึง โดยการใช้ Link Server กับ OracleCode (SQL)
USE [test]
GO
/****** Object: StoredProcedure [dbo].[P_GET_BACKUP_MASTER] Script Date: 08/14/2013 16:55:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[P_GET_BACKUP_MASTER] --@SHIFT VARCHAR(1),@DATE NVARCHAR(10)
AS
BEGIN
/*################################################################### INSERT PROCESS ###################################################################*/
INSERT INTO PLC_PROCESS_MAC
SELECT P.RESOURCE_CODE,P.ITEM,P.ALTERNATE,P.OPERATION_CODE,v.PROCESS_NAME,P.CT
FROM (select * from openquery(ap_prod2,
'SELECT B.SEGMENT1 ITEM,RT.alternate_routing_designator ALTERNATE,SE.STANDARD_OPERATION_CODE OPERATION_CODE,
RE.RESOURCE_CODE,USAGE_RATE_OR_AMOUNT HOUR_PER_QTY,ROUND(USAGE_RATE_OR_AMOUNT*3600,0) CT
FROM BOM_OPERATIONAL_ROUTINGS_V RT
INNER JOIN BOM_OPERATION_SEQUENCES_V SE ON(RT.routing_sequence_id =SE.routing_sequence_id)
INNER JOIN BOM_OPERATION_RESOURCES_V RE ON(SE.OPERATION_SEQUENCE_ID=RE.OPERATION_SEQUENCE_ID)
INNER JOIN mtl_system_items_b B ON(RT.assembly_item_id = B.inventory_item_id)
WHERE(SE.DISABLE_DATE Is Null And B.organization_id = ''104'') ORDER BY RT.LAST_UPDATE_DATE DESC') ) P INNER JOIN V_PROCESS V ON P.OPERATION_CODE = v.PROCESS_CODE
WHERE P.RESOURCE_CODE+P.ITEM+CONVERT(NVARCHAR ,P.ALTERNATE)+P.OPERATION_CODE NOT IN (SELECT RESOURCE_CODE+ITEM+CONVERT(NVARCHAR ,ALTERNATE)+OPERATION_CODE FROM PLC_PROCESS_MAC )
/*################################################################### INSERT JOB ###################################################################*/
INSERT INTO PLC_JOB_DESC
SELECT J.WIP_ENTITY_NAME AS JOB_NAME, J.SEGMENT1, J.DESCRIPTION, J.ATL, J.START_QUANTITY AS SALES_ORDER
FROM OPENQUERY(ap_prod2,
'SELECT T.*,wd.START_QUANTITY
FROM (select job_name from AAP_MASTER_WIP_JOB_V where (job_name like ''AP-%'') and (job_name <>''AP-'') and (job_date_close is null) ) V
LEFT JOIN (select WIP_ENTITY_NAME,SEGMENT1,DESCRIPTION,alternate_routing_designator atl from aap_rep_master_wip_job_test_v order by WIP_ENTITY_NAME) T
ON V.job_name = T.WIP_ENTITY_NAME
LEFT JOIN wip_discrete_jobs_v wd ON V.job_name= wd .WIP_ENTITY_NAME
ORDER BY V.job_name ') J
WHERE J.WIP_ENTITY_NAME NOT IN (SELECT JOB_NAME FROM PLC_JOB_DESC)
/*################################################################### INSERT ITEM ###################################################################*/
INSERT INTO PLC_ORACLE_ITEM_MASTER
SELECT *
FROM OPENQUERY(ap_prod2,'SELECT INVENTORY_ITEM_ID,SEGMENT1,DESCRIPTION FROM MTL_SYSTEM_ITEMS_B WHERE ORGANIZATION_ID = 104 and segment1 like ''5-%''')
WHERE INVENTORY_ITEM_ID NOT IN (SELECT INV_ID FROM PLC_ORACLE_ITEM_MASTER)
END_WORK:
END