Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / General / Other SQL Server Topics / January 2008

Tip: Looking for answers? Try searching our database.

multiple numbers in one statement

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Yo mama - 28 Jan 2008 22:15 GMT
Hi,
Is there any way to insert into table 100 integers from 1 to 100 in one
insert statement?
Now I have to use loop to insert numbers. I was wondering if there is the
simpler way.
Erland Sommarskog - 28 Jan 2008 22:39 GMT
> Is there any way to insert into table 100 integers from 1 to 100 in one
> insert statement?
> Now I have to use loop to insert numbers. I was wondering if there is the
> simpler way.

WITH numbers(n) AS
  SELECT row_number() OVER (ORDER BY object_id) FROM sys.columns
)
SELECT n FROM numbers WHERE n <= 100

The row_number function is handy for a lot of things.

If you want lots of numbers, you not have a good table to work from. Here
is a query for a million numbers:

CREATE TABLE Numbers (Number int NOT NULL PRIMARY KEY);
WITH digits (d) AS (
  SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION
  SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION
  SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION
  SELECT 0)
INSERT Numbers (Number)
  SELECT Number
  FROM   (SELECT i.d + ii.d * 10 + iii.d * 100 + iv.d * 1000 +
                 v.d * 10000 + vi.d * 100000 AS Number
          FROM   digits i
          CROSS  JOIN digits ii
          CROSS  JOIN digits iii
          CROSS  JOIN digits iv
          CROSS  JOIN digits v
          CROSS  JOIN digits vi) AS Numbers
  WHERE  Number > 0

Signature

Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

Plamen Ratchev - 29 Jan 2008 03:30 GMT
Here is a method using the system table spt_values
(http://www.sqlmag.com/Articles/ArticleID/22920/pg/2/2.html), which works
fine for small numbers table (note: the table is undocumented and assuming
unsupported). This works fine on SQL Server 2000, 2005, and the current 2008
CTP:

CREATE TABLE Numbers (nbr INT NOT NULL PRIMARY KEY)

INSERT INTO Numbers ( nbr )
SELECT number FROM master..spt_values
WHERE type = 'P'
  AND number BETWEEN 1 AND 100

HTH,

Plamen Ratchev
http://www.SQLStudio.com
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.