SQL IF EXIST在脚本中

时间:2015-11-12 20:19:16

标签: sql-server sql-server-2008 jython-2.5

我这是在错误的地方,请指引我到正确的地方 我正在使用Ignition BY inductiveautomation,并尝试编写一个将根据IF EXIST条件更新或插入的脚本。 我有一个查询,将新记录插入下面列出的我的数据库,但现在我希望能够更新“设置表”,如果它已经存在。

Code:
queryValue1 = event.source.parent.getComponent('MoldName').selectedStringValue
queryValue2 = event.source.parent.getComponent('MachineName').selectedStringValue
queryValue3 = event.source.parent.getComponent('Text Area').text
if system.gui.confirm("Steve! Are you sure you want to Save changes?", 
   "Save Changes"):      
      system.db.runPrepUpdate("INSERT INTO setup_sheetlist (MoldName,machineName,Setupsheet) VALUES ('%s','%s','%s')" % (queryValue1, queryValue2 , queryValue3), getKey=1)   

这是我的查询,它在我的数据库中工作,但我对如何将其实现到RunPrepUpdate感到迷茫,

    Code:
    IF EXISTS( SELECT * FROM setup_sheetlist WHERE moldname='100SCX-3' AND Machinename='MM11')
       UPDATE setup_sheetlist 
       SET  setupsheet ='updated value 3'
       WHERE moldname='100SCX-3' AND Machinename='MM11'

    ELSE
           INSERT INTO setup_sheetlist(MoldName,machineName,Setupsheet)
           VALUES('100SCX-3','MM11','TRIAL 2')

这是我到目前为止,这给了我一个错误信息

    queryValue1 = event.source.parent.getComponent('MoldName').selectedStringValue
queryValue2 = event.source.parent.getComponent('MachineName').selectedStringValue
queryValue3 = event.source.parent.getComponent('Text Area').text
if system.gui.confirm("Steve! Are you sure you want to Save changes?", 
    "Save Changes"):        

            system.db.runPrepUpdate("IF EXISTS( SELECT * FROM setup_sheetlist WHERE moldname='?' AND Machinename='?')UPDATE setup_sheetlist SET  setupsheet ='?'    WHERE moldname='?' AND Machinename='?'" ,[queryValue1,queryValue2,queryValue3,queryValue1,queryValue2] )
            ELSE 
            INSERT 
            INTO 
            setup_sheetlist (MoldName,machineName,Setupsheet)
            VALUES ('%s','%s','%s')" % (queryValue1, queryValue2 , queryValue3))

错误

Error: java.lang.Exception: java.lang.Exception: Error executing system.db.runPrepUpdate(IF EXISTS( SELECT * FROM setup_sheetlist WHERE moldname='?' AND Machinename='?')UPDATE setup_sheetlist       SET  setupsheet ='updated value 3'   WHERE moldname='?' AND Machinename='?', [100SCX-3, MM09, 100SCX-3, MM09], , , false, false)


   caused by Exception: Error executing system.db.runPrepUpdate(IF EXISTS( SELECT * FROM setup_sheetlist WHERE moldname='?' AND Machinename='?')UPDATE setup_sheetlist       SET  setupsheet ='updated value 3'   WHERE moldname='?' AND Machinename='?', [100SCX-3, MM09, 100SCX-3, MM09], , , false, false)
   caused by GatewayException: SQL error for "IF EXISTS( SELECT * FROM setup_sheetlist WHERE moldname='?' AND Machinename='?')UPDATE setup_sheetlist       SET  setupsheet ='updated value 3'   WHERE moldname='?' AND Machinename='?'": The index 1 is out of range.
   caused by SQLServerException: The index 1 is out of range.

完整错误报告http://pastebin.com/d7Jt7fcr

1 个答案:

答案 0 :(得分:0)

我应该在查询结束时一次性传递变量,而不是将其分解为部分。

  system.db.runPrepUpdate("IF EXISTS( SELECT * FROM setup_sheetlist WHERE moldname=? AND Machinename=?)UPDATE setup_sheetlist SET setupsheet =? WHERE moldname=? AND Machinename=? ELSE INSERT INTO setup_sheetlist (MoldName,machineName,Setupsheet) VALUES (?,?,?)" , (queryValue1,queryValue2,queryValue3,queryValue1,queryValue2,queryValue1, queryValue2 , queryValue3 ))