Update while copy records - sql

I want to copy records from one table to another. While doing this I want to set a flag of those records I copy.
This is how I would do it (simplified):
BEGIN TRANSACTION copyTran
insert into destination_table (name)
select top 100 name
from source_table WITH (TABLOCKX)
order by id
update source_table
set copy_flag = 1
where id in (select top 100 id from source_table order by id)
COMMIT TRANSACTION copyTran
Is there an easier way?

By leveraging OUTPUT clause you can boil it down to a single UPDATE statement
UPDATE source_table
SET copy_flag = 1
OUTPUT inserted.name
INTO destination_table(name)
WHERE id IN
(
SELECT TOP 100 id
FROM source_table
ORDER BY id
)
Note: Now tested. Should work just fine.

The problem with your query is, that you may get different records in your UPDATE if someone inserts some data while you are running your query. It is saver to use the INSERTED keyword.
Declare #temp TABLE (Id integer);
INSERT INTO destination_table (name)
OUTPUT INSERTED.Id into #temp
SELECT TOP 100 name
FROM source_table
ORDER BY id
UPDATE source_table
SET copy_flag = 1
WHERE Id IN (SELECT Id FROM #temp)

I think that you could use a temporary table in which you will store the top 100 ids from your source table, after having ordered them. This way you will avoid executing the select statement in where clause of update for each id.
BEGIN TRANSACTION copyTran
insert into destination_table (name)
select top 100 name
from source_table
order by id
declare #Ids TABLE(id int)
#Ids = (select top 100 id from source_table order by id)
update source_table
set copy_flag = 1
where id in (SELECT * FROM #Ids)
COMMIT TRANSACTION copyTran

Related

Trigger to update foreign key field after insert on same table

I have two tables:
Table1 (surveyid [PKID], surveyname)
Table2 (visitid [PKID], surveyname, surveyid [FKID - refers to Table1]).
After inserting a new row into Table2, I would like to update Table2.surveyid with the surveyid from Table1, based on matching surveyname.
I thought it maybe wasn't possible (or good practice?) to create a trigger to update the same table. But I seem to have created a trigger that will do this. The problem is that after insert, the trigger updates the surveyid for every row, instead of just the newly inserted rows.
This trigger code works, but how do I ensure the trigger only updates the surveyid for newly inserted rows, and not all rows?
CREATE TRIGGER tr_update_table2_fk
ON Table2
AFTER INSERT
AS
BEGIN
UPDATE Table2
SET surveyid = (SELECT t1.surveyid
FROM Table1 t1
WHERE t1.surveyname = Table2.surveyname)
END;
Thank you MattM and DaleK, you've helped me figure out the answer. I was adding the inserted table into the subquery where clause before, instead of the query where clause. This does the trick:
CREATE TRIGGER tr_update_table2_fk
on Table2
AFTER INSERT
AS
BEGIN
UPDATE Table2 SET
surveyid = (
SELECT t1.surveyid
FROM Table1 t1
WHERE t1.surveyname = Table2.surveyname
)
WHERE Table2.visitid IN (SELECT visitid FROM inserted)
END;
Yes, the inserted table is the answer.
I'd use it to capture the visitids of the inserted rows, then filter by them in a WHERE clause on the end of your UPDATE statement in your trigger.
E.g.
CREATE OR ALTER TRIGGER tr_update_table2_fk
ON Table2
AFTER INSERT
AS
BEGIN
DROP TABLE IF EXISTS #VisitIds ;
CREATE TABLE #VisitIds ( [id] INT ) ;
INSERT INTO #VisitIds ( [id] )
SELECT [visitid]
FROM inserted ;
UPDATE Table2
SET [surveyid] =
(
SELECT t1.[surveyid]
FROM Table1 AS t1
WHERE t1.[surveyname] = Table2.[surveyname]
)
WHERE [visitid] IN ( SELECT [id] FROM #VisitIds ) ;
END
GO

how to see difference between 2 tables

I have 2 tables: 1 temp and the other one is my main table.
Each day I would update my temp table and I want to update my main table based on the changes I made from the temp table.
Example: The first temp table contains an id and name. Then I insert the value from temp into the main table. But when I made changes from my temp like insert another id and name, I want my main table to compare and only insert the unique id from the temp table.
As you said, it seems like you have a table object named as temp table. If this is the case then you may use after insert trigger on temp table to insert new inserted value in your main table.
CREATE TRIGGER AfterINSERTTrigger on [Temptable]
FOR INSERT
AS DECLARE #id INT,
#col1 VARCHAR(50),
.
.
SELECT #id = ins.id FROM INSERTED ins;
SELECT #col1 = ins.col1 FROM INSERTED ins;
.
.
INSERT INTO [MainTable](
[id]
,[col1]
.
.)
VALUES (#id,
#col1,
.
.
.
);
PRINT 'We Successfully Fired the AFTER INSERT Triggers in SQL Server.'
GO
Similarly you can update your table on update of record in temptable using update trigger. You may find this link on more info on trigger. LINK
OR
If you are creating temp table object to get the new inserted record then use simple not in or not exists clause to get the newly inserted record.
Using NOT IN
insert into maintable ( id, col1, ...)
select Id , col1, .... from temptable
where id not in (select id from maintable)
Using NOT EXISTS
insert into maintable ( id, col1, ... )
select id, col1, ... from temptable as temp
where not exists (select id from maintable as main where main.id=temp.id)
You can use NOT EXISTS as follows
INSERT into main_table(
id, name,
...
)
SELECT
id,name,
...
FROM temp_table t
WHERE
NOT EXISTS(
SELECT 1
FROM main_table m
WHERE m.id = t.id
)
Cheers!!

with clause execution order

In a simplified scenario I have table T that looks somthing like:
Key Value
1 NULL
1 NULL
1 NULL
2 NULL
2 NULL
3 NULL
3 NULL
I also have a very time-consuming function Foo(Key) which must be considered as a black box (I must use it, I can't change it).
I want to update table T but in a more efficient way than
UPDATE T SET Value = dbo.Foo(Key)
Basically I would execute Foo only one time for each Key.
I tried something like
WITH Tmp1 AS
(
SELECT DISTINCT Key FROM T
)
, Tmp2 AS
(
SELECT Key, Foo(Key) Value FROM Tmp1
)
UPDATE T
SET T.Value = Tmp2.Value
FROM T JOIN Tmp2 ON T.Key = Tmp2.Key
but unexpectedly computing time doesn't change at all, because Sql Server seems to run Foo again on every row.
Any idea to solve this without other temporary tables?
One method is to use a temporary table. You don't have much control over how SQL Server decides to optimize its queries.
If you don't want a temporary table, you could do two updates:
with toupdate as (
select t.*, row_number() over (partition by id order by id) as seqnum
from t
)
update toupdate
set value = db.foo(key)
where seqnum = 1;
Then you can run a similar update again:
with toupdate as (
select t.*, max(value) over (partition by id) as as keyvalue
from t
)
update toupdate
set value = keyvalue
where value is null;
You might try it like this:
CREATE FUNCTION dbo.Foo(#TheKey INT)
RETURNS INT
AS
BEGIN
RETURN (SELECT #TheKey*2);
END
GO
CREATE TABLE #tbl(MyKey INT,MyValue INT);
INSERT INTO #tbl(MyKey) VALUES(1),(1),(1),(2),(2),(3),(3),(3);
SELECT * FROM #tbl;
DECLARE #tbl2 TABLE(MyKey INT,TheFooValue INT);
WITH DistinctKeys AS
(
SELECT DISTINCT MyKey FROM #tbl
)
INSERT INTO #tbl2
SELECT MyKey,dbo.Foo(MyKey) TheFooValue
FROM DistinctKeys;
UPDATE #tbl SET MyValue=TheFooValue
FROM #tbl
INNER JOIN #tbl2 AS tbl2 ON #tbl.MyKey=tbl2.MyKey;
SELECT * FROM #tbl2;
SELECT * FROM #tbl;
GO
DROP TABLE #tbl;
DROP FUNCTION dbo.Foo;

Can I use the output clause in the Update command to generate 2 rows

I can use OUTPUT to generate the before and after row values when doing an update to a table. And I can put these values into a table.
But they go into the table in 1 single row.
I need the before values as a row, and the after values as a new row.
So this snippet isn't working for me at the moment, but it shows the thing I am trying to do. Is there any way to do this using OUTPUT.
update dbo.table1
set employee = employee + '_updated'
OUTPUT 'd',DELETED.* INTO dbo.table2,
OUTPUT 'i',INSERTED.* INTO dbo.table2,
WHERE id = 4 OR id = 2;
This snippet below works, but only creates a single row:
update dbo.table1
set employee = employee + '_updated'
OUTPUT 'd', DELETED.*, 'i', INSERTED.* INTO dbo.table2,
WHERE id = 4 OR id = 2;
I can do it using triggers, but that's not allowed in this case.
And I can do it manually (selecting what I'm going to update into table2, then doing the update...)
Any tips or hints appreciated on how to do it just using just OUTPUT in the update?
Rgds, Dave
To elaborate on an answer given in the comments:
declare #TempTable table (
d_id int,
d_employee varchar(50),
d_other varchar(50),
u_id int,
u_employee varchar(50),
u_other varchar(50)
)
update Table1
set employee = employee + '_updated'
output deleted.id d_id, deleted.employee d_employee, deleted.other d_other,
inserted.id u_id, inserted.employee u_employee, inserted.other u_other
into #TempTable
where id = 4 or id = 2;
insert Table2 (change_type, employee_id, employee, other)
select
'd',
d_id,
d_employee,
d_other
from #TempTable
union all
select
'i',
u_id,
u_employee,
u_other
from #TempTable
I made some assumptions about your schema as it wasn't given, but this should get you started.

Delete multiple duplicate rows in table

I have multiple groups of duplicates in one table (3 records for one, 2 for another, etc) - multiple rows where more than 1 exists.
Below is what I came up with to delete them, but I have to run the script for however many duplicates there are:
set rowcount 1
delete from Table
where code in (
select code from Table
group by code
having (count(code) > 1)
)
set rowcount 0
This works well to a degree. I need to run this for every group of duplicates, and then it only deletes 1 (which is all I need right now).
If you have a key column on the table, then you can use this to uniquely identify the "distinct" rows in your table.
Just use a sub query to identify a list of ID's for unique rows and then delete everything outside of this set. Something along the lines of.....
create table #TempTable
(
ID int identity(1,1) not null primary key,
SomeData varchar(100) not null
)
insert into #TempTable(SomeData) values('someData1')
insert into #TempTable(SomeData) values('someData1')
insert into #TempTable(SomeData) values('someData2')
insert into #TempTable(SomeData) values('someData2')
insert into #TempTable(SomeData) values('someData2')
insert into #TempTable(SomeData) values('someData3')
insert into #TempTable(SomeData) values('someData4')
select * from #TempTable
--Records to be deleted
SELECT ID
FROM #TempTable
WHERE ID NOT IN
(
select MAX(ID)
from #TempTable
group by SomeData
)
--Delete them
DELETE
FROM #TempTable
WHERE ID NOT IN
(
select MAX(ID)
from #TempTable
group by SomeData
)
--Final Result Set
select * from #TempTable
drop table #TempTable;
Alternatively you could use a CTE for example:
WITH UniqueRecords AS
(
select MAX(ID) AS ID
from #TempTable
group by SomeData
)
DELETE A
FROM #TempTable A
LEFT outer join UniqueRecords B on
A.ID = B.ID
WHERE B.ID IS NULL
It is frequently more efficient to copy unique rows into temporary table,
drop source table, rename back temporary table.
I reused the definition and data of #TempTable, called here as SrcTable instead, since it is impossible to rename temporary table into a regular one)
create table SrcTable
(
ID int identity(1,1) not null primary key,
SomeData varchar(100) not null
)
insert into SrcTable(SomeData) values('someData1')
insert into SrcTable(SomeData) values('someData1')
insert into SrcTable(SomeData) values('someData2')
insert into SrcTable(SomeData) values('someData2')
insert into SrcTable(SomeData) values('someData2')
insert into SrcTable(SomeData) values('someData3')
insert into SrcTable(SomeData) values('someData4')
by John Sansom in previous answer
-- cloning "unique" part
SELECT * INTO TempTable
FROM SrcTable --original table
WHERE id IN
(SELECT MAX(id) AS ID
FROM SrcTable
GROUP BY SomeData);
GO;
DROP TABLE SrcTable
GO;
sys.sp_rename 'TempTable', 'SrcTable'
You can alternatively use ROW_NUMBER() function to filter out duplicates
;WITH [CTE_DUPLICATES] AS
(
SELECT RN = ROW_NUMBER() OVER (PARTITION BY SomeData ORDER BY SomeData)
FROM #TempTable
)
DELETE FROM [CTE_DUPLICATES] WHERE RN > 1
SET ROWCOUNT 1
DELETE Table
FROM Table a
WHERE (SELECT COUNT(*) FROM Table b WHERE b.Code = a.Code ) > 1
WHILE ##rowcount > 0
DELETE Table
FROM Table a
WHERE (SELECT COUNT(*) FROM Table b WHERE b.Code = a.Code ) > 1
SET ROWCOUNT 0
this will delete all duplicate rows, But you can add attributes if you want to compare according to them .