所以我已经在这个代码上挠头好一阵子了,我不明白为什么我的代码不允许我这么做。
这在我的create语句中:
create table account ( number int not null generated always as identity (start with 1000, increment by 1, no cache),
id int not null,
balance int not null check (balance >=0),
type char (1) not null check (type in ('C', 'S')),
status char (1) not null check (status in ('A', 'I')),
primary key (number)); 这是我的sqc文件中的内容。
printf ("Please enter your ID: ");
scanf("%d", &vid);
printf ("Please enter your Account type (C for checking or S for Saving): ");
scanf("%s", &vtype);
printf ("Please enter your initial balance: ");
scanf("%d", &vbalance);
exec sql insert into account (id, balance, type, status)
values (:vid, :vbalance, :vtype, 'A');
exec sql declare accountid cursor for
select number
from account
group by number order by number desc fetch first 1 rows only;
exec sql open accountid;
exec sql fetch accountid into :vnumber;
exec sql close accountid;
printf("Your new account ID: %d", vnumber);由于某种原因,它总是返回0,而不是1000:
Please enter your ID: 100
Please enter your Account type (C for checking or S for Saving): C
Please enter your initial balance: 100
Your new account ID: 0之后,我还检查了该表,发现它仍然是空的,就好像该命令从未运行过或拒绝了insert命令一样。
编辑:所以我认为我的主要问题是试图插入到我的‘状态’,'A‘。我尝试将其放入一个变量中,并将该变量发送到表中,但它将该变量作为null返回。因为我的表不想要空值,所以它拒绝了它,因此没有创建事务。
我用测试值手动插入到P2.Account中,它起作用了,输出了正确的值。那么,有没有人能帮我找到一个源码,帮助我在插入语句中输入'A‘,而不会变成null呢?这可能是我唯一的问题。
发布于 2014-11-28 01:43:54
在数据库中启动事务时,您可以选择回滚(撤消)更改或提交(保存)更改。有多个原因导致了这种功能。第一种情况是在插入多条记录时遇到错误;回滚将撤消所有操作,这样您就可以修复错误并重新启动插入。
看起来您缺少insert后的语句:
EXEC SQL COMMIT;我更熟悉甲骨文和Pro-C,但是IBM在DB2站点上有一个很好的小教程:
https://stackoverflow.com/questions/27160140
复制相似问题