| Author |
Topic |
|
muek
Starting Member
16 Posts |
Posted - 2008-08-08 : 14:38:21
|
| Hi,is possible to create a query that gives me the number from 1 to 20? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-08 : 14:46:02
|
| YupSELECT numberFROM master..spt_valuesWHERE type='p'AND number BETWEEN 1 AND 20 |
 |
|
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2008-08-08 : 15:27:59
|
If you don't want your code to be dependent on system tables in the master database, you should create your own table of sequential numbers within the application databases.Or, for small sets of numbers, use this:;with NumList as (select 1 as NumVal UNION ALL select NumVal + 1 from NumList where NumVal < 20)select *from NumList e4 d5 xd5 Nf6 |
 |
|
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2008-08-08 : 15:51:39
|
For larger datasets, this is still pretty fast:;with NumList (NumVal) as (select 0 as NumVal UNION ALL select NumVal + 1 from NumList where NumVal < 9)select Ones.NumVal + Tens.NumVal * 10 + Hundreds.NumVal * 100 + Thousands.NumVal * 1000from NumList Ones, NumList Tens, NumList Hundreds, NumList Thousands e4 d5 xd5 Nf6 |
 |
|
|
muek
Starting Member
16 Posts |
Posted - 2008-08-11 : 04:25:04
|
| Thanks that all I need.I need to study "with" expression. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-11 : 04:38:31
|
SELECT 1 AS Number UNION ALLSELECT 2 UNION ALLSELECT 3 UNION ALLSELECT 4 UNION ALLSELECT 5 UNION ALLSELECT 6 UNION ALLSELECT 7 UNION ALLSELECT 8 UNION ALLSELECT 9 UNION ALLSELECT 10 UNION ALLSELECT 11 UNION ALLSELECT 12 UNION ALLSELECT 13 UNION ALLSELECT 14 UNION ALLSELECT 15 UNION ALLSELECT 16 UNION ALLSELECT 17 UNION ALLSELECT 18 UNION ALLSELECT 19 UNION ALLSELECT 20 E 12°55'05.25"N 56°04'39.16" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-11 : 05:14:15
|
quote: Originally posted by muek Thanks that all I need.I need to study "with" expression.
its called CTE (common table expression). for more details, look into books online or refer link belowhttp://code.msdn.microsoft.com/SQLExamples/Wiki/View.aspx?title=RecursiveCTE |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2008-08-11 : 10:30:27
|
quote: Originally posted by Peso SELECT 1 AS Number UNION ALLSELECT 2 UNION ALLSELECT 3 UNION ALLSELECT 4 UNION ALLSELECT 5 UNION ALLSELECT 6 UNION ALLSELECT 7 UNION ALLSELECT 8 UNION ALLSELECT 9 UNION ALLSELECT 10 UNION ALLSELECT 11 UNION ALLSELECT 12 UNION ALLSELECT 13 UNION ALLSELECT 14 UNION ALLSELECT 15 UNION ALLSELECT 16 UNION ALLSELECT 17 UNION ALLSELECT 18 UNION ALLSELECT 19 UNION ALLSELECT 20
Elegant in its simplicity.Boycott Beijing Olympics 2008 |
 |
|
|
|