在提交事务时在PL / SQL中声明变量

时间:2015-12-17 11:07:06

标签: sql database oracle sqltransaction

我尝试在Oracle Developer中编写此事务:

DECLARE @UserID int:
SET @UserID = 2323;

BEGIN TRANSACTION;

DELETE FROM OWNER.TABLE
WHERE USER_ID = @UserID;

COMMIT TRANSACTION

但是int是红色的 - 错误说没有进出

2 个答案:

答案 0 :(得分:1)

由于这是标记为Oracle而您说您正在使用Oracle Developer ...

您正在使用的语法对于Oracle数据库来说完全错误 - 您似乎正在尝试使用用于SQL SERVER的代码。

为oracle重写它可以很简单:

DELETE FROM OWNER.TABLE
WHERE USER_ID = 2323;

COMMIT;

但是,如果你特别需要一个绑定变量,那么:

VARIABLE UserID NUMBER;

BEGIN
  :UserID := 2323;
END;
/

DELETE FROM OWNER.TABLE
WHERE USER_ID = :UserID;

COMMIT;

或(使用pl / sql变量):

DECLARE
  UserID OWNER.TABLE.USER_ID%TYPE := 2323;
BEGIN
  DELETE FROM OWNER.TABLE
  WHERE USER_ID = UserID;

  COMMIT;
END;
/

但是,如果它是SQL Server,那么它可以工作:

SQL Fiddle

MS SQL Server 2014架构设置

CREATE TABLE table_name ( User_ID INT );

INSERT INTO table_name VALUES ( 2322 );
INSERT INTO table_name VALUES ( 2323 );

DECLARE @UserID INT = 2323;

BEGIN TRANSACTION;

DELETE FROM table_name
WHERE  User_ID = @UserID;

COMMIT TRANSACTION;

查询1

SELECT * FROM table_name

<强> Results

| User_ID |
|---------|
|    2322 |

答案 1 :(得分:0)

使用这个

DECLARE @UserID int;  set @UserID = 2323