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 ««123»»

update table with next value Expand / Collapse
Author
Message
Posted Friday, October 5, 2012 9:13 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, November 24, 2014 6:03 AM
Points: 134, Visits: 227
hi
i think i just put them in to quick to get a reply.
they are odrered on the number from the irbt table.
so 2- 25 in that oder
2
3
4
5 etc
Post #1369132
Posted Friday, October 5, 2012 9:16 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:50 PM
Points: 13,325, Visits: 12,811
ronan.healy (10/5/2012)
hi
i think i just put them in to quick to get a reply.
they are odrered on the number from the irbt table.
so 2- 25 in that oder
2
3
4
5 etc


So you are saying that we can use IRBRATING as the order?


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1369135
Posted Friday, October 5, 2012 9:22 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, November 24, 2014 6:03 AM
Points: 134, Visits: 227
ya just double checked there and its ordered on the irbrating
Post #1369139
Posted Friday, October 5, 2012 9:41 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:50 PM
Points: 13,325, Visits: 12,811
Just to help you with future posts, if you post your data in a nice and clean consumable format it will go a long way.

CREATE TABLE dbo.IRBT 
(
MOODYSRATE CHAR(10) NOT NULL,
SNPRATE CHAR(10) NOT NULL,
FITCHRATE CHAR(10) NOT NULL,
IRBRATING NUMERIC(4) NOT NULL,
CONSTRAINT IRBTPK PRIMARY KEY CLUSTERED (MOODYSRATE)
)

CREATE TABLE ACRT
(
CNO CHAR(100) NOT NULL,
OPICSCNO CHAR(100) NULL,
MOODYSRATE CHAR(10) NOT NULL,
SNPRATE CHAR(10) NOT NULL,
FITCHRATE CHAR(10) NOT NULL,
MOODYSIRB NUMERIC(4) NULL,
SPIRB NUMERIC(4) NULL,
FITCHIRB NUMERIC(4) NULL,
MOODYSWATCH BIT DEFAULT 0 NOT NULL,
SPWATCH BIT DEFAULT 0 NOT NULL,
FITCHWATCH BIT DEFAULT 0 NOT NULL,
MIDRATEAGENCY CHAR(10) NULL,
MIDRATING CHAR(10) NULL,
MIDIRB NUMERIC NULL,
NEGWATCH BIT DEFAULT 0 NOT NULL
)

INSERT INTO IRBT VALUES ('AAA','AAA','AAA','2')
INSERT INTO IRBT VALUES ('AA1','AA+','AA+','3')
INSERT INTO IRBT VALUES ('AA2','AA','AA','4')
INSERT INTO IRBT VALUES ('AA3','AA-','AA-','5')
INSERT INTO IRBT VALUES ('A1','A+','A+','6')
INSERT INTO IRBT VALUES ('A2','A','A','7')
INSERT INTO IRBT VALUES ('A3','A-','A-','8')
INSERT INTO IRBT VALUES ('BAA1','BBB+','BBB+','9')
INSERT INTO IRBT VALUES ('BAA2','BBB','BBB','10')
INSERT INTO IRBT VALUES ('BAA3','BBB-','BBB-','11')
INSERT INTO IRBT VALUES ('BA1','BB+','BB+','12')
INSERT INTO IRBT VALUES ('BA2','BB','BB','16')
INSERT INTO IRBT VALUES ('BA3','BB-','BB-','20')
INSERT INTO IRBT VALUES ('B1','B+','B+','22')
INSERT INTO IRBT VALUES ('B2','B','B','23')
INSERT INTO IRBT VALUES ('B3','B-','B-','24')
INSERT INTO IRBT VALUES ('CAA1','CCC+','CCC+','24')
INSERT INTO IRBT VALUES ('CAA2','CCC','CCC','24')
INSERT INTO IRBT VALUES ('CAA3','CCC-','CCC-','24')
INSERT INTO IRBT VALUES ('CA','CC','CC','24')
INSERT INTO IRBT VALUES ('C','C','C','24')
INSERT INTO IRBT VALUES ('D','D','D','25')

