For example:
111111111
010101010
CodePudding user response:
The CREATE TABLE # T (NUM VARCHAR (5))
INSERT INTO # T
SELECT '1' UNION ALL
SELECT '0'
DECLARE @ I INT
DECLARE @ SQL1 VARCHAR (8000)
DECLARE @ SQL2 VARCHAR (8000)
The SET @ I=9
WHILE @ I> 1
The BEGIN
The SET @ SQL1=ISNULL (@ SQL1, ' ') + '+ T + CAST (@ I AS VARCHAR) +' NUM '
SET @ SQL2=ISNULL (@ SQL2, ' ') + 'JOIN # T AS T' + CAST (@ I AS VARCHAR) + 'ON 1=1'
The SET @ I=@ I - 1
END
SELECT the SET @ SQL1='# T.N UM +' + @ SQL1 + 'FROM # T + @ SQL2
The EXEC (@ SQL1)