SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Updation


Updation

Author
Message
kapil_kk
kapil_kk
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5212 Visits: 2767
I have a table proceduremaster in which I need to update ParentProcedureID on basis of procedurecode and procedureID.

create table proceduremaster
( procedureid int primary key ,
procedurecode varchar(10),
ParentProcedureID int)

Insert into proceduremaster(procedureid, procedurecode)
select 86, '0062'
union all
select 87,'0062A'
union all
select 88,'0062B'
union all
select 93,8680
union
select 94,8680

Desired Result-- For all 3 rows inserted ParentProcedureID would be 86,86,86 as '0062' is parent of all procedureCode...

i wrote the query as-
UPDATE P
SET P.ParentProcedureID = (SELECT TOP 1 PP.ProcedureID FROM ProcedureMaster PP
WHERE rtrim(PP.ProcedureCode)= LEFT(RTRIM(P.ProcedureCode),LEN(RTRIM(P.ProcedureCode))-1) )
FROM ProcedureMaster P
WHERE right(rtrim(P.ProcedureCode),1) ='A'
but its not working correctly.
please suggest

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
BriPan
BriPan
SSC-Addicted
SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)

Group: General Forum Members
Points: 411 Visits: 296
please post table structure and more sample data
also give more focus on what you want.
kapil_kk
kapil_kk
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5212 Visits: 2767
i have already posted the script of table creation with some sample data...

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Jason-299789
Jason-299789
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4837 Visits: 3232
kapil_kk (11/19/2012)
i have already posted the script of table creation with some sample data...


Well you posted the DDL for a table called ProcedureCode, but not one for ProcedureMaster, which is referenced in the update.

If this is a true Parent Child Hierarchy, I would expect the row where ProcedureId=86 to have a NULL ParentProcedureId as this would be classed as the root node, so it cant be the parent of itself.

_________________________________________________________________________
SSC Guide to Posting and Best Practices
Jason-299789
Jason-299789
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4837 Visits: 3232
Heres some code that may be what you are looking for and sets the rows where the Procedurecode = '0062A' and '0062B' with 86, but doesnt do that where the Procedure Id=86.


create table #procedurecode
( procedureid int primary key ,
procedurecode varchar(10),
ParentProcedureID int)

Insert into #procedurecode(procedureid, procedurecode)
select 86, '0062'
union all
select 87,'0062A'
union all
select 88,'0062B'

Update
pc
Set
pc.ParentProcedureID=p.procedureid
from
#procedurecode pc
JOIN #procedurecode p on Left(pc.procedurecode,4)=P.procedurecode
Where
Right(pc.procedurecode,1) like '[A-Z]'

Select * From #procedurecode



_________________________________________________________________________
SSC Guide to Posting and Best Practices
kapil_kk
kapil_kk
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5212 Visits: 2767
oops sorry.....
that table is of proceduremaster only...

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
kapil_kk
kapil_kk
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5212 Visits: 2767
here is the table structure with some sample data:
create table proceduremaster
( procedureid int primary key ,
procedurecode varchar(10),
ParentProcedureID int)

Insert into proceduremaster(procedureid, procedurecode)
select 86, '0062'
union all
select 87,'0062A'
union all
select 88,'0062B'
union all
select 93,8680
union
select 94,8680

Desired o/p - for first 3 rows parentProcedureId would be 86 and for last 2 rows parentprocedureId would be 93

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
kapil_kk
kapil_kk
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5212 Visits: 2767
but jason I alsio need to updateed parentprocedureID of procedureId 86 as 86 only----

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Jason-299789
Jason-299789
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4837 Visits: 3232
kapil_kk (11/19/2012)
but jason I alsio need to updateed parentprocedureID of procedureId 86 as 86 only----


Personally I think thats the wrong path to take, as a Parent should not be the Parent of itself, its like saying that you are your own Father, it jsut doesnt make logical sense.

however this adaption of the code will work and update the ParentprocedureId 86 with for procdure 86.


Update
pc
Set
pc.ParentProcedureID=p.procedureid
from
#procedurecode pc
JOIN #procedurecode p on Left(pc.procedurecode,4)=P.procedurecode



_________________________________________________________________________
SSC Guide to Posting and Best Practices
kapil_kk
kapil_kk
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5212 Visits: 2767
Hi Jason,

Update
pc
Set
pc.ParentProcedureID=p.procedureid
from
ProcedureMaster pc
JOIN ProcedureMaster p on Left(pc.procedurecode,4)=P.procedurecode
Where
Right(pc.procedurecode,1) like '[0-9]'

This will also works..
your code is also working..
thnks a lot!!!:-)

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
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