To generate a sequence see
Generating a Sequence in SQL[
^]
For the first column [Book No] use an identity column:
CREATE TABLE Book
(
[Book No] int identity(1,1),
[Receipt No] varchar(9)
)
For the second column use the means of generating a sequence, format it (this is poor practice by the way) and insert into the table like this
;WITH q AS
(
SELECT 1 AS num
UNION ALL
SELECT num + 50
FROM q
WHERE num < 9000 - 49
)
INSERT INTO Book
SELECT FORMAT(num,'000#') + '-' + FORMAT(num + 49, '000#')
FROM q
OPTION (MAXRECURSION 180)
Note if you are using a version of SQL earlier than 2012 you will need to swap out the last SELECT for the one in the comment.
[Edited to ensure max number is 9000 not 9049]
[EDIT 2] OP has restated their requirements
CREATE TABLE Book
(
[Book No] int,
[Receipt No] int
)
;WITH q AS
(
SELECT 1 AS num
UNION ALL
SELECT num + 1
FROM q
WHERE num < 9000
), q2 as
(
SELECT 1 as num2
UNION ALL
SELECT num2 + 1
FROM q2
WHERE num2 < 50
)
INSERT INTO Book
SELECT Q.num, Q2.num2
FROM q CROSS JOIN q2
OPTION (MAXRECURSION 9000)
SELECT * from Book
order by [Book No], [Receipt No]
[EDIT 3] ... a slightly better description of the requirements has come forth:
;WITH q AS
(
SELECT 1 AS num
UNION ALL
SELECT num + 1
FROM q
WHERE num < 9000
)
SELECT num, FORMAT(NTILE(9000/50) OVER(ORDER BY num),'000#') AS Receipt
FROM q
ORDER BY num
OPTION (MAXRECURSION 9000)