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:
sequen开发者_运维问答ce
-----------
53
(1 row(s) affected)
How to print in order like this:
34
35 36 37Can help me with CTE example for this?
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
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.
精彩评论