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


Updation


Updation

Author
Message
kapil_kk
kapil_kk
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2427 Visits: 2763
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 Journeyman
SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)

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

Group: General Forum Members
Points: 2427 Visits: 2763
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
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1134 Visits: 3229
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
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1134 Visits: 3229
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
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2427 Visits: 2763
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
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2427 Visits: 2763
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
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2427 Visits: 2763
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
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1134 Visits: 3229
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
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2427 Visits: 2763
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