简体   繁体   English

IF语句查询语法错误

[英]IF Statement Query Syntax Error

I am trying to replace a foreach statement with a single SQL query, my code I want to change looks like this: 我试图用一个SQL查询替换一个foreach语句,我要更改的代码如下所示:

 this.Settings = new List<Setting>()
        {
            // Write all the settings from the VB
            new Setting() { Name = "NOOFORDERS", Value = null },
            new Setting() { Name = "ORDNVGREQ", Value = null },
            new Setting() { Name = "IncWendsDel", Value = null },
            new Setting() { Name = "AUTOEMAIL_INVOICE", Value = null },
            new Setting() { Name = "REQEMAIL_INVOICE", Value = null },
            new Setting() { Name = "REQEMAIL_SALES", Value = null },
            new Setting() { Name = "REQEMAIL_PICKSLIP", Value = null },
            new Setting() { Name = "REQEMAIL_DESPATCH", Value = null },
            new Setting() { Name = "CARRPARTSHIP", Value = null },
            new Setting() { Name = "DELNOTEREQ", Value = null },
            new Setting() { Name = "REQ_CARRPARTSHIP", Value = null },
            new Setting() { Name = "BUDGETREQ", Value = null },
            new Setting() { Name = "PriceList", Value = null },
            new Setting() { Name = "EmployeeRenew", Value = null },
            new Setting() { Name = "Warehouse", Value = null },
            new Setting() { Name = "EmployeeDetails", Value = null },
        };
var value = this.db.tblbus_setvalues.Where( x => x.SettingID == setting.Name && x.BusinessCode == this.Business.BusinessCode ).FirstOrDefault().Value;
switch (value.GetType())
{
    case typeof(bool):
        (value) ? setting.Value = "true" : setting.Value = "false";
        break;
    default:
        setting.Value = value.ToString();
        break;
    }
    if ( !string.IsNullOrEmpty(setting.Value) ) setting.Value = this.db.tblbus_settings.Where( x => x.SettingID == setting.Name ).FirstOrDefault().Default.ToString();

My SQL which I want to replace the above code with looks like this: 我想用上面的代码替换上面的SQL,如下所示:

SELECT t1.SettingID, t2.Value, t1.Default
FROM `tblbus_settings` t1
LEFT JOIN `tblbus_setvalues` t2
ON t1.SettingID = t2.SettingID
WHERE `BusinessCode` = "XXX" // Dependency
AND t1.`BusType` = "CUS"

Can anyone help me write an IF statement onto this SQL query which will only display the t2.Value if the t1.Default is null ? 有人可以帮我在此SQL查询上写IF语句,如果t1.Defaultnull时,该语句仅显示t2.Value吗?

I tried things like: 我尝试过类似的事情:

IF(t2.value == NULL) { t1.Default }
ELSE { t2.Value }

But they all give me SQL syntax errors. 但是它们都给了我SQL语法错误。

您可以使用合并函数,该函数返回列表中的第一个非NULL值,如果没有非NULL值,则返回NULL:

 SELECT t1.SettingID, COALESCE(t2.Value, t1.Default) FROM ...

Something like this? 像这样吗

SELECT t1.SettingID, t2.Value, t1.Default, 
CASE t1.Default IS NOT NULL
   THEN t1.Default 
   ELSE t2.Value
FROM `tblbus_settings` t1
LEFT JOIN `tblbus_setvalues` t2
ON t1.SettingID = t2.SettingID
WHERE `BusinessCode` = "XXX" // Dependency
AND t1.`BusType` = "CUS"

See this for meore https://msdn.microsoft.com/en-gb/library/ms181765.aspx 请参阅此https://msdn.microsoft.com/en-gb/library/ms181765.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM