taji01 taji01 - 6 months ago 58
SQL Question

Truncate multiple tables in SQL Server using stored procedure

I'm using SQL Server. I want to create a stored procedure that truncates 3 of my tables (

dbo.table1
,
dbo.table2
,
dbo.table3
).

I want to clear out all of my tables this way. What am I missing?

CREATE PROCEDURE truncate_tables()
AS
truncate table dbo.table1
truncate table dbo.table2
truncate table dbo.table3

Answer

Remove () next to the stored procedure name. () required if you are passing any parameter to the SP.

Add ; in end of each TRUNCATE TABLE statement as query terminator.

CREATE PROCEDURE truncate_tables
AS
    TRUNCATE TABLE dbo.table1;
    TRUNCATE TABLE dbo.table2;
    TRUNCATE TABLE dbo.table3;
Comments