1

In MySQL I can do something like this:

SELECT @row := @row + 1 as num FROM
(select 0 union all select 1) t1,
(select 0 union all select 1) t2,
(select 0 union all select 1) t3,
(select @row := 0) as t4;

The output of which is:

num
1
2
3
4
5
6
7
8

I tried to do this in sql server, but met many road blocks:

First I tried this:

SELECT * FROM
(select 0 union all select 1) t1,
(select 0 union all select 1) t2,
(select 0 union all select 1) t3;

and received:

Msg 8155, Level 16, State 2, Line 7
No column name was specified for column 1 of 't1'.
Msg 8155, Level 16, State 2, Line 8
No column name was specified for column 1 of 't2'.
Msg 8155, Level 16, State 2, Line 9
No column name was specified for column 1 of 't3'.

So I did this:

SELECT * FROM
(select 0 as f union all select 1) t1,
(select 0 as f union all select 1) t2,
(select 0 as f union all select 1) t3;
  1. Why do I have to specify a name for column 1 on the derived tables?

Next, I tried to setup a scalar, I guessed I had to do it this way:

DECLARE @row as int
SET @row = 0

I can do this:

SELECT @row = @row + 1

Which results in nothing back until I do SELECT @row which now shows a 1

I cannot do (as I was in MySQL at the start):

DECLARE @row as int
SET @row = 0

SELECT @row = @row + 1 as num FROM
(select 0 as f union all select 1) t1,
(select 0 as f union all select 1) t2,
(select 0 as f union all select 1) t3;

I get:

Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'as'.
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near 't1'.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near 't2'.
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near 't3'.
  1. How do I mimic the behavior of the first query (from MySQL) in sql server?
4

3 に答える 3

2

行番号だけが必要な場合はROW_NUMBER()、TSQLで使用できます。

SELECT ROW_NUMBER() OVER (ORDER BY t1.f) num FROM
  (SELECT 0 AS f UNION ALL SELECT 1) t1,
  (SELECT 0 AS f UNION ALL SELECT 1) t2,
  (SELECT 0 AS f UNION ALL SELECT 1) t3;

これにより、各組み合わせの行番号が返されます。ROW_NUMBER()が必要なORDER BYので、最初の便利な列で注文するだけです。

SQLFiddleはこちら

于 2012-10-11T21:04:53.477 に答える
1

意図を理解していて、SQL Server 2005以降を使用している場合は、次のことを試してください。

select row_number() over(order by t1.f) as num from
(select 0 as f union all select 1) t1,
(select 0 as f union all select 1) t2,
(select 0 as f union all select 1) t3,
(select 0 as f) as t4;

収量:

num
--------------------
1
2
3
4
5
6
7
8
于 2012-10-11T21:05:22.370 に答える
0

Well, for this little query :

DECLARE @row as int
SET @row = 0

SELECT @row = @row + 1 as num FROM
(select 0 as f union all select 1) t1,
(select 0 as f union all select 1) t2,
(select 0 as f union all select 1) t3;

you try to give a name to variable, so setting AND outputting is what you want, that is not possible. you have to do the first or the last, not both..

DECLARE @row as int
SET @row = 0

SELECT @row = @row + 1 FROM
(select 0 as f union all select 1) t1,
(select 0 as f union all select 1) t2,
(select 0 as f union all select 1) t3;

SELECT @row as num
于 2012-10-12T09:24:01.300 に答える