这是MERGE命令的合适人选吗?
源数据必须是另一个表还是可以传递的变量?
如果它必须是一个表,是否将传递的变量插入到临时表中?
你能帮我解释一下语法吗?
CREATE PROCEDURE [dbo].[usp_ConvertToMerge]
@GL_DT date
,@SRC_SYS_ID varchar(60)
,@MLR_SRC_SYS_CD char(3)
,@TRSRY_FEED_DT date
,@Data varchar(20)
AS
BEGIN
IF NOT EXISTS (SELECT
@GL_DT
FROM
MLR_REBATE_IBOR_INFO_2
WHERE
[GL_DT] = @GL_DT
AND [SRC_SYS_ID] = @SRC_SYS_ID
AND [MLR_SRC_SYS_CD] = @MLR_SRC_SYS_CD
AND [TRSRY_FEED_DT] = @TRSRY_FEED_DT)
BEGIN
INSERT INTO [dbo].[MLR_REBATE_IBOR_INFO_2]
([GL_DT],
[SRC_SYS_ID],
[MLR_SRC_SYS_CD],
[TRSRY_FEED_DT],
[Data])
SELECT
@GL_DT
,@SRC_SYS_ID
,@MLR_SRC_SYS_CD
,@TRSRY_FEED_DT
,@Data
END
ELSE
BEGIN
UPDATE [dbo].[MLR_REBATE_IBOR_INFO_2]
SET [Data] = @Data
WHERE [GL_DT] = @GL_DT
AND [SRC_SYS_ID] = @SRC_SYS_ID
AND [MLR_SRC_SYS_CD] = @MLR_SRC_SYS_CD
AND [TRSRY_FEED_DT] = @TRSRY_FEED_DT
END
END
GO
答案 0 :(得分:0)
我想我做到了:
CREATE PROCEDURE MyMergeTest
@GL_DT date
,@SRC_SYS_ID char(20)
,@MLR_SRC_SYS_CD char(3)
,@TRSRY_FEED_DT date
,@Data varchar(20)
AS
BEGIN
MERGE MLR_REBATE_IBOR_INFO_2 AS target
USING
(
SELECT
@GL_DT
,@SRC_SYS_ID
,@MLR_SRC_SYS_CD
,@TRSRY_FEED_DT
,@Data
) AS source
(
GL_DT
,SRC_SYS_ID
,MLR_SRC_SYS_CD
,TRSRY_FEED_DT
,Data
)
ON (
target.GL_DT = source.GL_DT AND
target.SRC_SYS_ID = source.SRC_SYS_ID AND
target.MLR_SRC_SYS_CD = source.MLR_SRC_SYS_CD AND
target.TRSRY_FEED_DT = source.TRSRY_FEED_DT
)
WHEN MATCHED THEN
UPDATE SET Data = source.Data
WHEN NOT MATCHED THEN
INSERT
(
[GL_DT],
[SRC_SYS_ID],
[MLR_SRC_SYS_CD],
[TRSRY_FEED_DT],
[Data]
)
VALUES
(
[GL_DT], --<<it looks like these can eiether be the variable eg, @GL_DT, or prefixed by 'source.'
[SRC_SYS_ID],
[MLR_SRC_SYS_CD],
[TRSRY_FEED_DT],
[Data]
);
END