Learn how ReRunSQL transforms common SQL operations into idempotent, safe-to-rerun scripts with intelligent existence checks and safeguards.
When you run a CREATE TABLE statement twice, it fails with "table already exists". ReRunSQL adds an existence check to prevent this error.
CREATE TABLE Users (
Id INT PRIMARY KEY,
Name NVARCHAR(100),
Email NVARCHAR(255)
);
-- Check if table exists before creating
IF NOT EXISTS (
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'Users'
)
BEGIN
CREATE TABLE Users (
Id INT PRIMARY KEY,
Name NVARCHAR(100),
Email NVARCHAR(255)
);
END;
Adding a column that already exists causes an error. ReRunSQL checks if the column exists before attempting to add it.
ALTER TABLE Users
ADD PhoneNumber NVARCHAR(20);
-- Add column only if it doesn't exist
IF NOT EXISTS (
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Users'
AND COLUMN_NAME = 'PhoneNumber'
)
BEGIN
ALTER TABLE Users
ADD PhoneNumber NVARCHAR(20);
END;
Creating an index twice fails with "index already exists". ReRunSQL adds a check to skip creation if the index exists.
CREATE INDEX IX_Users_Email
ON Users(Email);
-- Create index only if it doesn't exist
IF NOT EXISTS (
SELECT * FROM sys.indexes
WHERE name = 'IX_Users_Email'
AND object_id = OBJECT_ID('Users')
)
BEGIN
CREATE INDEX IX_Users_Email
ON Users(Email);
END;
Running INSERT statements multiple times creates duplicate data. ReRunSQL converts them to conditional inserts or MERGE/UPSERT patterns.
INSERT INTO Users (Id, Name, Email)
VALUES (1, 'John Doe', '[email protected]');
-- Insert only if record doesn't exist
IF NOT EXISTS (SELECT 1 FROM Users WHERE Id = 1)
BEGIN
INSERT INTO Users (Id, Name, Email)
VALUES (1, 'John Doe', '[email protected]');
END;
MERGE Users AS target
USING (SELECT 1 AS Id, 'John Doe' AS Name, '[email protected]' AS Email) AS source
ON target.Id = source.Id
WHEN NOT MATCHED THEN
INSERT (Id, Name, Email)
VALUES (source.Id, source.Name, source.Email);
Dropping a table that doesn't exist causes an error. ReRunSQL adds an existence check before dropping.
DROP TABLE TempData;
-- Drop table only if it exists
IF EXISTS (
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'TempData'
)
BEGIN
DROP TABLE TempData;
END;
Dropping a column that doesn't exist causes an error. ReRunSQL checks column existence first.
ALTER TABLE Users
DROP COLUMN MiddleName;
-- Drop column only if it exists
IF EXISTS (
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Users'
AND COLUMN_NAME = 'MiddleName'
)
BEGIN
ALTER TABLE Users
DROP COLUMN MiddleName;
END;
Creating a procedure that exists fails. ReRunSQL uses DROP IF EXISTS or CREATE OR ALTER patterns.
CREATE PROCEDURE GetUserById
@UserId INT
AS
BEGIN
SELECT * FROM Users WHERE Id = @UserId;
END;
-- Create or replace procedure
CREATE OR ALTER PROCEDURE GetUserById
@UserId INT
AS
BEGIN
SELECT * FROM Users WHERE Id = @UserId;
END;
-- Drop if exists, then create
IF EXISTS (SELECT * FROM sys.objects
WHERE type = 'P' AND name = 'GetUserById')
BEGIN
DROP PROCEDURE GetUserById;
END;
GO
CREATE PROCEDURE GetUserById
@UserId INT
AS
BEGIN
SELECT * FROM Users WHERE Id = @UserId;
END;
ReRunSQL intelligently transforms the most common DDL and DML operations:
Transform your SQL scripts in seconds with intelligent existence checks and safeguards
Try ReRunSQL Now →