Solved

T-SQL syntax to add a unique value to field

Posted on 2002-07-10
1
257 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
1 Comment
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 225 total points
Comment Utility
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

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.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ODBC Informix Driver 11 91
Query combine all charges 6 56
Vertical to Horizontal Data in Excel 2013--Large File 10 60
.csv files 5 76
Creating and Managing Databases with phpMyAdmin in cPanel.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
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…

728 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

11 Experts available now in Live!

Get 1:1 Help Now