Solved

T-SQL syntax to add a unique value to field

Posted on 2002-07-10
1
297 Views
Last Modified: 2012-05-04
I have a table with an empty field.  I would like to somehow count records that meet a criteria and assign a numeric value to that field.

My end result is to take info from a SQL table and dump it into a flat text file.  For each record that meets a criteria (BATCH = '999' and SEQUENCE is NULL) I want to put a unique number (1,2,3,4...) in the SEQUENCE field.  What I am running into is that I am not able to get SQL to look at each record individually.  It performs the update to all of the records in the SEQUENCE field and I am therefore not left with unique values.  Any help you can give would be greatly appreciated.

 

DATE       Amount     Branch  Batch   Sequence Account

---------- ---------- ------- ------- -------- -------

06/26/2002 117.08     33 658      999 NULL     105288

06/26/2002 117.00     33 658      999 NULL     301654

06/26/2002 80.00      33 658      999 NULL     032655

06/26/2002 255.00     33 658      999 NULL     311640

06/26/2002 200.00     33 658      999 NULL     145930

06/26/2002 70.00      33 658      999 NULL     126073

06/26/2002 70.00      33 658      999 NULL     126073
0
Comment
Question by:freydaddy1
[X]
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
1 Comment
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 225 total points
ID: 7143187
Try this loop:

set nocount on
declare @c int
set @c = 1
nextrecord:
SET ROWCOUNT 1
UPDATE report set seq = @c WHERE batch = 999 and seq IS NULL
IF @@ROWCOUNT >0  
BEGIN
  set @c = @c+ 1
  GOTO nextrecord
END
SET ROWCOUNT 0

select * from report where batch = 999 order by seq


CHeers
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Creating and Managing Databases with phpMyAdmin in cPanel.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

739 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