Truncate Referenced Table

  • Comments posted to this topic are about the item Truncate Referenced Table

  • 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.

  • 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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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

  • 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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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,

    T:cool:

  • I have one more question, if two tables are refering each other how can we truncate both tables?

  • Then use following link -

    Truncate all tables reference foreign keys[/url]

    Vimal LohaniSQL 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

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply