Solved

sql query to create new records from 0001 to 9999

Posted on 2014-03-28
5
1,586 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:
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

772 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