Blank column to be unique increment

Hi Everyone,
I'm trying to write an update SQL to fill an Integer column with an 
incremental number.

Example:
My table has 500 rows, I want this new Integer column to be filled with 
number of 1 to 500.

Is there a way to do this via an update SQL statement?

Thanks in advance.
0
Myrlar
1/29/2010 3:20:52 AM
sqlserver.programming 1873 articles. 0 followers. Follow

2 Replies
999 Views

Similar Articles

[PageSpeed] 40

Here is one example:

CREATE TABLE Foo (
  keycol INT NOT NULL PRIMARY KEY,
  datacol CHAR(1),
  seq_nbr INT);

INSERT INTO Foo (keycol, datacol) VALUES (1, 'a');
INSERT INTO Foo (keycol, datacol) VALUES (8, 'b');
INSERT INTO Foo (keycol, datacol) VALUES (11, 'c');
INSERT INTO Foo (keycol, datacol) VALUES (15, 'd');
INSERT INTO Foo (keycol, datacol) VALUES (17, 'e');

WITH Ranked AS (
SELECT seq_nbr, ROW_NUMBER() OVER(ORDER BY keycol) AS rk
FROM Foo)
UPDATE Ranked
SET seq_nbr = rk;

SELECT keycol, datacol, seq_nbr
FROM Foo;

/*

keycol      datacol seq_nbr
----------- ------- -----------
1           a       1
8           b       2
11          c       3
15          d       4
17          e       5

*/

DROP TABLE Foo;

-- 
Plamen Ratchev
http://www.SQLStudio.com
0
Plamen
1/29/2010 4:00:31 AM
Here is another solution:

  create table #t(n int)
  
  declare @offset int
  insert into #t values (1)
  set @offset=1
  
  while @offset < 500
  begin
    insert into #t (n)
    select n+@offset
    from #t
    where n+@offset <= 500
    
    set @offset=@offset*2
  end
  
  select * from #t
  
  drop table #t

-- 
Gert-Jan


Myrlar wrote:
> 
> Hi Everyone,
> I'm trying to write an update SQL to fill an Integer column with an
> incremental number.
> 
> Example:
> My table has 500 rows, I want this new Integer column to be filled with
> number of 1 to 500.
> 
> Is there a way to do this via an update SQL statement?
> 
> Thanks in advance.
0
Gert
1/29/2010 9:11:44 AM
Reply:

Similar Artilces:

Help with column
I have a datafeed that comes to me everyday i need to format one column of around 1000 rows so it multiples the total column but a 1000 so getting rid of the decimal point i.e =A3100.00 becoming 10000 , i also need to do this using a macro. can anyone help=20 Regards=20 Paul Dim iLastRow As Long Dim i As Long iLastRow = Cells(Rows.Count, "A").End(xlUp).Row For i = 1 To iLastRow Cells(i, "A").Value = Cells(i, "A").Value * 100 Next i -- HTH Bob Phillips "lennymos123" <paul@palo.co.uk> wrote in message news:1115651206....

How do I pivot a range of cells, i.e. from column to row?
How do I pivot/transpose the data from a range of cells from, say, a column to a row? I imagine it should be very simple, but I haven't found out how yet. For instance: row# 1 data 1 2 data 2 3 data 3 4 data 4 to column# 1 2 3 4 data1 data2 data3 data4 ...

SUM cells in column based upon criteria in two others.
Can not seem to put together a formula that accomplishes the following: Column J contains contributions by individuals for 2010 Column I contains contributions by individuals made in 2009 Column N contains an indicator (+ - or 0) of the increase or decrease over the two years Column P contains a designation of which members are new members. I need a formula that looks at column I cells against column J cells, totals the contributions made be each of the criteria in column N, but does NOT include those with any designation in column P. Example of the data: I ...

Consecutive unique numbers
Hi I use publisher to print some labels at work. It would be really useful if I could print each label with a unique and possibly sequential number. Is this possible using MS publisher, or even MS Word? Thanks Andy. Hi Andy, Yes, you can do this using Microsoft Word or Publisher. What you need to do is create a database of numbers, such as using Excel, and then using the Mail Merge features in Microsoft Word/Publisher, you can merge those numbers to each label. Visit the training section of my site to learn more about merging onto labels/business cards using a database. Brian K...

Creating column
I would like to create a column that automatically adds one for each row. Ex. 2001, 2002, 2003, 2004 Hi, Type 2001 in any one cell (say G1) and then in G2, type =G1+1. COpy this formula down -- Regards, Ashish Mathur Microsoft Excel MVP "AAA1986" <AAA1986@discussions.microsoft.com> wrote in message news:6850AC6B-AD98-4AE3-9ABA-D81D06583052@microsoft.com... > I would like to create a column that automatically adds one for each row. > > Ex. 2001, 2002, 2003, 2004 ...

