prevent sql server 2005 from printing on console - sql-server

How can I disable the verbose output that gets printed on the console every time I run large queries in SQL Server 2005 Management Studio...
It keeps saying..
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
and keeps doing that.
I have some print statement in between the SQL, they all get lost! How can I turn this thing off ?

Use this statement on start of batch:
SET NOCOUNT ON;
There is Documentation on MSDN.

You need to write one statement prior your T-SQL.
SET NOCOUNT ON

Related

SET NOCOUNT OFF disregarded in SQLCMD

I want to see how many rows are affected for each DDL statement that is run by a query, so I set SET NOCOUNT OFF at the start of each query that is run.
Sample query:
SET NOCOUNT OFF;
GO
BEGIN TRY
BEGIN TRANSACTION
UPDATE dbo.tbProvClause SET ClauseTemplate = 'Clause1' where DocumentName = '\Templates\EndorsAccessPlainLanguageQCEng.CDS';
UPDATE dbo.tbProvClause SET ClauseTemplate = 'Clause 2' where DocumentName = '\Templates\EndorsEnforcedRemovallLtdMktPublicPropertyQCEng.CDS';
UPDATE dbo.tbProvClause SET ClauseTemplateFR = 'Malgré French Clause 1' where DocumentNameFR = '\Templates\EndorsAccessHOPPQcFr.CDS';
UPDATE dbo.tbProvClause SET ClauseTemplateFR = 'Malgré les exceptions Clause 2' where DocumentNameFR = '\Templates\EndorsEnlèvementFTNdomainepublicERLMPublicPropertyQcFr.CDS';
COMMIT TRAN
PRINT 'Script Completed With Success - Changes committed on ' + CAST(current_timestamp AS varchar(25))
END TRY
BEGIN CATCH
--
END CATCH
GO
and it returns
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
Script Completed With Success - Changes committed on Nov 29 2017 12:10PM
This is good. But when I run the same in SQLCMD, I get only 1 row .i.e.
sqlcmd -S testserver -dTestDB -i StackOverflowSQL.sql
(1 rows affected)
Script Completed With Success - Changes committed on Nov 29 2017 12:24PM
How do I retain the ability of the SET NOCOUNT OFF in SQLCMD? The reason I asked this question is that I have a number of scripts that I want to batch using SQLCMD and I will be saving their logs. In this case, the SET NOCOUNT OFF is very useful in checking how many lines of 1 rows affected will give a feedback that the run was successful.
Try something like this and see if it works.
use -v (small letter v).
sqlcmd -v NOCOUNT=OFF -S testserver -dTestDB -i StackOverflowSQL.sql
Or
In the same command prompt first run SET NOCOUNT=OFF before calling sqlcmd .
Look into below documentation link and search for "Variable Precedence". You will get some idea.
https://learn.microsoft.com/en-us/sql/tools/sqlcmd-utility
It's just a hunch. Remove the Go statement after "set nocount off".
First of all "set nocount off" statement does not require a GO. Secondly, I think GO (A batch executor) may be setting the option only for that batch.
There is a rather stupid workaround if my suggestion does not work.
You can:
print ##rowcount
after every sql statement which you may be interested to monitor the row counts.
Found the problem. There were multiple versions of SQLCMD installed in the machine. To find out which version I was using:
E:\Test>where sqlcmd
C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE
C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\SQLCMD.EXE
The one that I was using was SQL Server 2008 R2 version. the I looked into the system environment variables PATH and changed the order and now it uses the SQL server 2012 version. After changing the PATH
E:\Test>where sqlcmd
C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\SQLCMD.EXE
C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE
E:\Test>sqlcmd -S testserver -dTestdb -i StackOverflowSQL.sql
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
Script Completed With Success - Changes committed on Nov 29 2017 3:37PM
It works!

Sql server 2008 cache Memory

