Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


update table with next value


update table with next value

Author
Message
ronan.healy
ronan.healy
SSC-Enthusiastic
SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)

Group: General Forum Members
Points: 136 Visits: 232
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
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16616 Visits: 17024
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)
ronan.healy
ronan.healy
SSC-Enthusiastic
SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)

Group: General Forum Members
Points: 136 Visits: 232
ya just double checked there and its ordered on the irbrating
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16616 Visits: 17024
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)
ronan.healy
ronan.healy
SSC-Enthusiastic
SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)

Group: General Forum Members
Points: 136 Visits: 232
thanks very much for help will try that

and also will try improve my [post in future
again thanks
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16616 Visits: 17024
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. :-D 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)
ronan.healy
ronan.healy
SSC-Enthusiastic
SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)

Group: General Forum Members
Points: 136 Visits: 232
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
ChrisM@home
ChrisM@home
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1161 Visits: 9736
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
ronan.healy
ronan.healy
SSC-Enthusiastic
SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)

Group: General Forum Members
Points: 136 Visits: 232
thanks for response will try that
ronan.healy
ronan.healy
SSC-Enthusiastic
SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)

Group: General Forum Members
Points: 136 Visits: 232
still getting this error

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


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search