SQL cmd.command包含OR​​语句

时间:2019-08-22 01:18:14

标签: sql command-text

下面是我的SQL cmd.CommandText查询:

cmd.CommandText = "SELECT CONVERT(date, [DateTime]) 'Date of Download', ActionBy AS 'User Full Name', COUNT(DISTINCT CAST(Identifier AS NVARCHAR(10)) + Remarks + Link) 'Number of Document Downloads' " + 
    " FROM [dbo].[AuditLog] "+
    " WHERE ActionTaken = 'Clicked' "+
    " and Type = 'Attachment Link'" +
    " and CONVERT(date, [DateTime]) BETWEEN CONVERT(date,'" + scanStartDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') and CONVERT(date,'" + scanEndDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') "+
    " GROUP BY CONVERT(date, [DateTime]), ActionBy "+
    " HAVING COUNT(DISTINCT Identifier) > " + limit + 
    " ORDER BY COUNT(DISTINCT Identifier) DESC";

我想为OR包括一个额外的参数Type,如下所示:

" and Type = 'Attachment Link' OR 'Attachment Link - Search'" +

我可以知道怎么做吗?

谢谢。

编辑:抱歉,未在错误消息中添加,如下所示。

  

在需要条件的上下文中在“和”附近指定的非布尔类型的表达式。

2 个答案:

答案 0 :(得分:2)

在“或”运算符后指定列

cmd.CommandText = "SELECT CONVERT(date, [DateTime]) 'Date of Download', ActionBy AS 'User Full Name', COUNT(DISTINCT CAST(Identifier AS NVARCHAR(10)) + Remarks + Link) 'Number of Document Downloads' " + 
        " FROM [dbo].[AuditLog] "+
        " WHERE ActionTaken = 'Clicked' "+
        " and (Type = 'Attachment Link' OR Type = 'Attachment Link - Search') " +
        " and CONVERT(date, [DateTime]) BETWEEN CONVERT(date,'" + scanStartDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') and CONVERT(date,'" + scanEndDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') "+
        " GROUP BY CONVERT(date, [DateTime]), ActionBy "+
        " HAVING COUNT(DISTINCT Identifier) > " + limit + 
        " ORDER BY COUNT(DISTINCT Identifier) DESC";

答案 1 :(得分:1)

我认为我过于简化了,但是你的意思是:

cmd.CommandText = "SELECT CONVERT(date, [DateTime]) 'Date of Download', ActionBy AS 'User Full Name', COUNT(DISTINCT CAST(Identifier AS NVARCHAR(10)) + Remarks + Link) 'Number of Document Downloads' " + 
    " FROM [dbo].[AuditLog] "+
    " WHERE ActionTaken = 'Clicked' "+
    " and (Type = 'Attachment Link' OR Type = 'Attachment Link - Search') " +
    " and CONVERT(date, [DateTime]) BETWEEN CONVERT(date,'" + scanStartDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') and CONVERT(date,'" + scanEndDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') "+
    " GROUP BY CONVERT(date, [DateTime]), ActionBy "+
    " HAVING COUNT(DISTINCT Identifier) > " + limit + 
    " ORDER BY COUNT(DISTINCT Identifier) DESC";

您可能需要()进行操作顺序,我认为您的意思是“ =”而不是“-”。