INSERT INTO ACRT VALUES ('ALLIANCE & LEICESTER BS','11292' ,'A2-', 'NR' ,'NR', '0.00','0.00','0.00','1','0','0','MOODYS','A2-','2.00','1')
INSERT INTO ACRT VALUES ('PRS 2005 2X A2A MBS (XS0234203684)','120577','AA2','A+' ,'AAA','4.00','6.00','2.00','0','0','0','MOODYS','AA2' ,'16.00','1')

The above code will actually run on SQL Server and doesn't have the SYBASE LOCK ALLPAGES.

OK so now on to a solution for your issue.

Using a cte this is actually pretty simple.

--before update
select * from ACRT

;with cte as
(
select *, ROW_NUMBER() over (Order by IRBRATING) as RowNum
from IRBT
)

update ACRT set MIDIRB = c2.IRBRATING
from ACRT a
join cte c on a.MIDIRB = c.IRBRATING
join cte c2 on c.RowNum + 1 = c2.RowNum

--now we can view the data after the update
select * from ACRT




_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1369153
Posted Friday, October 5, 2012 9:46 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, November 24, 2014 6:03 AM
Points: 134, Visits: 227
thanks very much for help will try that

and also will try improve my [post in future
again thanks
Post #1369156
Posted Friday, October 5, 2012 9:48 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:50 PM
Points: 13,325, Visits: 12,811
ronan.healy (10/5/2012)
thanks very much for help will try that

and also will try improve my [post in future
again thanks


No problem. Happy to help. The best thing you can do is to test your ddl and dml so you know it will work. Let me know if that doesn't work.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1369157
Posted Tuesday, October 9, 2012 4:23 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, November 24, 2014 6:03 AM
Points: 134, Visits: 227
hi
out of interest is it possible to do it this way
update ACRT set MIDIRB = min(IRBT2.IRBRATING)
from ACRT,IRBT,IRBT2 where ACRT.MIDIRB = IRBT.IRBRATING
and IRBT2.IRBRATING > IRBT.IRBRATING

and if so what do i have to do to get this error out of it

Msg 208, Level 16, State 1, Line 1
Invalid object name 'IRBT2'.

IRBT2 is a table in database
Post #1370289
Posted Tuesday, October 9, 2012 4:41 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 3:46 AM
Points: 1,037, Visits: 7,020
ronan.healy (10/9/2012)
hi
out of interest is it possible to do it this way
update ACRT set MIDIRB = min(IRBT2.IRBRATING)
from ACRT,IRBT,IRBT2 where ACRT.MIDIRB = IRBT.IRBRATING
and IRBT2.IRBRATING > IRBT.IRBRATING

and if so what do i have to do to get this error out of it

Msg 208, Level 16, State 1, Line 1
Invalid object name 'IRBT2'.

IRBT2 is a table in database


No - there's an aggregate operator but no GROUP BY clause.

-- test this
SELECT
ACRT.*,
x.MIDIRB
FROM ACRT
INNER JOIN IRBT
ON IRBT.IRBRATING = ACRT.MIDIRB
CROSS APPLY (
SELECT MIDIRB = MIN(IRBT2.IRBRATING)
FROM IRBT2
WHERE IRBT2.IRBRATING > IRBT.IRBRATING
) x

-- if it correctly displays the rows to be updated and the correct value to update to,
-- then convert the SELECT to an UPDATE:
UPDATE ACRT SET MIDIRB = x.MIDIRB
FROM ACRT
INNER JOIN IRBT
ON IRBT.IRBRATING = ACRT.MIDIRB
CROSS APPLY (
SELECT MIDIRB = MIN(IRBT2.IRBRATING)
FROM IRBT2
WHERE IRBT2.IRBRATING > IRBT.IRBRATING
) x




Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Post #1370298
Posted Tuesday, October 9, 2012 4:45 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, November 24, 2014 6:03 AM
Points: 134, Visits: 227
thanks for response will try that
Post #1370304
Posted Tuesday, October 9, 2012 4:46 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, November 24, 2014 6:03 AM
Points: 134, Visits: 227
still getting this error

Msg 208, Level 16, State 1, Line 1
Invalid object name 'IRBT2'.
Post #1370307
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse