Oracle - 无法使用绑定变量更新表记录

时间:2015-06-15 21:50:10

标签: oracle bind-variables

此查询返回1行:

SELECT col1, col2 FROM table1 WHERE col1 = :column1;

但是这会更新0行:

UPDATE table1 SET col2 = :column2  WHERE col1 = :column1;
COMMIT;

我添加了这个约束来将col1设置为主键,但它没有修复它。

ALTER TABLE table1 ADD CONSTRAINT col1_pk PRIMARY KEY (col1);

我是从SQL Developer尝试这个,知道它为什么不更新行?

修改

col1是VARCHAR2(32 BYTE) NOT NULL

col2是CLOB NOT NULL

编辑2 :测试用例,在选择和更新句子中设置:var1到0011223344556677。

CREATE TABLE MY_TABLE 
   (    COL1 VARCHAR2(32 BYTE) NOT NULL ENABLE, 
    COL2 CLOB, 
     CONSTRAINT "MY_TABLE_PK" PRIMARY KEY ("COL1")
   )

INSERT INTO MY_TABLE (COL1, COL2) VALUES ('0011223344556677', '1434407992143440799214344079921434407992');

SELECT * FROM MY_TABLE WHERE COL1 = :var1;

UPDATE MY_TABLE SET COL2 = 'test' WHERE COL1 = :var1;
   COMMIT;

1 个答案:

答案 0 :(得分:1)

TL; DR - 确保将存储在绑定变量中的值解析为字符串而不是数字。

我在SQL Developer(版本4.0.3.16)中运行它:

CREATE TABLE MY_TABLE 
   (    COL1 VARCHAR2(32 BYTE) NOT NULL ENABLE, 
    COL2 CLOB, 
     CONSTRAINT "MY_TABLE_PK" PRIMARY KEY ("COL1")
   );
/
INSERT INTO MY_TABLE (COL1, COL2) VALUES ('0011223344556677', '1434407992143440799214344079921434407992');
/
VARIABLE var1 VARCHAR2(32);
/
BEGIN
  :var1 := '0011223344556677';
END;
/
SELECT * FROM MY_TABLE WHERE COL1 = :var1;
/
UPDATE MY_TABLE SET COL2 = 'test' WHERE COL1 = :var1;
/
COMMIT;
/
SELECT * FROM MY_TABLE;
/

运行良好:

table MY_TABLE created.
1 rows inserted.
anonymous block completed
COL1                             COL2                                                                           
-------------------------------- --------------------------------------------------------------------------------
0011223344556677                 1434407992143440799214344079921434407992                                         

1 rows updated.
committed.
COL1                             COL2                                                                           
-------------------------------- --------------------------------------------------------------------------------
0011223344556677                 test                                                                             

如果您将变量赋值更改为(删除引号):

BEGIN
  :var1 := 0011223344556677;
END;

然后将值解析为数字,忽略前导零,输出为:

table MY_TABLE created.
1 rows inserted.
anonymous block completed
no rows selected


0 rows updated.
committed.
COL1                             COL2                                                                           
-------------------------------- --------------------------------------------------------------------------------
0011223344556677                 1434407992143440799214344079921434407992