SQL 如果脚本中存在
SQL IF EXIST in a script
我走错区了,请指引我到正确的区
我正在使用 Ignition BY inductiveautomation,并尝试编写一个脚本,该脚本将根据 IF EXIST 条件进行更新或插入。
我有一个查询将在下面列出的我的数据库中插入一条新记录,但现在我希望能够更新 "setupsheet"(如果它已经存在)。
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.
我应该在查询结束时一次性传递所有变量,而不是将其分成几部分。
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 ))
我走错区了,请指引我到正确的区 我正在使用 Ignition BY inductiveautomation,并尝试编写一个脚本,该脚本将根据 IF EXIST 条件进行更新或插入。 我有一个查询将在下面列出的我的数据库中插入一条新记录,但现在我希望能够更新 "setupsheet"(如果它已经存在)。
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.
我应该在查询结束时一次性传递所有变量,而不是将其分成几部分。
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 ))