Mario
Mario

Reputation:

Looping and printing without messages

I using this loop to print number

DECLARE @A Int
SET @A = 33
WHILE @A < 55
BEGIN
SELECT @A as sequence
SET @A = @A + 1
END
GO

But problem that with every loop message is printed like example:

sequence
-----------
53

(1 row(s) affected)

How to print in order like this:

34
35
36
37

Can help me with CTE example for this?

Upvotes: 0

Views: 46

Answers (2)

Adriaan Stander
Adriaan Stander

Reputation: 166476

Use PRINT

DECLARE @A INT
SET @A = 33
WHILE @A < 55
BEGIN
PRINT @A
SET @A = @A + 1
END
GO

For the CTE you can try

DECLARE @A INT,
        @End INT
SET @A = 33
SET @End = 55

;WITH Selected AS (
        SELECT  @A Val
        UNION ALL
        SELECT  Val + 1
        FROM    Selected
        WHERE   Val < @End
)
SELECT *
FROM Selected

Upvotes: 1

Konamiman
Konamiman

Reputation: 50293

If all you want is to print the value, you can use the PRINT statement. If you want to actually return the result (if your code is part of a stored procedure, for example), you could define a temporary table type variable, insert data on it on each loop, then return the contents of the table.

Upvotes: 1

Related Questions