September 2, 2012 at 10:59 pm
Actual Table:
IdName Parent_nameParent_id
1A.B A
2A.B(1).C B
3A.B(2).D B
4A.B.C.D.E D
5A.B.C.D(1).E D
6A.B.C.D(2).F D
Above table Name column having rows like(A.B,A.B(1).C,A.B.C.D.E,A.B.C.D(1).E,A.B.C.D(2).F)
Parent_Name column having rows like(A,B,B,D,D,D)
Requirement: The above table id is identity column based upon identity column compare Name with Parent_name need to generate Parent_id column.
I have write SP like the below:
CREATE Procedure [dbo].[SSIS_Parent_attr_list_seq_id]
as
begin
set nocount on;
WITH Cte_ATTR_LIST_LKP(Attribute_Name, Parent_Attribute_Name, ATTR_LIST_SEQ_ID, PARENT_ATTR_LIST_SEQ_ID)
AS (SELECT ast.Attribute_Name,
ast.Parent_Attribute_Name,
alk.ATTR_LIST_SEQ_ID,
alk.PARENT_ATTR_LIST_SEQ_ID
FROM dbo.ATTR_LIST_STG AS ast INNER JOIN
dbo.ATTR_LIST_LKP AS alk ON
ast.Attribute_Name = alk.Attribute_Name)
update ATTR_LIST_LKP set PARENT_ATTR_LIST_SEQ_ID=c_PARENT_ATTR_LIST_SEQ_ID from(
SELECT a1.ATTR_LIST_SEQ_ID, a1.Attribute_Name, a2.ATTR_LIST_SEQ_ID AS c_PARENT_ATTR_LIST_SEQ_ID
FROM Cte_ATTR_LIST_LKP AS a1 inner JOIN
Cte_ATTR_LIST_LKP AS a2 ON a1.Parent_Attribute_Name = a2.Attribute_Name) a3 inner join ATTR_LIST_LKP a4
on a3.Attribute_Name=a4.Attribute_Name
end
September 2, 2012 at 11:46 pm
Here are 2 ways:
DECLARE @t TABLE (IdName INT, Parent_name VARCHAR(30), Parent_id VARCHAR(30))
INSERT INTO @t
SELECT 1, 'A.B','A'
UNION ALL SELECT 2, 'A.B(1).C','B'
UNION ALL SELECT 3, 'A.B(2).D','B'
UNION ALL SELECT 4, 'A.B.C.D.E','D'
UNION ALL SELECT 5, 'A.B.C.D(1).E','D'
UNION ALL SELECT 6, 'A.B.C.D(2).F','D'
SELECT IdName, Parent_name, Parent_id, parent2
FROM @t
CROSS APPLY (SELECT REVERSE(Parent_name)) a(rname1)
CROSS APPLY (SELECT SUBSTRING(rname1, 1+CHARINDEX('.', rname1), LEN(rname1))) b(rname2)
CROSS APPLY (SELECT REVERSE(SUBSTRING(rname2, 1, CHARINDEX('.', rname2 + '.')-1))) c(parent1)
CROSS APPLY (SELECT SUBSTRING(parent1, 1, CHARINDEX('(', parent1 + '(')-1)) d(parent2)
SELECT IdName, Parent_name, Parent_id, parent2
FROM @t
CROSS APPLY dbo.DelimitedSplit8K(REVERSE(Parent_Name), '.') a
CROSS APPLY (SELECT SUBSTRING(REVERSE(item), 1, CHARINDEX('(', REVERSE(item) + '(')-1)) d(parent2)
WHERE itemnumber = 2
The second uses Jeff Moden's (community) DelimitedSplit8K FUNCTION that can be found here:
http://www.sqlservercentral.com/articles/Tally+Table/72993/
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
September 3, 2012 at 12:07 am
Hi jeff,
Thanks for your information but hear
columns: id,name,parent_name, Parent_id
Hear id is identity column, Name and parent_name having some rows as per showing above post, name column need to compare Parent_name and generate Parent_id using Id (identity ) column.
Note: It's not static i want apply the the code entire table like 100 row or 200 dynamically it needs to work.
September 3, 2012 at 12:19 am
mahi123 (9/3/2012)
Hi jeff,Thanks for your information but hear
columns: id,name,parent_name, Parent_id
Hear id is identity column, Name and parent_name having some rows as per showing above post, name column need to compare Parent_name and generate Parent_id using Id (identity ) column.
Note: It's not static i want apply the the code entire table like 100 row or 200 dynamically it needs to work.
Can you modify the DECLARE TABLE and INSERT/SELECT statements above to show us clearly what is in your table?
Expected results (explicitly) would help also.
What I wrote will work for 1,000,000 rows if that's what you have in your table.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply