[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

T-SQL syntax to add a unique value to field

Posted on 2002-07-10
1
Medium Priority
?
309 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 900 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Suggested Courses

650 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