Importing customer class blanks out tax schedule
Hi - I used Integration Manager to update (only) the customer class. This caused those particular customer's to have their tax schedule ID blanked out. I set all fields other than the customer class to "Use Default". I was expecting no change to fields other than the customer class. Is this wrong? Ginny IM simulates a person keying in data. When you key data into the Customer Class field, by default, it copies data from the class into other fields (like tax schedule). You may have also changed some of the account numbers associated with the customer. -- Richard L...

Blank reply in emails but can view them in "auto preview"
When recieving a reply to some emails, I see nothing but the original email text without the reply text. The only way that I can see the reply text is if I set Outlook to use "auto preview", but there's nothing there if the message is opened. We are using Outlook 2000 sp3 internet mail (POP3) through a Verizon DSL connection (SMC Barricade router). It also seems to only happen when correxponding to people that are using Exchange Server. I've seen a few posts regarding this issue but I have not seen any resolution. I think it may also have to do with messages th...

Dropbox on each cell of column
Is it possible to have a dropbox appear on each line of a column similar to having a combo box on a form in the datasheet view in Access? Yes there is. If you open the "Forms" tool bar, you can select either a drop down box or a list box. Once you place it on your sheet, right click on it and select "format control", then select the "control" tab. The top line (input range) points to the row or column of cells that you want to appear within the box. The second line (cell range) points to a cell in which a number will appear based on what line within the inp...

Sorting Columns on HTML file created in Excel
Hi.. I'm not sure if I'm just searching for the wrong words, but can't find the answer to this.. I've created an excel file, about 15 columns.. In excel, I can sort the columns easily, but, I've saved the file as HTML now. How can I make it that those columns can still be sortable on the internet? Is there special code I need to put somewhere? I've seen javascript codes that can do this, but I would like to do it all via excel if possible. Any help appreciated! Thanks, -- Rob Hi Terri and Rob, You could include the JavaScript (Bookmarklet) within your HTML cod...

How to Add Columns to "Blank" MS Project?
Is there a way to add columns to "Blank" project that comes up when I click on File/New? I want to always have Text1, Text2, etc with the names Renamed on every Project file I open. This is a multi-part message in MIME format. ------=_NextPart_000_0014_01CAC558.64747C70 Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: quoted-printable Hi TKGerdie, Welcome to this Microsoft Project newsgroup :-) The best way is to set up a project as you want it and then save it as a = template. Please see FAQ Item: 8. New Project Template. ...

Why doesn't the code create a column stacked chart?
Hi again...im sorry to ask for help so much, but charts vba is tricky ... I try to create a column stacked chart of three bars containing each three different values, much like Andy Pope's http://andypope.info/charts/StackColTotal.htm The CHT_21QSBA_S, CHT_21QSBA_B and CHT_21QSBA_A ranges are each three different cells (name CHT_21QSBA_S = A1,B5,Q3) and I thought this together with stating charttype=xlcolumnstacked should create a stacked chart - but it make 9 bars instead... Why? .... Set cht = ActiveSheet.ChartObjects("R_Q_" & Sheet1.Range("RAPP_TILLF").Value ...

Auto filter on blank lines
When I auto filter there is normally blank lines at the bottom but now when it's filtered it's greyed out and I can't add any new lines. ...

wmp10 "choose columns" greyed out
This may be a duplicate post. My computer crashed while I was posting it, so I don't know if it went through. Anyway, my problem is with WMP10. There used to be an option to see the song titles (probably an older version), and I don't see that anymore. Under "All music" there is "Artist/Album" and all I see is the artist. I went to "View" to try and add columns, and the option for "choose columns" is greyed out. Any ideas on how to fix this? I'm not too familiar with WMP (was using WINAMP) so any help would be appreciated. ...

Business Contacts
Just found this site - exciting to learn from the experts! I have a long list of business contacts sorted by Accounts. Unfortunately, I clicked the Accounts column while in the Business Contacts view, and all those accounts disappeared from the column. Now all my contacts are ungrouped (over 3,000). Each individual contact still retains its Linked Account designation, but those accounts are no longer organized via that filter. Can anyone help with bringing back that category to my Business Contacts view? I hope this explanation makes sense. Thanks so much in advance. . Sub...

how to merge data from multiple columns to one column
I have first, middle, and last name in different columns. How do I merge these three into one column? Try... =A1&" "&B1&" "&C1 for the format John T. Smith OR =C1&", "&A1&" "&B1 for the format Smith, John T. Hope this helps! In article <24D1088D-0AB1-4910-BF0E-37D8D7A4C056@microsoft.com>, "w8ting4hlp" <w8ting4hlp@discussions.microsoft.com> wrote: > I have first, middle, and last name in different columns. How do I merge > these three into one column? ...

