Insert values into table from the same table - sql

Using SQL server (2012)
I have a table - TABLE_A with columns
(id, name, category, type, reference)
id - is a primary key, and is controlled by a separte table (table_ID) that holds the the primary next available id. Usually insertions are made from the application side (java) that takes care of updating this id to the next one after every insert. (through EJBs or manually, etc..)
However,
I would like to to write stored procedure (called from java application) that
- finds records in this table where (for example) reference = 'AAA' (passed as
parameter)
- Once multiple records found (all with same reference 'AAA', I want it to INSERT new
records with new ID's and reference = 'BBB', and other columns (name, category, type)
being same as in the found list.
I am thinking of a query similar to this
INSERT INTO table_A
(ID
,NAME
,CATEGORY
,TYPE,
,Reference)
VALUES
(
**//current_nextID,**
(select NAME
from TABLE_A
where REFENCE in (/*query returning value 'AAA' */),
(select CATEGORY
from TABLE_A
where REFENCE in (/*query returning value 'AAA' */),
(select TYPE
from TABLE_A
where REFENCE in (/*query returning value 'AAA' */),
'BBB - NEW REFERENCE VALUE BE USED'
)
Since, I don't know how many records I will be inserting , that is how many items in the result set of a criteria query
select /*field */
from TABLE_A
where REFENCE in (/*query returning value 'AAA' */),
I don't know how to come up with the value of ID, on every record. Can anyone suggest anything, please ?

It's not clear from your question how sequencing is handled but you can do something like this
CREATE PROCEDURE copybyref(#ref VARCHAR(32)) AS
BEGIN
-- BEGIN TRANSACTION
INSERT INTO tablea (id, name, category, type, reference)
SELECT value + rnum, name, category, type, 'BBB'
FROM
(
SELECT t.*, ROW_NUMBER() OVER (ORDER BY id) rnum
FROM tablea t
WHERE reference = 'AAA'
) a CROSS JOIN
(
SELECT value
FROM sequence
WHERE table_id = 'tablea'
) s
UPDATE sequence
SET value = value + ##ROWCOUNT + 1
WHERE table_id = 'tablea'
-- COMMIT TRANSACTION
END
Sample usage:
EXEC copybyref 'AAA';
Here is SQLFiddle demo

Related

Oracle query to find the latest record which is not null and based on the other table field value

Table A looks like,
am doing to select details of max(key) say,
select * from A where key in (select max(key) from A);
Running the above query gives output,
Key Number type
2915935 B
where Number is Null.
I want to find the number from next max(key) but type from the current max value. If null again find the number field from next max(wo_key) so that i get output like below,
2915935 06924278753 B
Please suggest a way i can do the above.
If i got it right, I think the idea here is to fetch the number from next max(key) provided the number for current max(key) is null.
select a.key,c.Number,a.type from tableA a
join (select max(key) as key from tableA) b on a.key = b.key
join (select number from
(
select number from tableA
where number is not null
order by key desc
)where rownum = 1)c on 1=1;
let me know if this is what you looking for.
Try this:
CREATE TABLE A("Key" NUMBER(20), "Number" NUMBER(20), "Type" VARCHAR(10));
INSERT INTO A VALUES(2915929,'','A');
INSERT INTO A VALUES(2915935,'','B');
INSERT INTO A VALUES(1582987,'03892448882','A');
INSERT INTO A VALUES(2175622,'05924488825','C');
INSERT INTO A VALUES(2385156,'06924278753','V');
select "Key"
,NVL(NULLIF("Number",0), (SELECT MAX("Number") FROM A)) AS "Number"
,"Type"
from A
where "Key" in (select max("Key") from A);
Output:
KEY NUMBER TYPE
2915935 6924278753 B
Check the # SQL Fiddle

SQL Get the row number of the inserted row

I am trying to get the row number of an inserted record so I can use it for a select statement. What I am trying to accomplish is insert a person into one table, get that row number and then select something from another table where the row numbers match. Here is what I got so far:
INSERT INTO TableA Values (‘Person’)
Select timeToken
From
(
Select
Row_Number() Over (Order By tokenOrder) As RowNum
, *
From TableB WHERE taken = false
) t2
Where RowNum = (Row Number of Inserted Item)
How do I get the row number of the inserted item, I want to compare ids as some records might have been deleted so they would not match.
TABLEA Data (primary key is id)
id name
3 John
12 Steve
TABLEB Data (primary key is id)
id timeToken tokenOrder taken
2 1:00am 1 false
3 2:00am 2 false
5 3:00am 3 true
6 4:00am 4 false
My expect result when I insert person, the select take would return 4:00am
I am doing this in a stored procedure.
It is an error to think that rows have numbers unless an ORDER BY clause is included.
The only way to find a row after you have inserted it is to search for it. Presumably your table has a primary key; use that to search for it.
Try This .It may help you out
Declare #TableA_PK BIGINT
INSERT INTO TableA Values ('Person')
SET #TableA_PK=SCOPE_IDENTITY()
Select timeToken
From
(
Select
Row_Number() Over (Order By tokenOrder) As RowNum
, *
From TableB WHERE taken = false
) t2
Where RowNum =#TableA_PK
SCOPE_IDENTITY(): Scope Identity will captures the last inserted record primary key value and which can be stored in a varaible and
and then it can be for further re-use
By the sounds of it you are trying to do something like what is listed on thhe following link LINK - SQL Server - Return value after INSERT
Basically :
INSERT INTO TableA (Person)
OUTPUT Inserted.ID
VALUES('bob');
Adding a foreign key constraint(referencing primary key in table A) in table b will be good since you won't be able to delete records from table A without deleting them from table B. It'll be helpful for comparing the records using ID.
Try this
declare #rowNum int;
INSERT INTO TableA Values ('Person')
SET #rowNum =SCOPE_IDENTITY()
select * from TableA where id = #rowNum

sql server: How to detect changed rows

I want to create a trigger to detect whether a row has been changed in SQL Server. My current approach is to loop through each field, apply COLUMNS_UPDATED() to detect whether UPDATE has been called, then finally compare the values of this field for the same row (identified by PK) in inserted vs deleted.
I want to eliminate the looping from the procedure. Probably I can dump the content of inserted and deleted into one table, group on all columns, and pick up the rows with count=2. Those rows will count as unchanged.
The end goal is to create an audit trail:
1) Track user and timestamp
2) Track insert, delete and REAL changes
Any suggestion is appreciated.
Instead of looping you can use BINARY_CHECKSUM to compare entire rows between the inserted and deleted tables, and then act accordingly.
Example
Create table SomeTable(id int, value varchar(100))
Create table SomeAudit(id int, Oldvalue varchar(100), NewValue varchar(100))
Create trigger tr_SomTrigger on SomeTable for Update
as
begin
insert into SomeAudit
(Id, OldValue, NewValue)
select i.Id, d.Value, i.Value
from
(
Select Id, Value, Binary_CheckSum(*) Version from Inserted
) i
inner join
(
Select Id, Value, Binary_CheckSum(*) Version from Deleted
) d
on i.Id = d.Id and i.Version <> d.Version
End
Insert into sometable values (1, 'this')
Update SomeTable set Value = 'That'
Select * from SomeAudit

Return id if a row exists, INSERT otherwise

I'm writing a function in node.js to query a PostgreSQL table.
If the row exists, I want to return the id column from the row.
If it doesn't exist, I want to insert it and return the id (insert into ... returning id).
I've been trying variations of case and if else statements and can't seem to get it to work.
A solution in a single SQL statement. Requires PostgreSQL 8.4 or later though.
Consider the following demo:
Test setup:
CREATE TEMP TABLE tbl (
id serial PRIMARY KEY
,txt text UNIQUE -- obviously there is unique column (or set of columns)
);
INSERT INTO tbl(txt) VALUES ('one'), ('two');
INSERT / SELECT command:
WITH v AS (SELECT 'three'::text AS txt)
,s AS (SELECT id FROM tbl JOIN v USING (txt))
,i AS (
INSERT INTO tbl (txt)
SELECT txt
FROM v
WHERE NOT EXISTS (SELECT * FROM s)
RETURNING id
)
SELECT id, 'i'::text AS src FROM i
UNION ALL
SELECT id, 's' FROM s;
The first CTE v is not strictly necessary, but achieves that you have to enter your values only once.
The second CTE s selects the id from tbl if the "row" exists.
The third CTE i inserts the "row" into tbl if (and only if) it does not exist, returning id.
The final SELECT returns the id. I added a column src indicating the "source" - whether the "row" pre-existed and id comes from a SELECT, or the "row" was new and so is the id.
This version should be as fast as possible as it does not need an additional SELECT from tbl and uses the CTEs instead.
To make this safe against possible race conditions in a multi-user environment:
Also for updated techniques using the new UPSERT in Postgres 9.5 or later:
Is SELECT or INSERT in a function prone to race conditions?
I would suggest doing the checking on the database side and just returning the id to nodejs.
Example:
CREATE OR REPLACE FUNCTION foo(p_param1 tableFoo.attr1%TYPE, p_param2 tableFoo.attr1%TYPE) RETURNS tableFoo.id%TYPE AS $$
DECLARE
v_id tableFoo.pk%TYPE;
BEGIN
SELECT id
INTO v_id
FROM tableFoo
WHERE attr1 = p_param1
AND attr2 = p_param2;
IF v_id IS NULL THEN
INSERT INTO tableFoo(id, attr1, attr2) VALUES (DEFAULT, p_param1, p_param2)
RETURNING id INTO v_id;
END IF;
RETURN v_id:
END;
$$ LANGUAGE plpgsql;
And than on the Node.js-side (i'm using node-postgres in this example):
var pg = require('pg');
pg.connect('someConnectionString', function(connErr, client){
//do some errorchecking here
client.query('SELECT id FROM foo($1, $2);', ['foo', 'bar'], function(queryErr, result){
//errorchecking
var id = result.rows[0].id;
};
});
Something like this, if you are on PostgreSQL 9.1
with test_insert as (
insert into foo (id, col1, col2)
select 42, 'Foo', 'Bar'
where not exists (select * from foo where id = 42)
returning foo.id, foo.col1, foo.col2
)
select id, col1, col2
from test_insert
union
select id, col1, col2
from foo
where id = 42;
It's a bit longish and you need to repeat the id to test for several times, but I can't think of a different solution that involves a single SQL statement.
If a row with id=42 exists, the writeable CTE will not insert anything and thus the existing row will be returned by the second union part.
When testing this I actually thought the new row would be returned twice (therefor a union not a union all) but it turns out that the result of the second select statement is actually evaluated before the whole statement is run and it does not see the newly inserted row. So in case a new row is inserted, it will be taken from the "returning" part.
create table t (
id serial primary key,
a integer
)
;
insert into t (a)
select 2
from (
select count(*) as s
from t
where a = 2
) s
where s.s = 0
;
select id
from t
where a = 2
;

Oracle View subquery fails on table creation

I have the statement below when creating a Oracle view. The statement is to return a particular value as MYVALUE column.
(SELECT myval
FROM (SELECT myval
FROM mytable
WHERE primary_key = /*CS.primary_key*/ 12345
ORDER BY table_primary_key ASC)
WHERE ROWNUM < 2) AS MYVALUE,
The inner query can return more than one row. I am only interested in the 1st record and it must be ordered by the table_primary_key, hence the use of the sub query to allow the ROWNUM selection.
When I create the query in its current state above, the view is created successfully. When I uncomment CS.primary_key and remove the hardcoded 12345, the view creation fails, with no description of why
(SQLDeveloper 2.1): "Failed: Warning: execution completed with warning".
Also, just to try and narrow down the problem, I removed the ORDER BY, and ROWNUM as below, and the same error occurs
(SELECT myval
FROM (SELECT myval
FROM mytable
WHERE primary_key = CS.primary_key)
) AS MYVALUE,
Lastly, I know CS.primary_key is a valid reference, as I use in in other parts of my view without issues.
Any idea why the reference is valid, or how to get a more detailed error message?
edit: updated starting opening bracket
EDIT2: Thanks for the responses so far. Here is a summary of the problem, I think the CS.PRIMARY key should be in scope, as I use it in other places in my query. The code below works, but if I replace the hardcoded 1 to CS.primary_key, it fails:
drop view myview;
drop table mytable;
drop table mytable_parent;
drop table proof_table;
-- ISSUE TABLES
create table mytable_parent ( primary_key number primary key );
create table mytable ( primary_key number, myval varchar(255), parent_primary_key number);
insert into mytable_parent values (1);
insert into mytable_parent values (2);
insert into mytable values (1, 'myval1-1', 1);
insert into mytable values (2, 'myval1-2', 1);
insert into mytable values (3, 'myval2-1', 2);
-- EXAMPLE TABLE TO PROVE CS.* WORKS
create table proof_table ( primary_key number primary key, parent_primary_key number, any_old_value varchar(255));
insert into proof_table values (1, 1, 'proofval1-1');
insert into proof_table values (2, 2, 'proofval1-2');
-- VIEW
CREATE OR REPLACE FORCE VIEW myview AS
SELECT
-- PROOF STATEMENT USING CS.primary_key SUCCESSFULLY
(SELECT any_old_value FROM proof_table WHERE parent_primary_key IN
(SELECT primary_key FROM proof_table WHERE parent_primary_key =
-- USING CS REFERENCE, NO PROBLEM
CS.primary_key)
) AS PROOF_VALUE,
-- PROBLEM STATEMENT
(SELECT myval FROM (SELECT myval FROM mytable
WHERE parent_primary_key = /*CS.primary_key*/ 1
ORDER BY primary_key ASC)
WHERE ROWNUM < 2) AS MYVALUE
-- DEFINE CS
FROM mytable_parent CS;
From the limited information you have supplied I'd suggest that CS.PRIMARY_KEY is out of scope for your subquery. Hence it compiles OK when you use a literal and doesn't compile when it has to resolve the reference to CS.PRIMARY_KEY.
Include whichever table CS refers to in the subquery (with the relevent criteria) to check that this is the case.
If it is then you'll need to rewrite your query to ensure the CS table is in scope for all its dependencies.
Hope it helps...
See the details in this link: http://etutorials.org/SQL/Mastering+Oracle+SQL/Chapter+5.+Subqueries/5.4+Inline+Views/
You will see that inline views are executed prior to the outer query and nesting the reference to the CS table too deeply will cause it to become out of scope.
Replace the nested subquery where you do your ordering:
(SELECT myval
FROM (SELECT myval
FROM mytable
WHERE parent_primary_key = CS.primary_key
ORDER BY primary_key ASC)
WHERE ROWNUM < 2) AS MYVALUE
with an analytic function or other way of limiting your rows and it will work fine
This is untested but replacing the above code with:
(SELECT DISTINCT
FIRST_VALUE(myval) OVER (PARTITION BY parent_primary_key ORDER BY primary_key)
FROM mytable
WHERE parent_primary_key = CS.primary_key) AS MYVALUE
might be close to what you want and the reference to CS is in scope.