Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2008
»
SQL Server 2008 - General
»
truncate table
15 posts, Page 1 of 2
1
2
»»
truncate table
Rate Topic
Display Mode
Topic Options
Author
Message
kapil_kk
kapil_kk
Posted Wednesday, October 17, 2012 8:39 AM
Ten Centuries
Group: General Forum Members
Last Login: 2 days ago @ 11:16 PM
Points: 1,061,
Visits: 1,151
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..
Post #1373849
GilaMonster
GilaMonster
Posted Wednesday, October 17, 2012 8:42 AM
SSC-Dedicated
Group: General Forum Members
Last Login: Today @ 11:21 AM
Points: 37,686,
Visits: 29,943
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
rajaster
rajaster
Posted Wednesday, October 17, 2012 8:44 AM
Grasshopper
Group: General Forum Members
Last Login: Friday, February 22, 2013 8:21 AM
Points: 16,
Visits: 191
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
kapil_kk
kapil_kk
Posted Wednesday, October 17, 2012 9:20 AM
Ten Centuries
Group: General Forum Members
Last Login: 2 days ago @ 11:16 PM
Points: 1,061,
Visits: 1,151
truncate is not working..
Gives error-
Cannot truncate table 'rolemaster' because it is being referenced by a FOREIGN KEY constraint.
Post #1373889
kapil_kk
kapil_kk
Posted Wednesday, October 17, 2012 9:21 AM
Ten Centuries
Group: General Forum Members
Last Login: 2 days ago @ 11:16 PM
Points: 1,061,
Visits: 1,151
i tried by disabling the constraint on table roles master but after doing that also it didnt worked
Post #1373890
rajaster
rajaster
Posted Wednesday, October 17, 2012 9:27 AM
Grasshopper
Group: General Forum Members
Last Login: Friday, February 22, 2013 8:21 AM
Points: 16,
Visits: 191
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
IgorMi
IgorMi
Posted Wednesday, October 17, 2012 1:25 PM
SSCommitted
Group: General Forum Members
Last Login: Today @ 9:55 AM
Points: 1,877,
Visits: 1,445
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
Post #1374018
Sean Lange
Sean Lange
Posted Wednesday, October 17, 2012 1:37 PM
SSCrazy Eights
Group: General Forum Members
Last Login: Today @ 11:50 AM
Points: 8,585,
Visits: 8,227
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
Post #1374026
Sean Lange
Sean Lange
Posted Wednesday, October 17, 2012 1:38 PM
SSCrazy Eights
Group: General Forum Members
Last Login: Today @ 11:50 AM
Points: 8,585,
Visits: 8,227
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
Post #1374027
kapil_kk
kapil_kk
Posted Thursday, October 18, 2012 12:17 AM
Ten Centuries
Group: General Forum Members
Last Login: 2 days ago @ 11:16 PM
Points: 1,061,
Visits: 1,151
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..
Post #1374158
« Prev Topic
|
Next Topic »
15 posts, Page 1 of 2
1
2
»»
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.