how do I print out the table that I'm deleting from when I do a delete with a sql server…
- User
how do I print out the table that I'm deleting from when I do a delete with a sql server query like this: DELETE FROM tblSomething?
- Assistant
In SQL Server, you cannot directly print out the table while performing the DELETE operation. However, you can achieve this with a combination of SELECT and DELETE by using the OUTPUT clause. Here's an example:
sqlDECLARE @DeletedRows table ( Column1 INT, Column2 NVARCHAR(50) -- Add more columns as needed ) DELETE FROM tblSomething OUTPUT DELETED.Column1, DELETED.Column2 INTO @DeletedRows -- Add more columns as needed WHERE <your_condition> -- Add the condition(s) for the rows you want to delete SELECT * FROM @DeletedRows- First, declare a temporary table variable (
@DeletedRows) to store the deleted rows from thetblSomethingtable. Define it with the same column structure as thetblSomethingtable. - Use the DELETE statement with the OUTPUT clause to store the deleted rows in the
@DeletedRowstable variable. Make sure to replace<your_condition>with the appropriate condition for the rows you want to delete.
- First, declare a temporary table variable (
- User
I meant just printing out the table name and not any of the data. so I want it to print out the number of deleted rows with the table name mentioned
- Assistant
I see, if you just want to print the number of deleted rows along with the table name, you can use the following T-SQL script:
sqlDECLARE @RowCount INT DELETE FROM tblSomething OUTPUT DELETED.* INTO @DeletedRows -- Define this table variable with the same structure as your table SET @RowCount = (SELECT COUNT(*) FROM @DeletedRows) PRINT 'Deleted ' + CAST(@RowCount AS NVARCHAR(10)) + ' rows from tblSomething'This script will delete rows based on the given condition, store the deleted rows in the
@DeletedRowstable variable, and count them. Then, it will print a message showing the number of deleted rows along with the table nametblSomething.Please note that you still need to define the @DeletedRows table variable and replace
<your_condition>with the appropriate condition for the rows you want to delete. - User
is there a way to do it without using variables?
- Assistant
Yes, you can use @@ROWCOUNT system function to achieve this without using variables. @@ROWCOUNT returns the number of rows affected by the last executed statement.
sqlDELETE FROM tblSomething WHERE <your_condition> -- Add the condition(s) for the rows you want to delete PRINT 'Deleted ' + CAST(@@ROWCOUNT AS NVARCHAR(10)) + ' rows from tblSomething'Replace
<your_condition>with the appropriate condition for the rows you want to delete. After the DELETE statement, the @@ROWCOUNT function will hold the number of rows deleted, and the PRINT statement will display the message with the deleted rows count and table name. - User
What if I did this? wouldnt this be easier?
DELETE FROM tblSomething OUTPUT @@ROWCOUNT AS tblSomething WHERE <your_condition> – Add the condition(s) for the rows you want to delete
- Assistant
Unfortunately, using @@ROWCOUNT directly with the OUTPUT clause in the DELETE statement is not allowed in SQL Server. You will get a syntax error.
The OUTPUT clause is used for capturing the actual data from inserted or deleted rows, not for capturing the row count directly. So, you should use the previous example I provided with @@ROWCOUNT to accomplish your task without using variables:
sqlDELETE FROM tblSomething WHERE <your_condition> -- Add the condition(s) for the rows you want to delete PRINT 'Deleted ' + CAST(@@ROWCOUNT AS NVARCHAR(10)) + ' rows from tblSomething'This way, you can delete the desired rows and then PRINT the number of deleted rows along with the table name. Remember to replace
<your_condition>with the appropriate condition for the rows you want to delete.