Solved

SQL update column via loop

Posted on 2008-10-01
7
769 Views
Last Modified: 2010-08-05
Simple really

Need to update a column's value, by appending 1 to a counter and adding a string to it ...

SQL code should explain it all ...
-- Grab the max ext_ref_id and add one.
DECLARE @MAX_EXT_REF_ID AS INTEGER
 
SELECT @MAX_EXT_REF_ID = MAX(CAST(REPLACE(EXT_REF_ID, '_BLC', '') AS INTEGER)) + 1 
FROM [TBL_BLUECARE_CANDIDATES] 
WHERE [EXT_REF_ID] like '%_BLC'
 
SELECT @MAX_EXT_REF_ID -- RETURNS 78656
 
 
--SELECT ALL USERS THAT DO NOT HAVE '_BLC' in the [EXT_REF_ID]
SELECT [EXT_REF_ID]
FROM   [TBL_BLUECARE_CANDIDATES]
WHERE  [EXT_REF_ID] not like '%_BLC'
ORDER BY [EXT_REF_ID]
 
/* RETURNS DATA LIKE 
 
000
0001
001
002
003003003
007
01
079091
10661
11101110
11986
121212123
12335
1234
 
*/
 
 
-- NEED TO UPDATE ALL [EXT_REF_ID] WITHOUT '_BLC' and APPEND @MAX_EXT_REF_ID + 1 + '_BLC'
--  eg. 78656_BLC
--  eg. 78657_BLC
--  eg. 78658_BLC
--  eg. 78659_BLC
 
BEGIN LOOP 
	FOR EACH RECORD IN QUERY ABOVE
	UPDATE [EXT_REF_ID] = (@MAX_EXT_REF_ID + 1) + '_BLC'
END LOOP

Open in new window

0
Comment
Question by:kloner
[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
  • 4
  • 2
7 Comments
 
LVL 39

Assisted Solution

by:BrandonGalderisi
BrandonGalderisi earned 50 total points
ID: 22614780
Try this....

After you see if it works, remove the begin tran/rollback.
begin tran
DECLARE @MAX_EXT_REF_ID AS INTEGER
 
SELECT @MAX_EXT_REF_ID = MAX(CAST(REPLACE(EXT_REF_ID, '_BLC', '') AS INTEGER)) + 1 
FROM [TBL_BLUECARE_CANDIDATES] 
WHERE [EXT_REF_ID] like '%_BLC'
 
;with theRef as(
SELECT [EXT_REF_ID],row_number() over(order by [ext_Ref_id]) + @Max_ext_ref_id as rn
FROM   [TBL_BLUECARE_CANDIDATES]
WHERE  [EXT_REF_ID] not like '%_BLC')
 
update bcc
set ext_ref_id = cast(rn as varchar(16))+'_BLC'
from [tbl_bluecare_candidates] bcc
join theRef r
on bcc.ext_ref_id = r.ext_ref_id
 
select * from [tbl_bluecare_candidates]
 
rollback

Open in new window

0
 

Author Comment

by:kloner
ID: 22614943
I forgot to mention ... some of the existing [EXT_REF_ID] data that we are trying to update actually contains dulpicate data ... hence we can't rely on the join statement you suggested ... on bcc.ext_ref_id = r.ext_ref_id

.......

Current output dump:

000
000
0001
001
001
001
002
003003003
007
01
079091
10661
11101110
11986
11986
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22614968
So 001, 001, 001 would all need to be new records.  What is the PK of the table?
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:kloner
ID: 22615164
Well you won't believe this ... ther's no bloody PK in this OLD DB ... I can just get the duplicate records and manually update them hey as a quick win ...
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22615426
You should add an identity column and make it your PK.  I assume you don't have a clustered index either?
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22615433
It seems like that's what you are trying to do with the ext_ref_id.
0
 
LVL 27

Accepted Solution

by:
Zberteoc earned 75 total points
ID: 22616739
Here:
-- max value variable
DECLARE 
	@MAX_EXT_REF_ID int
 
-- get max value !!! WITHOUT ADDING 1 !!!
SELECT 
	@MAX_EXT_REF_ID = MAX(CAST(REPLACE(EXT_REF_ID, '_BLC', '') AS int)) 
FROM 
	[TBL_BLUECARE_CANDIDATES] 
WHERE 
	[EXT_REF_ID] like '%_BLC'
 
 
-- insert the distinct wanted [EXT_REF_ID] in a temp table
SELECT DISTINCT  
	[EXT_REF_ID]
INTO #aux
FROM   
	[TBL_BLUECARE_CANDIDATES] bc
WHERE  
	[EXT_REF_ID] not like '%_BLC'
ORDER BY
	[EXT_REF_ID]
 
-- add an identity column
ALTER TABLE #aux ADD id int identity
 
-- replace the [EXT_REF_ID] using the identity and @MAX_EXT_REF_ID
UPDATE bc SET
	[EXT_REF_ID]=id+@MAX_EXT_REF_ID
FROM
	[TBL_BLUECARE_CANDIDATES] bc
	INNER JOIN #aux a
		on a.[EXT_REF_ID]=bc.[EXT_REF_ID]

Open in new window

0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSIS how to COMPARE a data column from different servers? 6 140
Caste datetime 2 73
Format Date fields 11 64
What is this datetime? 1 33
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

732 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