免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2857 | 回复: 0
打印 上一主题 下一主题

ORACLE OCI 带RETURNING子句的插入语句试验 [复制链接]

论坛徽章:
15
射手座
日期:2014-11-29 19:22:4915-16赛季CBA联赛之青岛
日期:2017-11-17 13:20:09黑曼巴
日期:2017-07-13 19:13:4715-16赛季CBA联赛之四川
日期:2017-02-07 21:08:572015年亚冠纪念徽章
日期:2015-11-06 12:31:58每日论坛发贴之星
日期:2015-08-04 06:20:00程序设计版块每日发帖之星
日期:2015-08-04 06:20:00程序设计版块每日发帖之星
日期:2015-07-12 22:20:002015亚冠之浦和红钻
日期:2015-07-08 10:10:132015亚冠之大阪钢巴
日期:2015-06-29 11:21:122015亚冠之广州恒大
日期:2015-05-22 21:55:412015年亚洲杯之伊朗
日期:2015-04-10 16:28:25
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-04-02 16:52 |只看该作者 |倒序浏览
本帖最后由 yulihua49 于 2010-04-02 17:00 编辑

在设计表结构时,往往一个表没有合适的主键,常设计一个自增列,或default uuid列,插入数据时可以不插这个列。这个列往往用于外连接,可是插入后可能就找不到哪个是刚插入的了。此时必须使用带RETURNING子句的插入语句,在插入完成后立即得到键值。

  1. tuxticket@jgbticket:~/test> cat t_ret.c
  2. /**********************************************************
  3. * test RETURNING  
  4. **********************************************************/

  5. #include <sqli.h>
  6. /*****************************************
  7. create table ttype (
  8.         "id" number(2),
  9.         "i64" number(20),
  10.         "i2" number(4),
  11.         "d8" number(12,2),
  12.         "d4" number(4,2),
  13.         "t1" timestamp default systimestamp,
  14.         "r5" raw(128),
  15.         "date1" date default sysdate,
  16.         primary key ("t1")
  17. )
  18. *****************************************/


  19. typedef struct {
  20.         char id;
  21.         char i64[22];
  22.         short i2;
  23.         double d8;
  24.         double d4;
  25.         INT64 t1;
  26.         char r5[128];
  27.         char date1[YEAR_TO_SEC_LEN];
  28. } TTYPE_stu;

  29. main(int argc,char **argv)
  30. {
  31. T_SQL_Connect SQL_Connect;
  32. TTYPE_stu tt;
  33. int ret,sth;
  34. char stmt[4096],timestamp[30];
  35. struct timeval ts;
  36. INT64 now;

  37.         if(argc>1) ret=envcfg(argv[1]);
  38.         gettimeofday(&ts,0);
  39.         ret=db_open(&SQL_Connect);
  40.         if(ret) {
  41.                 printf("oprn database error!\n");
  42.                 return 1;
  43.         }
  44.         ___SQL_Transaction__(&SQL_Connect,TRANBEGIN);
  45.         tt.id=14;
  46.         tt.i2=0;
  47.         tt.d8=1234567890.12;
  48.         tt.d4=12.34;
  49.         rsecstrfmt(tt.date1,now_sec(),"YYYY-MM-DD HH24:MI:SS");
  50.         strcpy(stmt,"INSERT INTO TICKET.TTYPE (\"id\",\"i2\",\"d8\",\"d4\",\"date1\") VALUES(:1,:2,:3,:4,TO_DATE(:5,'YYYY-MM-DD HH24:MI:SS')) RETURNING TO_CHAR(\"t1\",'YYYY-MM-DD HH24:MI:SS.FF6') INTO :6");
  51.         sth=sqlo_prepare(SQL_Connect.dbh,stmt);
  52.         if(sth<0) {
  53.                 ___SQL_GetError(&SQL_Connect);
  54.                 printf("prepare %s err=%d,%s\n",stmt,
  55.                         SQL_Connect.Errno,
  56.                         SQL_Connect.ErrMsg);
  57.                  ___SQL_CloseDatabase__(&SQL_Connect);
  58.                 return 1;
  59.         }
  60.         if(SQLO_SUCCESS !=
  61.                 (sqlo_bind_by_pos(sth, 1, SQLOT_INT, &tt.id, sizeof(tt.id), NULL, 0)) ||
  62.                 (sqlo_bind_by_pos(sth, 2, SQLOT_INT, &tt.i2, sizeof(tt.i2), NULL, 0)) ||
  63.                 (sqlo_bind_by_pos(sth, 3, SQLOT_FLT, &tt.d8, sizeof(tt.d8), NULL, 0)) ||
  64.                 (sqlo_bind_by_pos(sth, 4, SQLOT_FLT, &tt.d4, sizeof(tt.d4), NULL, 0)) ||
  65.                 (sqlo_bind_by_pos(sth, 5, SQLOT_STR, tt.date1, sizeof(tt.date1), NULL, 0)) ||
  66.                 (sqlo_bind_by_pos(sth, 6, SQLOT_STR, timestamp, sizeof(timestamp), NULL, 0))) {

  67.                 ___SQL_GetError(&SQL_Connect);
  68.                 printf("bind %s err=%d,%s\n",stmt,
  69.                         SQL_Connect.Errno,
  70.                         SQL_Connect.ErrMsg);
  71.                 ___SQL_Close__(&SQL_Connect,sth);
  72.                 ___SQL_CloseDatabase__(&SQL_Connect);
  73.                 return 2;
  74.         }
  75.         ret=sqlo_execute(sth,1);
  76.         if(SQLO_SUCCESS != ret) {
  77.                 ___SQL_GetError(&SQL_Connect);
  78.                 printf("execute %s err=%d,%s\n",stmt,
  79.                         SQL_Connect.Errno,
  80.                         SQL_Connect.ErrMsg);
  81.                 ___SQL_Close__(&SQL_Connect,sth);
  82.                 ___SQL_CloseDatabase__(&SQL_Connect);
  83.                 return 3;
  84.         }

  85.         printf("exec=%d,timestamp=%s\n",ret,timestamp);

  86.         ___SQL_Close__(&SQL_Connect,sth);
  87.         ___SQL_Transaction__(&SQL_Connect,TRANROLLBACK);
  88.         ___SQL_CloseDatabase__(&SQL_Connect);
  89.         return 0;
  90. }

  91. tuxticket@jgbticket:~/test> ./t_ret ld.ini
  92. exec=0,timestamp=2010-04-02 16:44:43.625854


复制代码
参考:
http://blog.chinaunix.net/u3/92831/showart_2205055.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP