SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


truncate table


truncate table

Author
Message
kapil_kk
kapil_kk
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3079 Visits: 2766
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/
GilaMonster
GilaMonster
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87255 Visits: 45272
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

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


rajaster
rajaster
Valued Member
Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)

Group: General Forum Members
Points: 52 Visits: 351
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.
kapil_kk
kapil_kk
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3079 Visits: 2766
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/
kapil_kk
kapil_kk
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3079 Visits: 2766
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/
rajaster
rajaster
Valued Member
Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)

Group: General Forum Members
Points: 52 Visits: 351
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.
Igor Micev
Igor Micev
SSCertifiable
SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)

Group: General Forum Members
Points: 5900 Visits: 5080
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
My blog: www.igormicev.com
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26085 Visits: 17536
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.

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)
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26085 Visits: 17536
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.

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)
kapil_kk
kapil_kk
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3079 Visits: 2766
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/
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search