October 17, 2012 at 8:39 am
I have a table with name roleMaster( Roleid,rollname). Roll id is a PK in this table which is coming from another table of another database with name Roles (roleid,rolename).
I am writing to a script to import data in rolemaster from Roles table and for this first I need to truncate the table Rolemaster. But I am not able to do it.
Plz tell me the way to truncate the rolemaster so that I can transfer the data from Role table to RoleMaster..
Its very urgent..
_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
October 17, 2012 at 8:42 am
TRUNCATE TABLE RoleMaster
or, if it has foreign keys
DELETE FROM RoleMaster
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 17, 2012 at 8:44 am
Hi Kapil,
How are you trying to do this?, what error is being returned? try the above recommendations and if they do not work post further information. however it sounds like you have data being referenced by a foreign key elsewhere. in which case you may need to remove the links from that table also first before attempting to delete if you wish to avoid data becoming redundent.
October 17, 2012 at 9:20 am
truncate is not working..
Gives error-
Cannot truncate table 'rolemaster' because it is being referenced by a FOREIGN KEY constraint.
_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
October 17, 2012 at 9:21 am
i tried by disabling the constraint on table roles master but after doing that also it didnt worked
_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
October 17, 2012 at 9:27 am
you need to find the table that is referencing the data in your rolemaster table and remove it before you remove it. otherwhilst you will have a lot of problems on your hands further down the line. You need to follow Atomicity rules ie remove all associated data before proceeding to truncate the table.
October 17, 2012 at 1:25 pm
beebkhan (10/17/2012)
you need to find the table that is referencing the data in your rolemaster table and remove it before you remove it. otherwhilst you will have a lot of problems on your hands further down the line. You need to follow Atomicity rules ie remove all associated data before proceeding to truncate the table.
Hi
You don't have to delete the table referencing the rolemaster table data. You can modify its FK so that it takes some actions:
1. ON DELETE CASCADE (will delete the referencing rows)
2. ON DELETE SET NULL (FK must be nullable)
3. ON DELETE SET DEFAULT (the default value must be present in the parent table)
It's up to you what you want to do with the data in the table referencing rolemaster table data.
Regards
IgorMi
Igor Micev,My blog: www.igormicev.com
October 17, 2012 at 1:37 pm
beebkhan (10/17/2012)
you need to find the table that is referencing the data in your rolemaster table and remove it before you remove it. otherwhilst you will have a lot of problems on your hands further down the line. You need to follow Atomicity rules ie remove all associated data before proceeding to truncate the table.
Removing the data won't help if there is a foreign key reference. A truncate will still fail. You either have to remove the foreign key constraint or issue a delete as Gail said in her post.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 17, 2012 at 1:38 pm
kapil_kk (10/17/2012)
truncate is not working..Gives error-
Cannot truncate table 'rolemaster' because it is being referenced by a FOREIGN KEY constraint.
Did you read Gail's post?
Pay close attention to the second part.
or, if it has foreign keys
DELETE FROM RoleMaster
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 18, 2012 at 12:17 am
Hi,
delete from Rolemaster is working and all data gets deleted
But I want that if I disable the constraint of the referencing table and then disable the constraint of RoleMaster then also truncate not working, I think after disabling constraint of both tables truncate should work..
_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
October 18, 2012 at 1:37 am
to the reply from igor to my post earlier. I didnt say delete the table. I was talking about removing the referenced data e.g
Table A
TableAID int PRIMARY Key IDENTITY(1,1),
data varchar(100)
Table B
TableBID INT PRIMARY Key,
TableAID INT FOREIGN KEY CONSTRAINT,
TableCID INT FOREIGN KEY CONSTRAINT
Table C
TableCID int PRIMARY Key IDENTITY(1,1),
Data Varchar(100)
In the past I've removed linked data that is referenced within TableB and removed them from TableB using deletes before then removing them from TableA or in my case there was more than one place with referenced foreign key data removing it before deleting from the main table did the job. especially as I was using Ids using Identity e.g ID = 1, and referencing Table B using this as a foreignKey.
I was expecting kapils example to be the same as above or similar in which case my advise was to also remove data from any table referencing data as best practice.
of course if data is not being referenced then fine go ahead use the delete and remove data from TableA. I've never had to remove constraints, seems pointless then having to recreate them.
If I misunderstood no worries. Looks like kapils resolved.
October 18, 2012 at 1:49 am
beebkhan (10/18/2012)
to the reply from igor to my post earlier. I didnt say delete the table. I was talking about removing the referenced data e.gTable A
TableAID int PRIMARY Key IDENTITY(1,1),
data varchar(100)
Table B
TableBID INT PRIMARY Key,
TableAID INT FOREIGN KEY CONSTRAINT,
TableCID INT FOREIGN KEY CONSTRAINT
Table C
TableCID int PRIMARY Key IDENTITY(1,1),
Data Varchar(100)
In the past I've removed linked data that is referenced within TableB and removed them from TableB using deletes before then removing them from TableA or in my case there was more than one place with referenced foreign key data removing it before deleting from the main table did the job. especially as I was using Ids using Identity e.g ID = 1, and referencing Table B using this as a foreignKey.
I was expecting kapils example to be the same as above or similar in which case my advise was to also remove data from any table referencing data as best practice.
of course if data is not being referenced then fine go ahead use the delete and remove data from TableA. I've never had to remove constraints, seems pointless then having to recreate them.
If I misunderstood no worries. Looks like kapils resolved.
Hi,
Nice to reply.
I thought even not to remove the data as well as not to delete the table. You can retain the data with a default or nullable key for the FK using the ON DELETE adds to the FK definition.
Thanks
IgorMi
Igor Micev,My blog: www.igormicev.com
October 18, 2012 at 2:00 am
kapil_kk (10/18/2012)
I think after disabling constraint of both tables truncate should work..
No, it shouldn't. Disabled constraint is still a constraint. For a truncate the table must not be referenced by a foreign key constraint at all.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 18, 2012 at 4:05 am
thanks for the info Gila
_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
October 18, 2012 at 4:06 am
thanks beeb...
I am able to migrate the data after deleting data through delete statement rather than using Truncate statement
_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply