0

I have a table named AccessRight with this sample data:

ID   Accessfor   AccessOn
---------------------------
 1     40            51
 2     40            52
 3     40            53
 4     43            51
 4     43            54
 5     43            55

I want to insert/delete records of accessfor=40 and insert this table as a accessfor=43 means table will have records like below

1     43            51
2     43            52
3     43            53
4     43            54
5     43            55

Please suggest best optimum approach to solved this.

1
  • cant you make update to Accessfor = 40 to Accessfor = 43 Commented Jan 1, 2015 at 7:02

5 Answers 5

1

use update and delete

Use CTE to delete the duplicate records after updation.

update table set Accessfor = 43 where Accessfor = 40

;with cte as
(
select row_number() over (partition by Accessfor, AccessOn order by id) rn,* 
from table
)
delete from cte where rn>1
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you.But doing above query is giving error like below.Please do the needful 'AK_UserGroupRelation_GroupIDUserID'. The duplicate key value is (43, 51)
There seem to be constraint on table. First drop the constraint in start of query and add constraint again after delete
DBA will not allow to drop constrains,Please do the need ful.
@SandipkTatva - what is the problem in dropping and recreating
1
 Insert into AccessRight(ID,AccessFor,AccessOn)
   (select id,43,AccessOn from AccessRight where accessfor = 40 
   and accessOn not in(select accessOn from AccessRight where accessfor = 43));
   Delete from AccessRight where accessfor = 40;

EDIT : (Try this)

Insert into AccessRight(ID,AccessFor,AccessOn)
(
select id,0,AccessOn from AccessRight 
where id+''+accessfor+''+accesson not in (
select id+''+accessfor+''+accesson from accessright
where accessfor = 43
and accesson in(select accesson from accessright where accessfor = 40)
)
);
delete from accessright where accessfor <> 0;
update accessright set accessfor = 43;

Comments

0

you can just use update query to solve your problem like

update yourtablename set Accessfor=43 where Accessfor=40

i hope this may help you

1 Comment

Then what about the duplicate entries like 43 51 and 43 51?Please advise priya786
0

What you can do is take distinct AccessOn record to temp table and truncate first table and re insert all the distinct records with Accessfor = 43.

Check this :

-- Your Main Table
Create table #temp1
(
   Accessfor int,
   AccessOn int
)

-- This will be temp table
Create table #temp2
(
   Accessfor int,
   AccessOn int
)

-- Insert into Main table. This step was for me to get data in Main table.
Insert into #temp1 values(40, 51)
Insert into #temp1 values(40, 52)
Insert into #temp1 values(40, 53)
Insert into #temp1 values(43, 51)
Insert into #temp1 values(43, 54)
Insert into #temp1 values(43, 55)

select * from #temp1

--From here actual operation starts...
Insert into #temp2
  Select Distinct '43', AccessOn From #temp1

Truncate table #temp1
Insert into #temp1
  Select * From #temp2

select * from #temp1

Comments

0

then you can check it with the query

mysql_query("select id from tablename where  Accessfor=43 &&  Accesson='anyvalue'");
if(mysql_num_rows()>0)
{
mysql_query("delete from tablename Accessfor=43 &&  Accesson='anyvalue'");
}
else
{
//you do update
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.