dfwcharles
Technical User
Hi,
I ‘m trying to create a stored procedure with two variables that will do a simple insert into TableOne, and then insert into TableTwo based on the values inserted into Tableone, plus values that were pre-existing in Tableone.
Heres an example
TableOne:
location parent
----------------- -----------------
105-01A0G-FDR01 NULL
105-01A0G-FDR02 NULL
105-01A0G-TRN01 105-01A0G-FDR01
105-01A0G-TRN01 105-01A0G-FDR02
105-01A0G-BRK01 105-01A0G-TRN01
TableTwo:
location ancestor
----------------- ----------------
105-01A0G-BRK01 105-01A0G-BRK01
105-01A0G-BRK01 105-01A0G-FDR01
105-01A0G-BRK01 105-01A0G-FDR02
105-01A0G-BRK01 105-01A0G-TRN01
105-01A0G-FDR01 105-01A0G-FDR01
105-01A0G-FDR02 105-01A0G-FDR02
105-01A0G-TRN01 105-01A0G-FDR01
105-01A0G-TRN01 105-01A0G-FDR02
105-01A0G-TRN01 105-01A0G-TRN01
When I insert these values into tableone, @location = ‘105-01A0G-BRK01’ and @parent= ‘105-01A0G-TRN01’
The sp needs to insert the following rows into Tabletwo:
Location = @location, ancestor = @location
Location = @location, ancestor = select parent from tableone where location=@parent (original parent)
Location = @location, ancestor = select parent from tableone where location=(parent of parent of original parent)
Location = @location, ancestor = select parent from tableone where location=(parent of parent of parent of original parent)
This needs to continue until the parent = null in Tableone.
I tried using simple select statements with variables in the sp, but depending on the number of parents in tableone, you can return more than one value for those varibables, which of course wont work.
I hope I explained my problem clearly, I’m having a hard time understanding it myself.
Thanks in advance.
Charles
I ‘m trying to create a stored procedure with two variables that will do a simple insert into TableOne, and then insert into TableTwo based on the values inserted into Tableone, plus values that were pre-existing in Tableone.
Heres an example
TableOne:
location parent
----------------- -----------------
105-01A0G-FDR01 NULL
105-01A0G-FDR02 NULL
105-01A0G-TRN01 105-01A0G-FDR01
105-01A0G-TRN01 105-01A0G-FDR02
105-01A0G-BRK01 105-01A0G-TRN01
TableTwo:
location ancestor
----------------- ----------------
105-01A0G-BRK01 105-01A0G-BRK01
105-01A0G-BRK01 105-01A0G-FDR01
105-01A0G-BRK01 105-01A0G-FDR02
105-01A0G-BRK01 105-01A0G-TRN01
105-01A0G-FDR01 105-01A0G-FDR01
105-01A0G-FDR02 105-01A0G-FDR02
105-01A0G-TRN01 105-01A0G-FDR01
105-01A0G-TRN01 105-01A0G-FDR02
105-01A0G-TRN01 105-01A0G-TRN01
When I insert these values into tableone, @location = ‘105-01A0G-BRK01’ and @parent= ‘105-01A0G-TRN01’
The sp needs to insert the following rows into Tabletwo:
Location = @location, ancestor = @location
Location = @location, ancestor = select parent from tableone where location=@parent (original parent)
Location = @location, ancestor = select parent from tableone where location=(parent of parent of original parent)
Location = @location, ancestor = select parent from tableone where location=(parent of parent of parent of original parent)
This needs to continue until the parent = null in Tableone.
I tried using simple select statements with variables in the sp, but depending on the number of parents in tableone, you can return more than one value for those varibables, which of course wont work.
I hope I explained my problem clearly, I’m having a hard time understanding it myself.
Thanks in advance.
Charles