“你必须在'%'运算符的右侧提供一个值表达式。”查询语句中的错误

时间:2015-01-26 22:40:34

标签: mysql powershell

我的Powershell脚本包含查询 -

# This code defines the search string in the database table
$SQLQuery = "SELECT bug_id, 
       bug_status, 
       resolution, 
       short_desc, 
       DATE_FORMAT(deadline, "%m/%d/%Y")
FROM   bugs 
WHERE  ( bug_status IN ( 'RESOLVED', 'VERIFIED', 'INTEST' ) 
         AND deadline BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 30 DAY) 
       ) 
        OR ( bug_status IN ( 'RESOLVED', 'VERIFIED', 'INTEST' ) 
             AND deadline BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND 
                                  CURDATE() )        
       ORDER BY deadline ASC

"

在运行脚本时,它在日期格式化部分失败并出现以下错误 -

You must provide a value expression on the right-hand side of the '%' operator.
At line:5 char:32
+        DATE_FORMAT(deadline, "% <<<< m/%d/%Y")
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedValueExpression

有关如何转义这些字符的任何建议,但与此同时,它应该影响将在MySql数据库上触发的实际查询?

1 个答案:

答案 0 :(得分:0)

您在双引号字符串中使用双引号。未转义的嵌套双引号过早地终止了字符串,因此PowerShell将%解释为运算符,而不是字符串的一部分。

尝试用单引号替换嵌套的双引号,并使字符串成为正确的多行字符串:

$SQLQuery = @"
SELECT bug_id, 
       ...
       DATE_FORMAT(deadline, '%m/%d/%Y')
       ...
"@