Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

looking for a subtle way to increment variable in SQL 2005

Posted on 2013-06-26
Last Modified: 2013-08-20
hello experts,

    i am trying to increment counter within SQL 2005

 for example, instead of doing


I want to do

DECLARE @i smallint; SET @i = 1; -- instant initialization in SQL 2005 does not fly, i gather

SELECT @i++ UNION   --  this is just an improvisation
SELECT @i++  -- ????

i think, in 2008 and 2012 they have cool shortcuts to do this, but what about SQL 2005
i would prefer not to do SET @i = @i + 1 in between every time

thanks in advance
Question by:AlexF777
  • 2
  • 2
  • 2
LVL 16

Assisted Solution

EvilPostIt earned 500 total points
ID: 39278315
Are you talking about an auto increment or inserting a set a data and generating an ascending ID against that?
LVL 65

Expert Comment

by:Jim Horn
ID: 39278438
You're going to have to paint us a bigger picture of what you're trying to pull off here.

Author Comment

ID: 39278496
autoincrement: i want each consequitive SELECT statement to use the next value: i.e.
1, 2, 3, ...
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

LVL 65

Expert Comment

by:Jim Horn
ID: 39278531
>i would prefer not to do SET @i = @i + 1 in between every time
Unless you can paint us a bigger picture of what your trying to pull off, this is the only solution that is guaranteed to work.

You could have an identity column in your table, but it will incriment on rows, not on INSERT statements that created those rows, so that works only if your INSERTS insert one row at a time.

Author Comment

ID: 39278678
no, i want to increment my variable in memory
with SQL 2008 i could say


actually, i probably could use an identity column: i could insert all my rows into the temp
table, then do an INSERT into a live table FROM SELECT and that will probably do it ...
LVL 16

Accepted Solution

EvilPostIt earned 500 total points
ID: 39279272
Identity column was what I was leaning towards. That makes the most sense.

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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 datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

809 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