如何在Groovy脚本中的sql where子句中添加引号?

时间:2015-06-08 14:55:57

标签: sql sql-server groovy

Collegues,请帮我解决Groovy中的sql问题。 在我的SOAP UI groovy脚本中,我有sql查询:

sql.eachRow('select top 1 '+     
              'Country, '+    
   'from dbo.Address where UPPER(Country) = "JAPAN" ORDER BY NEWID()')  

一切都很好,直到我在where子句中没有引号的情况下工作。在我添加UPPER(国家)=" JAPAN"我收到一个假装:

  

com.microsoft.sqlserver.jdbc.SQLServerException:列名称无效   '日本'

如何使用where子句中的引号重写查询?

2 个答案:

答案 0 :(得分:4)

或者在Groovy代码中使用不同的引号字符:

 Shell Function Definitions
   A shell function is an object that is called like a simple command and executes a compound
   command with a new set of positional parameters.  Shell functions are declared as follows:

   name () compound-command [redirection]
   function name [()] compound-command [redirection]
          This defines a function named name.  The reserved word function  is  optional.   If
          the  function reserved word is supplied, the parentheses are optional.  The body of
          the function is  the  compound  command  compound-command  (see  Compound  Commands
          above).  That command is usually a list of commands between { and }, but may be any
          command listed under Compound Commands above.  compound-command is  executed  when-
          ever  name is specified as the name of a simple command.  Any redirections (see RE-
          DIRECTION below) specified when a function is defined are performed when the  func-
          tion is executed.  The exit status of a function definition is zero unless a syntax
          error occurs or a readonly function with the same name already exists.   When  exe-
          cuted,  the  exit  status of a function is the exit status of the last command exe-
          cuted in the body.  (See FUNCTIONS below.)

或多行字符串:

sql.eachRow("select top 1 " +     
            "Country, " +    
            "from dbo.Address where UPPER(Country) = 'JAPAN' ORDER BY NEWID()")

或带边距的多行字符串:

sql.eachRow('''select top 1
               Country, 
               from dbo.Address where UPPER(Country) = 'JAPAN' ORDER BY NEWID()''')

答案 1 :(得分:1)

我更喜欢参数而不是文字,特别是因为如果值来自用户输入或者值本身包含引号,它可以正常工作:

sql.eachRow('''select .... from dbo.Address 
     where UPPER(Country) = :country ...''', 
   [country: 'Japan'])