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
Solved

SQL update query syntax - SQL 2008 R2

Posted on 2014-02-20
3
368 Views
Last Modified: 2014-02-20
Hi experts,

In the data here, SAMPLE_ID_ALT needs to be updated to what I have typed in.  For every 'NS' in the SAMPLE_ID column, the SAMPLE_ID_ALT needs to be set incrementally to NS_001, NS_002 and start again when a new SITE_ID is found.  

I have tried this, and can see why the optimizer might not like it, but I cannot find a workaround.  Can anyone help please?  I would rather not use cursors if I can help it.

UPDATE GB_CB_LITH
SET SAMPLE_ID_ALT = 'NS_'+ RIGHT('000'+CAST(ROW_NUMBER()
                               OVER (PARTITION BY SITE_ID
                                     ORDER BY DEPTH_FROM)
               AS VARCHAR(3)),3)
  WHERE SAMPLE_ID = 'NS'
TEMP-LITH-DATA2.xlsx
0
Comment
Question by:colinspurs
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39873308
try this out

;WITH C AS
(
SELECT RIGHT('000'+CAST(ROW_NUMBER() OVER (PARTITION BY SITE_ID ORDER BY DEPTH_FROM) AS VARCHAR(3)),3) RN,* FROM GB_CB_LITH WHERE G.SAMPLE_ID = 'NS' 
)

UPDATE G
SET SAMPLE_ID_ALT = 'NS_'+ RN
FROM GB_CB_LITH G
JOIN C 
ON G.<PK> = C.<PK>
WHERE G.SAMPLE_ID = 'NS' 


--NOTE in the above you have to replace the PK with the primary keys

Open in new window

0
 
LVL 3

Author Comment

by:colinspurs
ID: 39873398
Superb!  Thanks.

I think you meant GB_CB_LITH G WHERE G.SAMPLE_ID = 'NS'  in line 3.
                                                  =
0
 
LVL 3

Author Closing Comment

by:colinspurs
ID: 39873400
Brilliant, thanks a lot.
0

Featured Post

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.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

856 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