Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

truncate table Expand / Collapse
Author
Message
Posted Wednesday, October 17, 2012 8:39 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 3:27 AM
Points: 1,893, Visits: 2,329
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/
Post #1373849
Posted Wednesday, October 17, 2012 8:42 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:21 AM
Points: 39,877, Visits: 36,221
TRUNCATE TABLE RoleMaster

or, if it has foreign keys

DELETE FROM RoleMaster



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1373853
Posted Wednesday, October 17, 2012 8:44 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, September 16, 2014 2:12 AM
Points: 30, Visits: 299
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.
Post #1373857
Posted Wednesday, October 17, 2012 9:20 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 3:27 AM
Points: 1,893, Visits: 2,329
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/
Post #1373889
Posted Wednesday, October 17, 2012 9:21 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 3:27 AM
Points: 1,893, Visits: 2,329
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/
Post #1373890
Posted Wednesday, October 17, 2012 9:27 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, September 16, 2014 2:12 AM
Points: 30, Visits: 299
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.
Post #1373901
Posted Wednesday, October 17, 2012 1:25 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 6:19 AM
Points: 2,963, Visits: 2,982
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,
SQL Server developer at Seavus
www.seavus.com
Post #1374018
Posted Wednesday, October 17, 2012 1:37 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, September 19, 2014 2:34 PM
Points: 12,923, Visits: 12,342
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1374026
Posted Wednesday, October 17, 2012 1:38 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, September 19, 2014 2:34 PM
Points: 12,923, Visits: 12,342
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1374027
Posted Thursday, October 18, 2012 12:17 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 3:27 AM
Points: 1,893, Visits: 2,329
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/
Post #1374158
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse