Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 931
taji01 Avatar asked Apr 21 '26 07:04

taji01


2 Answers

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;
like image 79
Arulkumar Avatar answered Apr 23 '26 21:04

Arulkumar


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;
like image 43
Gordon Linoff Avatar answered Apr 23 '26 21:04

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!