I have a message table that contains a message id field, a language id field and a text field. The application should display a message based on the identifier and language, which together form a unique key. All messages exist for the EN language, but not all have been translated into other languages. Therefore, for the English language, one album will always be selected. But if the user is French, and the application should display message No. 17, and it does not yet exist for the French language, I want to return message No. 17 to EN . I would like to execute this in one SELECT query, preferably without IF expressions.
EDIT: based on the answers provided - it is necessary to clarify that each message is translated into 10 languages, and maybe more. there must always be exactly one line returned based on the message id and lang id. but if this line does not exist, you must return the English message.
Final code:
declare @msgid int=2, @langid varchar(2)='fr' SELECT isnull(xx.msg, en.msg) msgtext FROM appmessages en LEFT JOIN appmessages xx ON en.msgid = xx.msgid and xx.langid=@langid WHERE en.langid = 'en' and en.msgid=@msgid
source share