Cell Greater Than Cell in Adjacent Column
A B C D 1 112.44 106.92 Y is A1>B4 2 104.90 96.03 N is A2>B4 3 109.71 103.46 N is A3>B4 4 115.74 111.50 Y is A4>B8 5 111.55 110.40 N is A5>B8 6 112.85 112.56 N is A6>B8 7 113.27 113.80 N is A7>B8 8 118.58 114.59 Y is A8>B9 9 118.25 115.85 Y is A9>B12 10 99.75 115.83 N is A10>B12 11 100.25 104.14 N is A11>B12 12 103.69 101.00 Y is A12>B13 13 103.58 99.84 Y is A13>B14 14 107.99 100.75 Y is A14>B15 15 106.33 104.27 I have two columns to which data is entered at the top by inserting a blank row and then adding the data points. The formula that I would ...

One cell from several sheets to one column
I have a spreadsheet with over 200 tabs/worksheets. I want to copy the same cell from every worksheet into one sheet into a single column. The worksheets have unique names. Is a macro the easiest way to move the data? If so, how can I set the macro up to go through each tab, so I don't have to go to each tab and run the macro separately? Thanks, Kel Examples of sheet names and what cell you want? -- Regards Ken....................... Microsoft MVP - Excel Sys Spec - Win XP Pro / XL 97/00/02/03 ------------------------------------------------------------...

How to recreate blank mailbox stores?
We've got ourselves into a mess, our server has crashed and whilst it has restarted the mailbox stores have refused to mount. We've tried the obvious things and regrettably concluded that we would need to go back to back up. Unfortunately our backup hasn't been working properly for a couple of weeks now and our local support guy is on holiday We don't store much e-mail on the server anyway. Time is of the essence and our guys need to be able to receive e-mails before the day is out. I've decided that I am prepared to sacrifice the existing data if necessary. Would it ...

can't create a blank database
When I click on new blank database, it opens as an MDE with 5 hidden and uneditable tables locked in. They are as follows. MSysAccessStorage, MSysACEs, MSysObjects, MSysQueries, MSysRelationships. How do I open just a blank Access database application so I can add tables and create/delete and do everything until I am ready to make as an MDE? On Tue, 19 Feb 2008 08:57:03 -0800, JGAMBLER <JGAMBLER@discussions.microsoft.com> wrote: >When I click on new blank database, it opens as an MDE with 5 hidden and >uneditable tables locked in. They are as follows. MSysAccessStorage, &g...

Rows and Columns
I am using Excel XP Each worksheet has 65,536 rows and 256 columns according to specifications. Is there a way to change these number to a smaller amount? I know I can hide rows and columns, but wondering if I can not have them to start them. TIA Bill The short answer is NO. Just don't format more than you need. -- Don Guillett SalesAid Software donaldb@281.com "EZDOIT" <Please@smile.com> wrote in message news:dxupc.98556$Ik.7697285@attbi_s53... > I am using Excel XP > > Each worksheet has 65,536 rows and 256 columns according to specifications. > >...

I have a column of data and I'd like to transpose it to go across.
I have a column of data going down that I'd like to transpose to go across the page so that I can add this info to data going across the page. I mean for example, I have data Jan, Feb, Mar, going down the page and I'd like them to be going across like 1st Month, 2nd Month, 3rd Month etc., but they are going from top to bottom and I want them to go from right to left. I tried the Transpose function, however, when I went to click on the arrow to the Right of PASTE, there was no arrow. Any suggestions?? John Copy the data, right click in Cell where you want to begin paste, sel...

Failed to hide column with message "cannot shift object off sheet"
Hi, I've a Excel 2000 worksheet that has 173 columns (A to FQ) with comments in a number of cells. I can't hide columns (say E:BW) with error message "cannot shift object off sheet". I searched into Microsoft Knowledgebase and found article 211769. I followed the resolution but stopped at step "3." where I can't find "Comment" of Format menu. Can anyone tell me how to resolve this problem? Thanks, Frankie What is in columns FR through IV? If nothing, columns FR:IV and Right-click on a selected columns header - select "Delete. -- HTH...

Sum of Column
I am trying to print a physical inventory with the total cost at the bottom. How can i do this? Thanks ...

Chart should read as blank cell not as 0,it shows blank but reads
A B c 1 Project Start Date Jan-10 2 Project Duration 25 3 Project Cost 1000000 In B6 i wriiten formula IF(ROWS($1:2)-1>C$2,"",ROWS($1:2)-1) In D6 i written formula IF(B6="","",B6/$C$2) in E6 i writtend formula IF(D6<=33.33%,(($I$1*D6^2)*100),IF(D6<=66.67%,($I$2*D6+$I$3),IF(D6>=66.67%,($I$4+$I$5*D6+$I$6*...

VBA Macro change column contents
I have a column in a worksheet which has a list of 3 digit numbers. I want to change the 3 digits to become 4 digits, the new fourth digit has to be a leading zero, i.e 355 to become 0355. I prefer the format of the digits to be a number but a text format would not be a problem. The length of the digits in the column are not static. i.e one month there may be 200 rows of data the next might be 300 and I would want the macro to cater for this variation. Thanks -- James James, A macro simply isn't necessary, select the column and apply a custom format of 0000 that...