Solved

sql query to create new records from 0001 to 9999

Posted on 2014-03-28
5
1,706 Views
Last Modified: 2014-04-07
As the title suggests, i need to create a new table call CardPool

In this table i need the following structure

CardNumber (4 digits unique number) clustered
ActiveStatus (bit) default 0

then in the table i need to have the rows from 0001 to 9999
i,e,
0001
0002
0003
...
9998
9999

Does anyone know the correct script to generate this?
0
Comment
Question by:websss
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39961155
there are other ways to do ...
;with data as (select 1 n union all select n + 1 from data where n < 9999 )
select right('000' + cast(n as varchar(10)), 4) from data
option (maxrecursion 10000)

Open in new window

;with digits as (select '0' n 
union all select '1'
union all select '2'
union all select '3'
union all select '4'
union all select '5'
union all select '6'
union all select '7'
union all select '8'
union all select '9'
)
select d1.n + d2.n + d3.n + d4.n
 from digits d1, digits d2, digits d3, digits d4
where d1.n + d2.n + d3.n + d4.n <> '0000'
order by 1

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39961156
of course, the above is only the SELECT part, which you can use to actually INSERT into your table
0
 

Author Comment

by:websss
ID: 39961268
Thanks,
I'm not sure how this works as i cannot see inserting into the CardNumber column?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39961785
the syntax, from above, would be in general:
;WITH alias AS ( <some query> )
INSERT INTO yourtable ( <columns> )
SELECT <columns>
  FROM alias

so, taking my first query sample:
;with data as (select 1 n union all select n + 1 from data where n < 9999 )
INSERT INTO CardPool ( CardNumber ) 
select right('000' + cast(n as varchar(10)), 4) 
from data
option (maxrecursion 10000) 

Open in new window

0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 39962624
Recursion is more resource-intensive than an inline CROSS JOIN.  A sequential list of numbers is usually called a "tally" table, so I retain that naming for the CTE:

;WITH
cteDigits AS (
    SELECT 0 AS digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL
    SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
),
cteTally AS (
    SELECT [1000s].digit * 1000 + [100s].digit * 100 + [10s].digit * 10 + [1s].digit AS tally
    FROM cteDigits [1s]
    CROSS JOIN cteDigits [10s]
    CROSS JOIN cteDigits [100s]
    CROSS JOIN cteDigits [1000s]
    WHERE
        [1000s].digit * 1000 + [100s].digit * 100 + [10s].digit * 10 + [1s].digit > 0
)
INSERT INTO dbo.CardPool ( CardNumber, ActiveStatus )
SELECT t.tally, 0
FROM cteTally t
ORDER BY
    t.tally
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question