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

Declaring variable in PL/SQL when Commit transaction

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

DECLARE @UserID int:
SET @UserID = 2323;

BEGIN TRANSACTION;

DELETE FROM OWNER.TABLE
WHERE USER_ID = @UserID;

COMMIT TRANSACTION

但是 int 是红色的 - 错误说没有输入或输出

用这个

DECLARE @UserID int;  set @UserID = 2323

因为这是标记为 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 服务器,那么这有效:

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 |