Creating a recycle bin for SQL Server 2005\2008

  • Looks like this would be great for when rollback scripts aren't provided during deployment time!

  • greggoble2 (5/20/2009)


    Looks like this would be great for when rollback scripts aren't provided during deployment time!

    No kidding. And much easier to implement as well. How many times have I written those scripts (rollbacks) where they've taken longer to develop than the frickin' code or project that is getting pushed.

  • Very impressive Chris, thanks for sharing this!

    Regarding "... not naming stored procedures with the sp_ prefix ..." I have also, on rare occasion, placed administrative/maintenance stored procedures in the master database so that they can be called on user databases. However, I use two underscores, for example sp__whyamihere, to avoid collision with current or future stored procedures provided by Microsoft.

  • Good post. A very creative and useful implementation of DDL triggers.

  • Hi Chris,

    Good topic & idea. Chris, I tried to create the trigger which is shown in the article. but i am getting syntax error. could you please provide me the complete script for trigger and stored procedure as well.

    thanks

    Joseph

  • hi chris

    i appreciate for the good ideas. :unsure:

    I am also facing the same problem while implementing the triggers and the stored procedures ..please provide the provide the complete script

  • Awesome article. I am saving it! Space management is something to watch out for when this approach is implemented (but then again, we all do it anyway, right? :-))

  • This is a great idea! I have downloaded and run both SQL scripts. Now when I try to do a DROP TABLE I get this error:

    [font="Courier New"]ErrorNumber: 102

    ErrorMessage: Incorrect syntax near '-'.[/font]

    In the Messages box it says:

    [font="Courier New"]

    Caution: Changing any part of an object name could break scripts and stored procedures.

    (1 row(s) affected)

    The transaction is in an uncommittable state. Rolling back transaction.

    The object dbo.[[mytablename]] has been moved to the recycle bin

    as object zz_RecycleBin.dbo_$_[[mytablename]]_$_[[my username]]_$_2009_05_27T13_41_14_933[/font]

    However, the table has not been dropped. And SP_UNDROP says:

    [font="Courier New"]There are no objects in the recycle bin[/font]

    This is on SQL Server 2005 (9.0.4035). What could I do to try solving this?

  • Ronald,

    Initially I tried to piece together the code in the article to no avail. I downloaded the sql code shown at the end of the article, made sure that I initiated the "use , ran the code and everything works tickety-boo.

    I created the schemas direct in each database where I want to associate this utility.

    I have associated the procedure against a number of databases within my SQL SERVER structure, and everything works as documented. As well as the UNDROP code.

    When I drop a table, it falls into the recylce schema, and when I UNDROP the table, it reappears as it was before.

    Version SQL Server : 9.00.4035.00, SP3, Standard Edition

  • From the author:

    Please see: http://www.sqlservercentral.com/Forums/Topic729503-1553-1.aspx

    for my Q&As from the article.

  • Hi Chris,

    Thanks! Our domain name also has a dash in it. So I have used Chad's bugfix and now it's working great! 🙂

    Another thought: would it be possible to have all dropped tables, views and so on in a separate database (called 'RecycleBin' for example)?

    Best regards,

    Ronald

  • Hi Ronald,

    Glad you've found the scripts useful.

    Yes, I considered using a separate repository (a database) but one of my goals when designing the solution was to make it as light as possible which is why I went with the schema inside the current database solution.

    The separate database solution would also have it's challenges such as permissions on objects, FKs on renamed tables etc.

    Chris

  • Hi, I tried the scripts and it works great. However, the restored table or function name has zz_RecycleBin prefix and postfix with user name and time stamps. Needs to be renamed manually. Anyone else having this problem? or is it designed to be this way to avoid name collision in case a new table or function name being created before doing undrop?

  • Here is what it looks like in the script

    USE [AdventureWorks]

    GO

    /****** Object: StoredProcedure [zz_RecycleBin].[dbo_$_uspGetManagerEmployees_$_TOSHL35@dad_$_2009_06_07T23_55_38_100] Script Date: 06/08/2009 09:24:18 ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    ALTER PROCEDURE [zz_RecycleBin].[dbo_$_uspGetManagerEmployees_$_TOSHL35@dad_$_2009_06_07T23_55_38_100]

    @ManagerID [int]

    AS

    BEGIN

    SET NOCOUNT ON;

    -- Use recursive query to list out all Employees required for a particular Manager

    WITH [EMP_cte]([EmployeeID], [ManagerID], [FirstName], [LastName], [RecursionLevel]) -- CTE name and columns

    AS (

    SELECT e.[EmployeeID], e.[ManagerID], c.[FirstName], c.[LastName], 0 -- Get the initial list of Employees for Manager n

    FROM [HumanResources].[Employee] e

    INNER JOIN [Person].[Contact] c

    ON e.[ContactID] = c.[ContactID]

    WHERE [ManagerID] = @ManagerID

    UNION ALL

    SELECT e.[EmployeeID], e.[ManagerID], c.[FirstName], c.[LastName], [RecursionLevel] + 1 -- Join recursive member to anchor

    FROM [HumanResources].[Employee] e

    INNER JOIN [EMP_cte]

    ON e.[ManagerID] = [EMP_cte].[EmployeeID]

    INNER JOIN [Person].[Contact] c

    ON e.[ContactID] = c.[ContactID]

    )

    -- Join back to Employee to return the manager name

    SELECT [EMP_cte].[RecursionLevel], [EMP_cte].[ManagerID], c.[FirstName] AS 'ManagerFirstName', c.[LastName] AS 'ManagerLastName',

    [EMP_cte].[EmployeeID], [EMP_cte].[FirstName], [EMP_cte].[LastName] -- Outer select from the CTE

    FROM [EMP_cte]

    INNER JOIN [HumanResources].[Employee] e

    ON [EMP_cte].[ManagerID] = e.[EmployeeID]

    INNER JOIN [Person].[Contact] c

    ON e.[ContactID] = c.[ContactID]

    ORDER BY [RecursionLevel], [ManagerID], [EmployeeID]

    OPTION (MAXRECURSION 25)

    END;

  • could be this useful for recover a dropped column or only for tables and database objects?

Viewing 15 posts - 16 through 30 (of 30 total)

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