update and insert from another table based on date and column id - sql

Say I have two tables test and test2, here is the data:
create table test (id number primary key, name varchar2(20), insertion_date date);
create table test2 (id number primary key, name varchar2(20), insertion_date date);
insert into test values (1,'Jay','05-Jan-19');
insert into test values (2,'John','05-Jan-20');
insert into test2 values (1,'Jay','05-Mar-25');
insert into test2 values (2,'John','05-Mar-25');
insert into test2 values (3,'Maria','05-Mar-22');
It looks like that:
test
1 Jay 05-JAN-19
2 John 05-JAN-20
test2
1 Jay 05-MAR-25
2 John 05-MAR-25
3 Maria 05-MAR-22
I want to update the row in test if the insertion_date column in the test2 table is in the future of insertion_date of test table and I want to insert every row that has an id from test2 which is not present in test.
I tried this for the update clause:
update test
set name = case when test.insertion_date < test2.insertion_date then test2.name else test.name end,
insertion_date = case when test.insertion_date < test2.insertion_date then test2.insertion_date else test.insertion_date end,
where test.id = test2.id;
But getting this error:
Error report -
SQL Error: ORA-01747: invalid user.table.column, table.column, or column specification
01747. 00000 - "invalid user.table.column, table.column, or column specification"
*Cause:
*Action:
For the insert column I got it working by doing this:
insert into test select * from test2 where id not in (select id from test);
However I was wondering if there is a better way to update and insert in one single clause.
the desired result is this:
test
1 Jay 05-MAR-25
2 John 05-MAR-25
3 Maria 05-MAR-22

Use MERGE.
merge into test
using test2
on (test.id = test2.id)
when matched then update test.insertion_date = test2.insertion_date
where test2.insertion_date > test.insertion_date
when not matched then insert (id,name,insertion_date)
values (test2.id,test2.name,test2.insertion_date)

Related

Is there a way to insert the same row values in a temp table? SQL Server

I have a temp table #t that has a column already called ID with about 75 values. I've inserted another column called status and I want all of the values in the 'status' column to equal "A". Is there a way I can do this without having to manually insert A for each row?
Would want it to look like this but for all 75 rows
|ID| |Status|
----------------
|24| | A |
Not sure if I understand your question correctly but you could do it with something like
update #t set Status = 'A'
You can do that by setting the default value for status column.
Create table #test(id int, status varchar(1) default 'A');
insert into #test (id) values (1),(2),(3);
Select * From #test;
id status
1 A
2 A
3 A
If your table is already created you may set the default value as the following:
ALTER TABLE #test ADD CONSTRAINT df_val DEFAULT 'A' FOR status;
See a demo from db<>fiddle.

Not able to insert a row in a table which has auto incremented primary key

I have a table reportFilters which has the following column names:
The reportFilterId is auto increment. I want to insert a row in the table with the script below:
IF OBJECT_ID(N'ReportFilters', N'U') IS NOT NULL
BEGIN
IF NOT EXISTS (SELECT * FROM [ReportFilters]
WHERE ReportId IN (SELECT ReportId FROM [Reports] WHERE ReportType = 'Operational Insights Command Staff Dashboard') )
BEGIN
INSERT INTO [ReportFilters] Values(1, 'SelectView', 'Select Views', 13, 'Views','Views', 'SelectView', 'a', 'b', 'c' );
END
END
GO
But I am getting the following error:
Column name or number of supplied values does not match table definition.
Can I please get help on this ? Thanks in advance.
I think the problem is on inserted columns can't match with inserted data because that will instead by your table column order which is ReportFilterId instead of ReportId
So that there are 11 columns in your table but your statement only provides 10 columns.
I would use explicitly specify for inserted columns (inserted columns start from ReportId except your PK ReportFilterId column)
INSERT INTO [ReportFilters] (ReportId,ReportFilterName,ReportFilterTitle....)
Values (1, 'SelectView', 'Select Views', 13, 'Views','Views', 'SelectView', 'a', 'b', 'c' );

Merge sql throws Unique constraint violation error

