How to delete all rows from all tables in a SQL Server database?

前端 未结 11 1178
遇见更好的自我
遇见更好的自我 2020-11-29 15:12

How to delete all rows from all tables in a SQL Server database?

11条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 15:47

    I had to delete all the rows and did it with the next script:

    DECLARE @Nombre NVARCHAR(MAX);
    DECLARE curso CURSOR FAST_FORWARD 
    FOR 
    Select Object_name(object_id) AS Nombre from sys.objects where type = 'U'
    
    OPEN curso
    FETCH NEXT FROM curso INTO @Nombre
    
    WHILE (@@FETCH_STATUS <> -1)
    BEGIN
    IF (@@FETCH_STATUS <> -2)
    BEGIN
    DECLARE @statement NVARCHAR(200);
    SET @statement = 'DELETE FROM ' + @Nombre;
    print @statement
    execute sp_executesql @statement;
    END
    FETCH NEXT FROM curso INTO @Nombre
    END
    CLOSE curso
    DEALLOCATE curso
    

    Hope this helps!

提交回复
热议问题