SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Windowed functions can only appear in the SELECT o
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

bacm914
Starting Member

USA
2 Posts

Posted - 05/28/2012 :  09:46:31  Show Profile  Reply with Quote
I am trying to update the ID # in this table based on Type. I keep getting the error "Windowed functions can only appear in the SELECT or ORDER BY clauses." I've tried changing it based on other things I've read but I just can't get it to work. I'm sure it's something very simple. What is wrong with this code?:

UPDATE Loc
SET Custom15 = ID
,Id =CASE Type
WHEN 'FMTD' THEN '10-'
WHEN 'FM8' THEN '20-'
END + CAST(999+ROW_NUMBER( ) OVER (PARTITION BY Type ORDER BY Loc) AS VARCHAR(15))

Thanks in advance!!

khtan
In (Som, Ni, Yak)

Singapore
17431 Posts

Posted - 05/28/2012 :  10:01:00  Show Profile  Reply with Quote

update l
set    Custom15 = ID
       ,Id = NEW_ID
from
(
    select *, NEW_ID = CASE Type
                       WHEN 'FMTD' THEN '10-'
                       WHEN 'FM8' THEN '20-'
                       END + CAST(999+ROW_NUMBER( ) OVER (PARTITION BY Type ORDER BY Loc) AS VARCHAR(15))
    from   Loc
) l



KH
Time is always against us

Go to Top of Page

bacm914
Starting Member

USA
2 Posts

Posted - 05/28/2012 :  10:29:43  Show Profile  Reply with Quote
Thanks a bunch! Worked perfectly!!
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17431 Posts

Posted - 05/28/2012 :  10:47:59  Show Profile  Reply with Quote
Welcome


KH
Time is always against us

Go to Top of Page

pkokil
Starting Member

1 Posts

Posted - 04/17/2013 :  06:15:01  Show Profile  Reply with Quote
Very useful post...Thanks a lotttttt......
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000