Oracle 中的变量操作
所以我整个下午都在网上寻找这个问题的答案。我有一个如下所示的表:
CREATE TABLE ldap_domains (
domain varchar(128) NOT NULL,
name varchar(16) NOT NULL,
description varchar(32) NOT NULL
)
ALTER TABLE ldap_domains
ADD CONSTRAINT pk_domain PRIMARY KEY (domain);
INSERT INTO ldap_domains VALUES ('test', 'test', 'test');
INSERT INTO ldap_domains VALUES ('test1', 'test', 'test');
INSERT INTO ldap_domains VALUES ('test2', 'test', 'test');
INSERT INTO ldap_domains VALUES ('test3', 'test', 'test');
我需要修改该表,以便新列成为主键。作为其中的一部分,我需要在应用约束之前使用唯一值更新新列。看起来像这样:
ALTER TABLE ldap_domains
DROP CONSTRAINT pk_domain;
ALTER TABLE ldap_domains
ADD id int;
DECLARE @key int
SET @key = 0
UPDATE ldap_domains SET @key = id = @key + 1;
ALTER TABLE ldap_domains
ALTER COLUMN id int NOT NULL;
ALTER TABLE ldap_domains
ADD CONSTRAINT pk_id PRIMARY KEY (id);
这在 SQLServer 上按预期工作,但是,我也需要它在 Oracle 上工作。本质上,Oracle 不喜欢的部分是:
DECLARE @key int
SET @key = 0
UPDATE ldap_domains SET @key = id = @key + 1;
有人能启发我吗?
So I've been digging around on the net all afternoon to find the answer to this. I have a table that looks like so:
CREATE TABLE ldap_domains (
domain varchar(128) NOT NULL,
name varchar(16) NOT NULL,
description varchar(32) NOT NULL
)
ALTER TABLE ldap_domains
ADD CONSTRAINT pk_domain PRIMARY KEY (domain);
INSERT INTO ldap_domains VALUES ('test', 'test', 'test');
INSERT INTO ldap_domains VALUES ('test1', 'test', 'test');
INSERT INTO ldap_domains VALUES ('test2', 'test', 'test');
INSERT INTO ldap_domains VALUES ('test3', 'test', 'test');
I need to modify this table so that a new column becomes the primary key. As part of this I need to update the new column with unique values before the constraint is applied. This looks like so:
ALTER TABLE ldap_domains
DROP CONSTRAINT pk_domain;
ALTER TABLE ldap_domains
ADD id int;
DECLARE @key int
SET @key = 0
UPDATE ldap_domains SET @key = id = @key + 1;
ALTER TABLE ldap_domains
ALTER COLUMN id int NOT NULL;
ALTER TABLE ldap_domains
ADD CONSTRAINT pk_id PRIMARY KEY (id);
This works as expected with SQLServer, however, I need this to work for Oracle as well. Essentially the section Oracle doesn't like is:
DECLARE @key int
SET @key = 0
UPDATE ldap_domains SET @key = id = @key + 1;
Can anyone enlighten me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您要做的就是最初将每一行设置为具有不同的 ID。在 Oracle 中执行此操作的首选方法是使用序列:
这不能保证插入的值是连续的(尽管在本例中,它们很可能是连续的)(如果相关的话)。
由于您不太可能有任何其他进程同时更新该字段,因此如果您确实不想创建序列,那么稍微作弊并使用类似的东西可能是安全的:
据我所知,您无法在 Oracle 中同时读取和设置每行的环境变量。
It appears that what you're trying to do is initially set each row to have a different ID. The preferred way to do this in Oracle is to use a sequence:
This will not guarantee that the inserted values are sequential (though, in this case, they likely will be), if that's relevant.
Since it is not likely that you will have any other processes updating the field at the same time, it's probably safe to cheat a little and use something like this, if you really don't want to create a sequence:
As far as I know, you can't simultaneously read and set an environment variable on a per-row basis in Oracle.
Oracle 的 sytnax 不正确。
您可以尝试像
我建议您温习 PL/SQL 语法那样的方法,它有点不同。上面的代码示例只是一个起点,您可能还有更多的工作要做,如果您想在 sqlplus 脚本中执行此操作但不使用 PL/SQL 块,它看起来会有所不同。
我也承认我对
SET @key = id = @key + 1;
会做什么感到困惑,所以我不能 100% 确定如何翻译它。这是 SQL Server 的功能吗?这个声明会产生什么结果呢?The sytnax is not correct for Oracle.
You could try something like
I suggest you brush up on PL/SQL syntax, it's a bit different. This code sample above is just a starting point, you will probably have a bit more work to do, and it would look differnent if you wanted to do it in a sqlplus script but not use PL/SQL blocks.
I also admit I'm confused about what
SET @key = id = @key + 1;
will do, so I'm not 100% sure how to translate it. Is this a SQL-Server feature? What will be the result of this statement?