Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8670965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:49:54+00:00 2026-06-12T18:49:54+00:00

In MySQL I can do something like this: SELECT @row := @row + 1

  • 0

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?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:49:55+00:00Added an answer on June 12, 2026 at 6:49 pm

    Well, if you just want the row number, you could just use ROW_NUMBER() in 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;
    

    That will return the row number for each combination. ROW_NUMBER() requires an ORDER BY, so just order by the first convenient column.

    SQLFiddle here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a MySQL statement like this: $sql=SELECT `name`, DATE(`time`), FROM `students` WHERE
Using MySQL , I can do something like: SELECT hobbies FROM peoples_hobbies WHERE person_id
I have a mysql table that looks something like this: Row 1: 'visitor_input_id' =>
Is there a mysql syntax that can preform a select values from a table,
I wonder if is it possible to run mysql command which can select an
I would like to ask something about query using mysql I have this table
I've been requesting MySQL results and looping through them like this: $query = SELECT
From MySQL - Get row number on select I know how to get the
In MySQL you can use the syntax DELETE t1,t2 FROM table1 AS t1 INNER
Our mysql processes can sometimes get backlogged and processes begin queuing up. I'd like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.