|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 7:11 AM
Points: 1,130,
Visits: 1,278
|
|
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
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Friday, March 01, 2013 6:16 AM
Points: 81,
Visits: 286
|
|
please post table structure and more sample data also give more focus on what you want.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 7:11 AM
Points: 1,130,
Visits: 1,278
|
|
| i have already posted the script of table creation with some sample data...
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Today @ 10:57 AM
Points: 838,
Visits: 2,200
|
|
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
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Today @ 10:57 AM
Points: 838,
Visits: 2,200
|
|
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
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 7:11 AM
Points: 1,130,
Visits: 1,278
|
|
oops sorry..... that table is of proceduremaster only...
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 7:11 AM
Points: 1,130,
Visits: 1,278
|
|
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
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 7:11 AM
Points: 1,130,
Visits: 1,278
|
|
| but jason I alsio need to updateed parentprocedureID of procedureId 86 as 86 only----
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Today @ 10:57 AM
Points: 838,
Visits: 2,200
|
|
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
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 7:11 AM
Points: 1,130,
Visits: 1,278
|
|
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!!!
|
|
|
|