March 14, 2012 at 8:50 am
Hi
This is my table(dynamic)
id type dynamic_name dynamic_table_id
2 2 View Art Work 1072,1310
3 2 Update Art Work 1072
4 2 View Module 1090
Here i want compare these table_id in tables table
Tables table structure
table_id table_name
1072 Print Ads
1090 Modules
1310 Print Adss
I want compare to this tables table table_id and dynamic table table_id and to select the table_name in tables table.
dynamic_name , dynamic_table_id , table_name
View Art Work 1072,1310 Print Ads,Print Adss
Update Art Work 1072 Print Ads
View Module 1090 Modules
This is expected output.
Share Knowledge team
March 14, 2012 at 9:26 am
DECLARE @dynamic TABLE (id INT PRIMARY KEY, type INT, dynamic_name VARCHAR(100), dynamic_table_id VARCHAR(100))
DECLARE @tables TABLE (table_id INT PRIMARY KEY, table_name VARCHAR(100))
INSERT INTO
VALUES
(2, 2, 'View Art Work', '1072, 1310'),
(3, 2, 'Update Art Work', '1072'),
(4, 2, 'View Module', '1090')
INSERT INTO
@tables
VALUES
(1072, 'Print Ads'),
(1090, 'Modules'),
(1310, 'Print Adss')
SELECT
D.dynamic_name, D.dynamic_table_id,
STUFF(
(
SELECT
', ' + T.table_name
FROM
@tables T
WHERE
CHARINDEX(',' + CAST(T.table_id AS VARCHAR(100)) + ',',
',' + REPLACE(D.dynamic_table_id, ' ', '') + ',') > 0
FOR XML PATH(''), TYPE
).value('./text()[1]', 'VARCHAR(MAX)'), 1, 2, '') table_name
FROM
@dynamic D
March 16, 2012 at 9:20 am
Hi ;
Nice post , I later to see the post and any way good work.
Thanks;
Faijurrahuman.A
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply