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


Truncate Referenced Table


Truncate Referenced Table

Author
Message
kodracon
kodracon
SSC-Enthusiastic
SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)

Group: General Forum Members
Points: 115 Visits: 162
Comments posted to this topic are about the item Truncate Referenced Table
Tim Wilson-Brown
Tim Wilson-Brown
Old Hand
Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)

Group: General Forum Members
Points: 329 Visits: 302
Is there any reason why you used NVARCHAR(128) rather than SYSNAME?

SYSNAME would make the script more portable between versions - and enforce restrictions on content.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)

Group: General Forum Members
Points: 116239 Visits: 41430
Tim.Wilson-Brown (1/19/2009)
Is there any reason why you used NVARCHAR(128) rather than SYSNAME?

SYSNAME would make the script more portable between versions - and enforce restrictions on content.


I agree that SYSNAME would be more appropriate, but SYSNAME is nothing more than a synonym for a NOT NULL NVARCHAR(128). From BOL...

sysname is a system-supplied user-defined data type that is functionally equivalent to nvarchar(128), except that it is not nullable. sysname is used to reference database object names.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Sanjay-940444
Sanjay-940444
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 Visits: 104
I got following error while compiling this x_TruncateTable proc:

Server: Msg 197, Level 15, State 1, Procedure x_TruncateTable, Line 126
EXECUTE cannot be used as a source when inserting into a table variable.

plz clarify on this.I am executing it on sql server 2000.

thx
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)

Group: General Forum Members
Points: 116239 Visits: 41430
Sanjay (2/2/2009)
I got following error while compiling this x_TruncateTable proc:

Server: Msg 197, Level 15, State 1, Procedure x_TruncateTable, Line 126
EXECUTE cannot be used as a source when inserting into a table variable.

plz clarify on this.I am executing it on sql server 2000.

thx


A little bit of simple troubleshooting (double clicking on the error message) shows that this code was never designed to work in SQL Server 2000. You will need to change the @fk table variable and all of it's references to a Temp Table in order for this to work in SQL Server 2000.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
MudLuck
MudLuck
Old Hand
Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)

Group: General Forum Members
Points: 305 Visits: 519
Like it well enough, was hopping for a SPROC that took into account dependencies across the entire database creating levels. With these levels you could say work from the branch inward. Truncating all children. then all parnet/children so on and so on until you arrived at an empty database with no keys.

On second thought I guess it would be even easier if I'm ending up with an empty db just to modify your sproc to only include the top loop that scripits out the FK to a "real" table and drops the original. Then call it with a sp_foreachtable statment.

Then truncate again using sp_foreachtable.

Than write another sproc that uses your stored FK's to recreate all the FK's because every table is empty i.e. null every constraint should create as it will validate true and you just nocked one out of the park...

I'll post my code when I finish.

Thanks,
TCool



Sanjay-940444
Sanjay-940444
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 Visits: 104
I have one more question, if two tables are refering each other how can we truncate both tables?
Vimal Lohani
Vimal Lohani
Mr or Mrs. 500
Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)

Group: General Forum Members
Points: 502 Visits: 659
Then use following link -
Truncate all tables reference foreign keys

Vimal Lohani
SQL DBA | MCP (70-461,70-462)

==============================
The greatest barrier to success is the fear of failure
** Success is a journey not a destination
**

Think before you print, SAVE TREES, Protect Mother Nature
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