Friday
Hi, ALL,
Given the following definition:
CREATE TABLE verify(name varchar(50), condition text, error text);
CREATE UNIQUE INDEX ind_verify ON verify( name );
How do I make sure I insert the records only if the name is not present already, but if its present just do nothing and continue without error.
I want to populate this table every time I start the program and establish a good connection to the DB.
Is check subquery the only way?
If I do an insert right now I am getting an error about unique constraint violation.
Thank you.
Working with SAP ASE 16.1.
Also someone needs to add/fix the appropriate tag.
When I type "SAP ASE" I see: "SAP ASE - BW Enablement" SAP ASE - ERP Enablement", "SAP ASE - HADR Enablement" and "SAP HANA accelerator for SAP ASE". What I don't see is "Adaptive Server Enterprise".
Thx.
Request clarification before answering.
There are a few ways to do what you want ...
We'll start by creating the table+index and performing an initial insert:
CREATE TABLE verify(name varchar(50), condition text, error text)
go
CREATE UNIQUE INDEX ind_verify ON verify( name )
go
insert verify (name, condition, error)
values ('name1', 'good condition', 'good error')
goFor each approach we'll perform two inserts:
insert / select / where not exists(...)
-- should NOT insert
insert verify (name, condition, error)
select 'name1', 'bad condition', 'bad error'
where not exists(select name from verify where name = 'name1')
go
-- should insert
insert verify (name, condition, error)
select 'name2', 'good condition', 'good error'
where not exists(select name from verify where name = 'name2')
goif not exists(...) / insert
-- should NOT insert
if not exists(select name from verify where name = 'name2')
begin
insert verify (name, condition, error)
values ('name2', 'bad condition', 'bad error')
end
go
-- should insert
if not exists(select name from verify where name = 'name3')
begin
insert verify (name, condition, error)
values ('name3', 'good condition', 'good error')
end
gomerge / when not matched
-- should NOT insert
merge into verify as tgt
using (select 'name3','bad condition', 'bad error') as src (name,condition,error)
on tgt.name = src.name
when not matched
then insert (name,condition,error)
values (src.name, src.condition, src.error)
go
-- should insert
merge into verify as tgt
using (select 'name4','good condition', 'good error') as src (name,condition,error)
on tgt.name = src.name
when not matched
then insert (name,condition,error)
values (src.name, src.condition, src.error)
goAt this point we should have 4 rows in our table, all with 'good condition' and 'good error' entries [NOTE: output has been edited to remove excessive spacing]:
select *
from verify
order by name
go
name condition error
----- -------------- ----------
name1 good condition good error
name2 good condition good error
name3 good condition good error
name4 good condition good errorObviously these example are basic T-SQL syntax. I can't speak to how easy (or hard) these are to implement if using a programmed front end (eg, python, perl,), let alone how to handle any special characters that may be embedded in the values of the two text fields.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 8 | |
| 8 | |
| 7 | |
| 4 | |
| 3 | |
| 3 | |
| 2 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.