如何将给定的SQL查询转换为SQL Server查询?

问题描述

我有一个SQL查询,我需要了解它并转换为sql Server查询。这是SQL查询

MERGE INTO study_event
    USING dual
    ON (study_id = :study_id)
       WHEN NOT MATCHED THEN
       INSERT (study_id,study_date)
       VALUES (:study_id,:study_date)

根据我的理解,这意味着如果study_event表未提供study_id,则将行插入具有给定study_id和study_date的study_event表。这是正确的吗?

我正在尝试创建将执行相同操作的sql Server查询。从堆栈溢出中,这就是我发现的内容

INSERT INTO study_event(study_id,study_date)
SELECT x.*
FROM (VALUES(@study_id,@study_date)) as x(study_id,study_date)
WHERE NOT EXISTS (SELECT 1 FROM studies s1 WHERE s1.study_id = x.study_id)

我想我理解这个查询,但是我不确定SELECT x.*是什么意思。此查询有效吗?x。*是什么意思?

解决方法

x指代values()子查询创建的别名,

FROM (VALUES(@study_id,@study_date)) as x(study_id,study_date)

这将创建一个派生表,该表具有一行study_idstudy_date和两列。

因此,基本上x.*为您提供了x的整行,其中包含查询的两个原始参数@study_id@study_date

免责声明:I answered the original question!