212 lines
8.4 KiB
Transact-SQL
212 lines
8.4 KiB
Transact-SQL
-- ============================================================================
|
|
-- HARD DELETE ALL COMPANY DATA - Demo Company Only
|
|
-- WARNING: This permanently deletes all data for the specified company!
|
|
-- ============================================================================
|
|
|
|
SET NOCOUNT ON;
|
|
DECLARE @CompanyId INT = 1; -- Demo Company (change if needed)
|
|
DECLARE @CompanyName NVARCHAR(255);
|
|
DECLARE @RowsDeleted INT = 0;
|
|
|
|
-- Get company name for confirmation
|
|
SELECT @CompanyName = CompanyName FROM Companies WHERE Id = @CompanyId;
|
|
|
|
PRINT '============================================================================';
|
|
PRINT 'HARD DELETE - Company: ' + ISNULL(@CompanyName, 'NOT FOUND');
|
|
PRINT 'Company ID: ' + CAST(@CompanyId AS NVARCHAR(10));
|
|
PRINT 'Started at: ' + CONVERT(NVARCHAR(30), GETDATE(), 120);
|
|
PRINT '============================================================================';
|
|
PRINT '';
|
|
|
|
IF @CompanyName IS NULL
|
|
BEGIN
|
|
PRINT 'ERROR: Company not found!';
|
|
RETURN;
|
|
END
|
|
|
|
BEGIN TRANSACTION;
|
|
|
|
BEGIN TRY
|
|
-- ========================================================================
|
|
-- LEVEL 1: Most dependent tables (delete first)
|
|
-- ========================================================================
|
|
|
|
PRINT 'Deleting Level 1: Most dependent tables...';
|
|
|
|
-- JobStatusHistory (references Jobs)
|
|
DELETE FROM JobStatusHistory WHERE JobId IN (SELECT Id FROM Jobs WHERE CompanyId = @CompanyId);
|
|
SET @RowsDeleted = @@ROWCOUNT;
|
|
PRINT ' ✓ JobStatusHistory: ' + CAST(@RowsDeleted AS NVARCHAR(10)) + ' rows';
|
|
|
|
-- JobPhotos (references Jobs)
|
|
DELETE FROM JobPhotos WHERE JobId IN (SELECT Id FROM Jobs WHERE CompanyId = @CompanyId);
|
|
SET @RowsDeleted = @@ROWCOUNT;
|
|
PRINT ' ✓ JobPhotos: ' + CAST(@RowsDeleted AS NVARCHAR(10)) + ' rows';
|
|
|
|
-- JobNotes (references Jobs)
|
|
DELETE FROM JobNotes WHERE JobId IN (SELECT Id FROM Jobs WHERE CompanyId = @CompanyId);
|
|
SET @RowsDeleted = @@ROWCOUNT;
|
|
PRINT ' ✓ JobNotes: ' + CAST(@RowsDeleted AS NVARCHAR(10)) + ' rows';
|
|
|
|
-- Appointments (references Jobs, Customers)
|
|
DELETE FROM Appointments WHERE CompanyId = @CompanyId;
|
|
SET @RowsDeleted = @@ROWCOUNT;
|
|
PRINT ' ✓ Appointments: ' + CAST(@RowsDeleted AS NVARCHAR(10)) + ' rows';
|
|
|
|
-- JobItems (references Jobs)
|
|
DELETE FROM JobItems WHERE JobId IN (SELECT Id FROM Jobs WHERE CompanyId = @CompanyId);
|
|
SET @RowsDeleted = @@ROWCOUNT;
|
|
PRINT ' ✓ JobItems: ' + CAST(@RowsDeleted AS NVARCHAR(10)) + ' rows';
|
|
|
|
-- QuoteItems (references Quotes)
|
|
DELETE FROM QuoteItems WHERE QuoteId IN (SELECT Id FROM Quotes WHERE CompanyId = @CompanyId);
|
|
SET @RowsDeleted = @@ROWCOUNT;
|
|
PRINT ' ✓ QuoteItems: ' + CAST(@RowsDeleted AS NVARCHAR(10)) + ' rows';
|
|
|
|
-- InventoryTransactions (references InventoryItems)
|
|
DELETE FROM InventoryTransactions WHERE InventoryItemId IN (SELECT Id FROM InventoryItems WHERE CompanyId = @CompanyId);
|
|
SET @RowsDeleted = @@ROWCOUNT;
|
|
PRINT ' ✓ InventoryTransactions: ' + CAST(@RowsDeleted AS NVARCHAR(10)) + ' rows';
|
|
|
|
-- MaintenanceRecords (references Equipment)
|
|
DELETE FROM MaintenanceRecords WHERE EquipmentId IN (SELECT Id FROM Equipment WHERE CompanyId = @CompanyId);
|
|
SET @RowsDeleted = @@ROWCOUNT;
|
|
PRINT ' ✓ MaintenanceRecords: ' + CAST(@RowsDeleted AS NVARCHAR(10)) + ' rows';
|
|
|
|
PRINT '';
|
|
|
|
-- ========================================================================
|
|
-- LEVEL 2: Mid-level dependencies
|
|
-- ========================================================================
|
|
|
|
PRINT 'Deleting Level 2: Mid-level dependencies...';
|
|
|
|
-- Jobs (references Customers, Quotes)
|
|
DELETE FROM Jobs WHERE CompanyId = @CompanyId;
|
|
SET @RowsDeleted = @@ROWCOUNT;
|
|
PRINT ' ✓ Jobs: ' + CAST(@RowsDeleted AS NVARCHAR(10)) + ' rows';
|
|
|
|
-- Quotes (references Customers)
|
|
DELETE FROM Quotes WHERE CompanyId = @CompanyId;
|
|
SET @RowsDeleted = @@ROWCOUNT;
|
|
PRINT ' ✓ Quotes: ' + CAST(@RowsDeleted AS NVARCHAR(10)) + ' rows';
|
|
|
|
-- CatalogItems (references CatalogCategories, Customers)
|
|
DELETE FROM CatalogItems WHERE CompanyId = @CompanyId;
|
|
SET @RowsDeleted = @@ROWCOUNT;
|
|
PRINT ' ✓ CatalogItems: ' + CAST(@RowsDeleted AS NVARCHAR(10)) + ' rows';
|
|
|
|
PRINT '';
|
|
|
|
-- ========================================================================
|
|
-- LEVEL 3: Base entities
|
|
-- ========================================================================
|
|
|
|
PRINT 'Deleting Level 3: Base entities...';
|
|
|
|
-- Customers
|
|
DELETE FROM Customers WHERE CompanyId = @CompanyId;
|
|
SET @RowsDeleted = @@ROWCOUNT;
|
|
PRINT ' ✓ Customers: ' + CAST(@RowsDeleted AS NVARCHAR(10)) + ' rows';
|
|
|
|
-- InventoryItems
|
|
DELETE FROM InventoryItems WHERE CompanyId = @CompanyId;
|
|
SET @RowsDeleted = @@ROWCOUNT;
|
|
PRINT ' ✓ InventoryItems: ' + CAST(@RowsDeleted AS NVARCHAR(10)) + ' rows';
|
|
|
|
-- Equipment
|
|
DELETE FROM Equipment WHERE CompanyId = @CompanyId;
|
|
SET @RowsDeleted = @@ROWCOUNT;
|
|
PRINT ' ✓ Equipment: ' + CAST(@RowsDeleted AS NVARCHAR(10)) + ' rows';
|
|
|
|
-- CatalogCategories
|
|
DELETE FROM CatalogCategories WHERE CompanyId = @CompanyId;
|
|
SET @RowsDeleted = @@ROWCOUNT;
|
|
PRINT ' ✓ CatalogCategories: ' + CAST(@RowsDeleted AS NVARCHAR(10)) + ' rows';
|
|
|
|
-- ShopWorkers
|
|
DELETE FROM ShopWorkers WHERE CompanyId = @CompanyId;
|
|
SET @RowsDeleted = @@ROWCOUNT;
|
|
PRINT ' ✓ ShopWorkers: ' + CAST(@RowsDeleted AS NVARCHAR(10)) + ' rows';
|
|
|
|
-- PricingTiers
|
|
DELETE FROM PricingTiers WHERE CompanyId = @CompanyId;
|
|
SET @RowsDeleted = @@ROWCOUNT;
|
|
PRINT ' ✓ PricingTiers: ' + CAST(@RowsDeleted AS NVARCHAR(10)) + ' rows';
|
|
|
|
-- CompanyOperatingCosts
|
|
DELETE FROM CompanyOperatingCosts WHERE CompanyId = @CompanyId;
|
|
SET @RowsDeleted = @@ROWCOUNT;
|
|
PRINT ' ✓ CompanyOperatingCosts: ' + CAST(@RowsDeleted AS NVARCHAR(10)) + ' rows';
|
|
|
|
-- Suppliers
|
|
DELETE FROM Suppliers WHERE CompanyId = @CompanyId;
|
|
SET @RowsDeleted = @@ROWCOUNT;
|
|
PRINT ' ✓ Suppliers: ' + CAST(@RowsDeleted AS NVARCHAR(10)) + ' rows';
|
|
|
|
PRINT '';
|
|
|
|
-- ========================================================================
|
|
-- LEVEL 4: Lookup tables (delete last)
|
|
-- ========================================================================
|
|
|
|
PRINT 'Deleting Level 4: Lookup tables...';
|
|
|
|
-- JobStatusLookup
|
|
DELETE FROM JobStatusLookup WHERE CompanyId = @CompanyId;
|
|
SET @RowsDeleted = @@ROWCOUNT;
|
|
PRINT ' ✓ JobStatusLookup: ' + CAST(@RowsDeleted AS NVARCHAR(10)) + ' rows';
|
|
|
|
-- JobPriorityLookup
|
|
DELETE FROM JobPriorityLookup WHERE CompanyId = @CompanyId;
|
|
SET @RowsDeleted = @@ROWCOUNT;
|
|
PRINT ' ✓ JobPriorityLookup: ' + CAST(@RowsDeleted AS NVARCHAR(10)) + ' rows';
|
|
|
|
-- QuoteStatusLookup
|
|
DELETE FROM QuoteStatusLookup WHERE CompanyId = @CompanyId;
|
|
SET @RowsDeleted = @@ROWCOUNT;
|
|
PRINT ' ✓ QuoteStatusLookup: ' + CAST(@RowsDeleted AS NVARCHAR(10)) + ' rows';
|
|
|
|
-- AppointmentStatusLookup
|
|
DELETE FROM AppointmentStatusLookup WHERE CompanyId = @CompanyId;
|
|
SET @RowsDeleted = @@ROWCOUNT;
|
|
PRINT ' ✓ AppointmentStatusLookup: ' + CAST(@RowsDeleted AS NVARCHAR(10)) + ' rows';
|
|
|
|
-- AppointmentTypeLookup
|
|
DELETE FROM AppointmentTypeLookup WHERE CompanyId = @CompanyId;
|
|
SET @RowsDeleted = @@ROWCOUNT;
|
|
PRINT ' ✓ AppointmentTypeLookup: ' + CAST(@RowsDeleted AS NVARCHAR(10)) + ' rows';
|
|
|
|
-- InventoryCategoryLookup
|
|
DELETE FROM InventoryCategoryLookup WHERE CompanyId = @CompanyId;
|
|
SET @RowsDeleted = @@ROWCOUNT;
|
|
PRINT ' ✓ InventoryCategoryLookup: ' + CAST(@RowsDeleted AS NVARCHAR(10)) + ' rows';
|
|
|
|
PRINT '';
|
|
PRINT '============================================================================';
|
|
PRINT 'SUCCESS! All company data deleted.';
|
|
PRINT 'Completed at: ' + CONVERT(NVARCHAR(30), GETDATE(), 120);
|
|
PRINT '============================================================================';
|
|
|
|
COMMIT TRANSACTION;
|
|
PRINT '';
|
|
PRINT 'Transaction committed successfully.';
|
|
|
|
END TRY
|
|
BEGIN CATCH
|
|
ROLLBACK TRANSACTION;
|
|
|
|
PRINT '';
|
|
PRINT '============================================================================';
|
|
PRINT 'ERROR! Rolling back transaction...';
|
|
PRINT '';
|
|
PRINT 'Error Message: ' + ERROR_MESSAGE();
|
|
PRINT 'Error Line: ' + CAST(ERROR_LINE() AS NVARCHAR(10));
|
|
PRINT '============================================================================';
|
|
|
|
-- Re-throw the error
|
|
THROW;
|
|
END CATCH;
|
|
|
|
SET NOCOUNT OFF;
|