我有以下选择:
SELECT name, text, lang FROM texts
WHERE name IN @r_names
AND lang IN ( @lv_lang, 'E' )
INTO TABLE @DATA(lt_texts).
它将为给定名称选择多行文本的文本。
我怎么说我想要带有lang = lv_lang
的文本,但如果它们不存在,那么选择一个在对数据库的一个请求中全部lang = 'E'
并且在应用程序端没有处理的文本?
您可以使用UNION
运算符来完成此任务:
SELECT name, text, lang FROM texts
WHERE name IN @r_names
AND lang = @lv_lang
UNION
SELECT name, text, lang FROM texts
WHERE name IN @r_names
AND lang = 'E'
AND NOT EXISTS ( SELECT name
FROM texts
WHERE name IN @r_names
AND lang = @lv_lang
)
INTO TABLE @DATA(lt_texts).
我喜欢合并这种事情(它将用第一个非空值填充您的目标(。您可以将 sy-langu 作为默认值,并按优先级顺序使用更多语言:
SELECT SINGLE coalesce( default~eqktx, greek~eqktx, english~eqktx )
FROM equi AS e LEFT OUTER JOIN eqkt AS default
ON default~equnr = e~equnr
AND default~spras = @sy-langu
LEFT OUTER JOIN eqkt AS greek
ON greek~equnr = e~equnr
AND greek~spras = 'G'
LEFT OUTER JOIN eqkt AS english
ON english~equnr = e~equnr
AND english~spras = 'E'
WHERE e~equnr = @ls_equi-equnr
INTO @DATA(lv_eqktx).
您的示例将变为:
SELECT coalesce( default~name, english~name ),
coalesce( default~text, english~text ),
coalesce( default~lang, english~lang )
FROM texts AS default LEFT OUTER JOIN texts AS english
ON english~name = default~name
AND english~lang = 'E'
WHERE default~name IN @r_names
AND default~lang = @lv_lang
INTO TABLE @DATA(lt_texts).
基于 Suncatcher 的答案,但没有 UNION。没有尝试过,所以我不知道它是否按预期工作:
SELECT name, text, lang FROM texts
WHERE name IN @r_names
AND ( lang = @lv_lang
OR ( NOT EXISTS ( SELECT name
FROM texts
WHERE name IN @r_names
AND lang = @lv_lang
)
AND lang = 'E'
)
)
INTO TABLE @DATA(lt_texts).