如果它还不存在,我想在表中插入一行。我有以下直接执行的代码:
insert into MyTable (Column1, Column2, Year, Code, Id, UPDATE_IDENT)
select 'Default', 'Default', 2014, '', 0, 0
from sysibm.sysdummy1
WHERE NOT EXISTS (SELECT * FROM MyTable
WHERE Column1 = 'c1'
AND Column2 = 'c2'
AND Year = 2014)
然而,当我按年将其更改为一个参数:
insert into MyTable (Column1, Column2, Year, Code, Id, UPDATE_IDENT)
select 'Default', 'Default', @year, '', 0, 0
from sysibm.sysdummy1
WHERE NOT EXISTS (SELECT * FROM MyTable
WHERE Column1 = 'c1'
AND Column2 = 'c2'
AND Year = @year)
还尝试在原子部件中使用声明的变量但没有成功:
BEGIN ATOMIC
DECLARE varJaar INTEGER;
SET varYear = @year;
insert into MyTable (Column1, Column2, Year, Code, Id, UPDATE_IDENT)
select 'Default', 'Default', varYear , '', 0, 0
from sysibm.sysdummy1
WHERE NOT EXISTS (SELECT * FROM MyTable
WHERE Column1 = 'c1'
AND Column2 = 'c2'
AND Year = varYear )
END;
我无法执行此操作,因为我无法在我的选择部分中使用参数。试图将@year修改为@year作为年份,但仍然没有成功。
有没有办法可以在我的选择部分声明临时变量或其他东西来使用这个参数?
答案 0 :(得分:0)
最终通过创建临时表来解决这个问题。 变量暂时存储。这个表应该自动清除,但是在触发我的方法之前我仍然在try-catch块中执行drop table命令:
Try
Dim adapter As New DataAdapter()
Dim commandText As String = "drop table QTEMP.tempVariable"
Dim command As DbCommand = adapter.CreateDbCommand(commandText)
adapter.ExecuteNonQuery(command)
Catch notHandledEx As Exception
End Try
create table #tempVariable (year int);
insert into #tempVariable values(@Year);
insert into MyTable (Column1, Column2, Year, Code, Id, UPDATE_IDENT)
select 'Default', 'Default', (select max(year) from #tempVariable), '', 0, 0
from sysibm.sysdummy1
WHERE NOT EXISTS (SELECT * FROM MyTable
WHERE Column1 = 'c1'
AND Column2 = 'c2'
AND Year = @Year)