I have below two table for which when i query table TEST_RUA:
select CLASS, ID_LL, ID_UU, TKR from TEST_RUA where ID_UU= 'GV9999B12M0'
it returns:
CLASS ID_LL ID_UU TKR
Bond (null) GV9999B12M0 WIB
When i query table TEST_RUA_MER:
select CLASS, ID_LL, ID_UU, TKR from TEST_RUA_MER where ID_UU= 'GV9999B12M0'
it returns:
CLASS ID_LL ID_UU TKR
Bond (null) GV9999B12M0 WIB
You can see both the values are same for table where ID_UU= 'GV9999B12M0'. The table TEST_RUA_MER has unique index on columns ID_LL, ID_UU, TKR.
Now i have below merge query which throws error as ORA-00001: unique constraint violated and i dont understand how can i avoid this error as both the table values are same then in this case this merge query should try to update and not to insert in table TEST_RUA_MER .
merge into TEST_RUA_MER h using (
select distinct r.CLASS, r.ID_LL, r.ID_UU, r.TKR from TEST_RUA r ) s
on (s.ID_LL=h.ID_LL and s.ID_UU=h.ID_UU and s.TKR=h.TKR) when matched then
update set h.CLASS = s.CLASS, h.ID_LL = s.ID_LL, h.ID_UU = s.ID_UU, h.TKR = s.TKR
when not matched then insert values (s.CLASS, s.ID_LL, s.ID_UU, s.TKR);
Looks like NULL causes problems; it isn't "equal" to anything, so this:
on (s.ID_LL=h.ID_LL
fails.
Try with
on (nvl(s.ID_LL, -1) = nvl(h.ID_LL, -1)
(depending on what ID_LL column's datatype is; I presumed it is a number).
Yes, As mentioned in the other answer also, cause of the error is s.ID_LL=h.ID_LL. You can update it as
( s.ID_LL=h.ID_LL OR (s.ID_LL is null and h.ID_LL is null) )
OR
( s.ID_LL=h.ID_LL OR coalesce(s.ID_LL, h.ID_LL) is null )

SQLDelight FTS5 insert trouble

I created a table in DBBrowser:
CREATE VIRTUAL TABLE IF NOT EXISTS Students USING FTS5
(
GroupId UNINDEXED,
StudentName
);
and insert values to it. After that I add DB with this table to my project.
It is declaration of this table in sqldelight .sq file:
CREATE VIRTUAL TABLE IF NOT EXISTS Students USING FTS5
(
GroupId INTEGER AS Int,
StudentName TEXT,
rank REAL
);
I need to explicit declare rank because I want to apply HAVING MIN(rank) for it when SELECT from table (otherwise it is not compile), but when I trying to insert values in table like that:
insert:
INSERT INTO Students VALUES (?,?);
I receive an error:
Unexpected number of values being inserted. found: 2 expected: 3
If I do like that:
insert:
INSERT INTO Students VALUES (?,?,?);
I receive an exception:
SQLiteException - table Students has 2 columns but 3 values were supplied (code 1): , while compiling: INSERT INTO Students VALUES (?,?,?)
How I can perform insert? Or maybe I can apply HAVING MIN(rank) without explicit declare?
does
insert:
INSERT INTO Students(GroupId, StudentName) VALUES (?,?);
work?

Do we have Sequence in Sybase

I see tutorial talking about Sequence with syntax and example.
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc00801.1601/doc/html/saiq-create-sequence-statement.html
But when I try creating sequence in Syabse through Squirrel, I get below error.
Error: 'SEQUENCE' is not a recognized CREATE option.
SQLState: ZZZZZ
ErrorCode: 155
Then how do we create auto increment unique identifier in Sybase.
How do we use Identity in Sequence. How come sequence available in one Sybase version and not in another.
I connect to sybase using below dependency.
<dependency>
<groupId>com.sybase.jdbcx</groupId>
<artifactId>jconn3</artifactId>
<version>6.0</version>
</dependency>
A sequence as an object does not exist in Sybase ASE (assumption). The equivalent functionality is done using identity columns.
create table test_tab (
test_tab_id int identity,
test_tab_name varchar(30) not null )
go
insert into test_tab (test_tab_name) values ('Hello')
go
insert into test_tab (test_tab_name) values ('World')
go
select * from test_tab
go
Result
(1 row affected)
(1 row affected)
test_tab_id test_tab_name
1 Hello
2 World
(2 rows affected)
One more point to add on identity is:
Once you truncate the table and insert data the old value of the identity is taken and then incremented.
Example:
select * from test_tab
go
2 rows
1 hello
2 world
Truncate the table test_tab
go
insert into test_tab (test_tab_name) values ('Hai')
go
insert into test_tab (test_tab_name) values ('done')
go
select * from test_tab
go
You will get as below
3 hai
4 done
can we assign the sequence per combination
E.g.
1 ABC
2 ABC
3 ABC
4 ABC
1 KLO
2 KLO
3 KLO