Is there any way to stop the extra new line after

2019-04-24 11:04发布

问题:

This is a petty question, but is such a pain for me. Often, I will write store procedures with multiple queries in them. To make debugging easier, I write it like so:

print 'Some notes about query 1'
select * from someTable

print 'some notes about query 2'
update sometable set column = null

then I get results like this:

Some notes about query 1

(8 row(s) affected)
Some notes about query 2

(8 row(s) affected)

They way it spaces itself makes it hard to read. I'd want the results to look like this:

Some notes about query 1
(8 row(s) affected)

Some notes about query 2
(8 row(s) affected)

I know its petty, but it irks me. Anyone have any thoughts?

回答1:

Putting together the answers you get:

--Set up test data
DECLARE @someTable AS TABLE(id int identity, somevalue int)

INSERT INTO @someTable (somevalue) VALUES (1)
INSERT INTO @someTable (somevalue) VALUES (2)
INSERT INTO @someTable (somevalue) VALUES (3)
INSERT INTO @someTable (somevalue) VALUES (4)
INSERT INTO @someTable (somevalue) VALUES (5)
INSERT INTO @someTable (somevalue) VALUES (6)
INSERT INTO @someTable (somevalue) VALUES (7)
INSERT INTO @someTable (somevalue) VALUES (8)

--Here's the method.
SET NOCOUNT ON  --As kd7 said this will get rid of spaces. Along with the rowcount.

print 'Some notes about query 1' 
select * from @someTable 
print '(' + CONVERT(varchar,@@rowcount)  +' row(s) affected)'--This adds the row count back in

print '' --Formatting row to add the space you require.

print 'Some notes about query 2' 
update @sometable set somevalue = null 
print '(' + CONVERT(varchar,@@rowcount)  +' row(s) affected)'


回答2:

Setting NOCOUNT ON will remove the space, but will also remove the (8 row(s) affected), not sure if that is viable for you.



回答3:

If you need rowcounts in output - then in SSMS there is no any decision, otherwise use set nocount on

BUT

you always may write your own application to run these queries with its own output including rowcounts



标签: tsql