Solved

sql query to create new records from 0001 to 9999

Posted on 2014-03-28
5
1,528 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
  • 3
5 Comments
 
LVL 142

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 142

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 142

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:
ScottPletcher 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now