INSERT INTO test_table (avail_seq) VALUES ((SELECT
COALESCE(MAX(avail_seq), 0) + 1 FROM test_table));
Whenever I run that SQL query, I get the following error:
The name 'avail_seq' is illegal in this context. Only constants,
constant expressions, or variables allowed here. Column names are
illegal. (severity 15, procedure N/A)
Here's my table definition:
create table test_table (
avail_seq int not null,
constraint test_table primary key (avail_seq)
)
grant all on test_table to public;
It seems to me like using IDENTITY would solve this, but alas, I
didn't create the table.
An alternative would be to do the SELECT subquery, by itself, and then
the INSERT, later, but that wouldn't be atomic.
In MySQL, I could use transactions. ie.
BEGIN;
@avail_seq = SELECT COALESCE(MAX(avail_seq), 0) + 1 FROM test_table;
INSERT INTO test_table (avail_seq) VALUES (@avail_seq);
COMMIT;
Or something like that. Unfortunately, I'm not sure how to do that
with Sybase.
Any ideas?
Bret_Halford - 23 Sep 2008 18:50 GMT
Write it without the VALUES keyword:
insert into test_table (avail_seq) select COALESCE(MAX(avail_seq), 0)
+ 1 FROM test_table
yawnmoth - 25 Sep 2008 23:57 GMT
> Write it without the VALUES keyword:
>
> insert into test_table (avail_seq) select COALESCE(MAX(avail_seq), 0)
> + 1 FROM test_table
That worked - thanks!