I am trying to validate the performance issues in my project I found some thing tricky
DECLARE #T DATETIME2
DBCC DROPCLEANBUFFERS
SET #T =GETDATE()
SELECT SUM(cost)
from tblLog
WHERE logID <=100000
PRINT cast(DATEDIFF(ms,#T,GETDATE()) as NVARCHAR(255))+' Milli seconds'
DBCC DROPCLEANBUFFERS
SET #T =GETDATE()
SELECT SUM(cost)
from tblLog
WHERE logID <=100000
PRINT cast(DATEDIFF(ms,#T,GETDATE()) as NVARCHAR(255))+' Milli seconds'
When I am executing above code whole at once (same query twice) even though I am cleaning buffer pool memory always second execution is taking lesser time when compared with first could please explain what is missing in this case.
Sample output:
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
Warning: Null value is eliminated by an aggregate or other SET operation.
(1 row(s) affected)
(1 row(s) affected)
403 Milli seconds
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
Warning: Null value is eliminated by an aggregate or other SET operation.
(1 row(s) affected)
(1 row(s) affected)
103 Milli seconds

How to alter the SQL output messages

I have multiple INSERT / SELECT statements as part of a single SQL data import script. When I run my script I get the output
(141926 row(s) affected)
(124366 row(s) affected)
(4 row(s) affected)
(1 row(s) affected)
But what I would really want is
(141926 row(s) affected) - Customers Deleted
(124366 row(s) affected) - Customers Inserted
(4 row(s) affected) - Customers missing last name etc
(1 row(s) affected)
Is there anyway to do this in SQL??
I agree with #yorick de Wid, in that I don' think you can customise the SQL output.
The closest that I can think of in SQL Server is to "roll your own", by doing something like:
declare #recordsaffected int
<execute your SQL statement here>
set #recordsaffected = ##ROWCOUNT
print convert(varchar,#recordsaffected) + ' <your message here>'

Why there is an extra "(1 row(s) affected)"

The SSMS shows an extra (1 row(s) affected) every time when I execute insert/update. For example, execute the following SQL
declare #a table (a int)
insert into #a values (1), (2)
update #a set a = 3
And the SSMS will display the following message.
(2 row(s) affected)
(1 row(s) affected)
(2 row(s) affected)
(1 row(s) affected)
I didn't find any database/server trigger. What could cause the extra (1 row(s) affected)?
That usually means you have the actual execution plan option turned on. The execution plan is sent as an extra rowset, resulting in an extra (1 row(s) affected) message.
To disable actual execution plan press Ctrl+M.
this should not happen. try to look into the actual execution plan, what is happening there.
I found this post as I was getting the same issue but with no execution plan options turned on or triggers of any kind.
I had a very simple piece of code that would not normally have any rows affected at all:
DECLARE #dbname VARCHAR(50) = 'AdventureWorks'
If was testing some other features of SQL Server with Management Studio v18.10, the code above returned a couple of output rows that were not expected.
(0 rows affected)
(1 row affected)
Completion time: 2022-01-07T12:04:36.6414442+00:00
I my case I tracked it down to the connection settings where the Enable Always Encrypted (column encryption) option was checked / selected. I had been testing Always Encrypted, which uses encryption on parameters.
It only appears if the variable is declared and set in the same statement.
Setting the option for Enable Always Encrypted (column encryption) to unselected / unchecked fixed the reporting affected rows.
Added to help others who may find this issue.
Fitz

How to remove 'enters' from TSQL scripts executed in Microsoft SQL Server Management Studio?

I'm working with a lot of TSQL scripts that perform management tasks on the SQL Server. Those scripts are saved as .sql files and executed by a DBA in Microsoft SQL Server Management Studio. I use the print statement to echo some information back to the DBA.
Consider the following simplification of a script:
PRINT 'Update user...'
UPDATE [User] SET UserName = UserName WHERE UserName = 'Administrator'
PRINT 'Delete user...'
DELETE FROM [User] WHERE UserName = 'Nothing'
PRINT 'Update & Delete finished'
When I'm running this script I get the following output:
Update user...
(1 row(s) affected)
Delete user...
(0 row(s) affected)
Update & Delete finished
There is always an enter before the result of the query. Some of my DBA's are complaining about the readability of the output. It is especially hard to interpret the results when a cursor is used in a script.
Is there a way to get rid of the preceding enters when the result of an action is displayed?
You could SET NOCOUNT ON , get the rows affected via ##ROWCOUNT and print a custom message yourself.

Resources