Truncate multiple tables in SQL Server using stored procedure

14,803

Solution 1

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;

Solution 2

You are missing BEGIN and END and semicolons:

CREATE PROCEDURE truncate_tables AS
BEGIN
      truncate table dbo.table1;
      truncate table dbo.table2;
      truncate table dbo.table3;
END;
Share:
14,803
taji01
Author by

taji01

Updated on June 22, 2022

Comments

  • taji01
    taji01 almost 2 years

    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
    
  • taji01
    taji01 almost 8 years
    around my truncate_tables() in the ) section i get a red squiggly line saying that. syntax error: Expecting Variable. What are they talking about?
  • Gordon Linoff
    Gordon Linoff almost 8 years
    @taji01 . . . Also, the empty parentheses are not needed.