Tuesday, June 11, 2013

SQL Puzzle #3 - How to delete all the records from the entire table in a database using single line query?

How to delete all the records from the entire table in a database using single line query?


Ans : sp_MSForEachTable "Delete ?"

Try it another method;

DECLARE @str VARCHAR(MAX);
SELECT @str =
(
SUBSTRING
(
(SELECT 'DELETE FROM ' + TABLE_NAME + ';' FROM INFORMATION_SCHEMA.TABLES
FOR XML PATH('')),1,99999
)
)
EXEC (@str)

No comments:

Post a Comment