By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
create table [t 1] (
id int
)
insert into [t 1] values (1), (2)
2 rows affected
create procedure sp_trunc(
@TrunTableSchema VARCHAR(100),
@TrunTableName VARCHAR(254)
)
as
begin
declare @sql nvarchar(1000);
select
@sql = 'truncate table [' + table_schema + '].['
+ table_name + ']'
from information_schema.tables
where table_type = 'BASE TABLE'
and table_schema = @TrunTableSchema
and table_name = @TrunTableName
;
if @sql is null
return; --handle not exist
else
EXEC sp_EXECUTESQL @sql;
end;
exec sp_trunc
@TrunTableSchema = 'dbo',
@TrunTableName = 't 1'
select *
from [t 1]
id |
---|