Solved

looking for a subtle way to increment variable in SQL 2005

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

    i am trying to increment counter within SQL 2005

 for example, instead of doing

SELECT  1 UNION
SELECT  2 UNION
SELECT 3

I want to do

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

INSERT INTO temp
SELECT @i UNION
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
0
Comment
Question by:AlexF777
  • 2
  • 2
  • 2
6 Comments
 
LVL 16

Assisted Solution

by:EvilPostIt
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?
0
 
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.
0
 

Author Comment

by:AlexF777
ID: 39278496
autoincrement: i want each consequitive SELECT statement to use the next value: i.e.
1, 2, 3, ...
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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.
0
 

Author Comment

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

SELECT @i UNION
SELECT @i+= 1 UNION

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 ...
0
 
LVL 16

Accepted Solution

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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now