Groovy的代碼是Groovy的:從文件中使用參數
import groovy.sql.*
dbUrl = 'jdbc:sqlserver://server'
dbUser = 'username'
dbPassword = 'password'
dbDriver = 'com.microsoft.sqlserver.jdbc.SQLServerDriver'
sql = Sql.newInstance(dbUrl, dbUser, dbPassword, dbDriver)
def sqlfromfile = new SqlFromFile()
sqlfromfile.sql_filename='select_query.sql'
sqlfromfile.read()
try{
def result = sql.rows(sqlfromfile.result,[id:'01'])
println result
}catch(e){
println e
}
class SqlFromFile {
def sql_filename
def read(){
result= new File("sqlfolder/"+sql_filename).text
return result
}}
它的正常工作與SQL文件:
SELECT * FROM [db].[dbo].[TABLE] WHERE [ID] = :id
但如果我嘗試做LIKE SELECT
SELECT * FROM [db].[dbo].[TABLE] WHERE [ID] LIKE %:id%
我有錯誤Incorrect syntax near '@P0'
有沒有辦法解決這個問題?
如果'id'是數字(如IDS往往是),爲什麼你想使用'like'? –
有時我需要選擇從第一個字符開始的id,如:SELECT * FROM'TABLE' LIKE'01%' – lsv