Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Update multiple rows with a specific sequence Expand / Collapse
Author
Message
Posted Tuesday, February 12, 2013 4:19 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, August 1, 2013 12:41 AM
Points: 44, Visits: 84
Can anyone help me to build a sql query for updating multiple rows with different id.

Sample Data, ACTCODE datatype nchar(6)

ACTCODE ACTDESC

110001 J's Dream
110003 FPAB
110009 Jharna Dhara

(Where ACTCODE BETWEEN 110001 AND 110009)

will be updated with different ACTCODE in a sequence with incremented by +1

220004 J's Dream
220005 FPAB
220006 Jharna Dhara



Post #1418863
Posted Tuesday, February 12, 2013 2:55 PM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Thursday, September 11, 2014 11:25 AM
Points: 620, Visits: 865
Might look into the ROW_NUMBER() function.

CREATE TABLE #TmpTbl (ACTCODE NCHAR(6));

INSERT INTO #TmpTbl (ACTCODE) VALUES ('110001'), ('110003'), ('110009');

DECLARE @SEQUENCE NCHAR(6) = 220000;

SELECT ACTCODE
, @SEQUENCE + ROW_NUMBER() OVER (ORDER BY ACTCODE)
FROM #TmpTbl;

DROP TABLE #TmpTbl;



_____________________________________________________________________
- Nate

@nate_hughes
Post #1419207
Posted Tuesday, February 12, 2013 4:02 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721

--this would be your original source table
IF OBJECT_ID('tempdb..#SourceTable') IS NOT NULL
DROP TABLE #SourceTable

CREATE TABLE #SourceTable (
[ID] INT IDENTITY(1,1) NOT NULL,
[Name] NVARCHAR(50) NULL,
PRIMARY KEY (ID))

SET IDENTITY_INSERT #SourceTable ON
INSERT INTO #SourceTable (ID,Name)
SELECT 110001,'J''s Dream'
UNION
SELECT 110003,'FPAB'
UNION
SELECT 110009,'Jharna Dhara'
SET IDENTITY_INSERT #SourceTable OFF

--this returns your original data
SELECT * FROM #SourceTable


If you are converting a large amount of data (say more than 100,000 rows) I'd recommend that the target table be a real table and not a temp table. If you have millions of rows you probably want to break the job into batches.


--create a temp table to hold the data
--and generate the new IDENTITY sequence
IF OBJECT_ID('tempdb..#TargetTable') IS NOT NULL
DROP TABLE #TargetTable

CREATE TABLE #TargetTable (
[ID] INT IDENTITY(22004,1) NOT NULL,
[Name] NVARCHAR(50) NULL,
PRIMARY KEY (ID))


INSERT INTO #TargetTable (Name)
SELECT Name
FROM #SourceTable

--delete the old data (make sure you have a backup)
TRUNCATE TABLE #SourceTable

--now insert the data with the new ID sequence
SET IDENTITY_INSERT #SourceTable ON
INSERT INTO #SourceTable (ID,Name)
SELECT ID,Name
FROM #TargetTable
SET IDENTITY_INSERT #SourceTable OFF

--this is your transformed data
SELECT * FROM #SourceTable


Post #1419231
Posted Tuesday, February 12, 2013 8:15 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, August 1, 2013 12:41 AM
Points: 44, Visits: 84
Actually I want to update by set command for multiple rows containing the ACTCODE in a sequence like (001, 002, 003 ETC) with the sample provided earlier.

Sample Data, ACTCODE datatype nchar(6)

ACTCODE ACTDESC

110001 J's Dream
110003 FPAB
110009 Jharna Dhara

(Where ACTCODE BETWEEN 110001 AND 110009)

will be updated with different ACTCODE in a sequence with incremented by +1

220004 J's Dream
220005 FPAB
220006 Jharna Dhara

By set command
Post #1419277
Posted Wednesday, February 13, 2013 6:11 AM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Thursday, September 11, 2014 11:25 AM
Points: 620, Visits: 865
Might not be the most efficient way but seems to meet your criteria, no?

CREATE TABLE #TmpTbl (RowId INT IDENTITY(1,1), ACTCODE NCHAR(6));

INSERT INTO #TmpTbl (ACTCODE) VALUES ('110001'), ('110003'), ('110009'), ('120000'), ('120001');

DECLARE @SEQUENCE NCHAR(6) = 220000;

SELECT * FROM #TmpTbl;

UPDATE tmp
SET ACTCODE = s.sACTCODE
FROM #TmpTbl tmp
INNER JOIN (
SELECT ACTCODE
, sACTCODE = @SEQUENCE + ROW_NUMBER() OVER (ORDER BY ACTCODE)
FROM #TmpTbl
WHERE ACTCODE BETWEEN 110001 AND 110009
) s ON tmp.ACTCODE = s.ACTCODE;

SELECT * FROM #TmpTbl;

DROP TABLE #TmpTbl;



_____________________________________________________________________
- Nate

@nate_hughes
Post #1419457
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse