tags:

views:

15

answers:

2

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?

+1  A: 

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.

Konamiman
How to run inside CTE and then list all values?
Mario
+1  A: 

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
astander