Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Sql dynamic from table

$
0
0
Hi,

I must generate a dynamic sql from a repository table
 


Level    Table NeedParent    id_Link
--------------------------------------------
1             a           NULL         NULL
2             b           NULL         id_A
3             c             2            id_B
4             d             3            id_C
5             e           NULL         id_D

Rules :
When the NeedsParent IS NULL ==> no parent need  
when NeedsParent IS NOT NULL ==> I have to go all the upper level to the first level with the column

Result : 


select * from a 
select * from b  
select * from c where idB = @idB
select * from d where idB = @idB AND idC = @idC AND idB = @idB
select * from e  


thx for help

DECLARE @TEMP TABLE([Level] INT,[TABLE] CHAR(1),NeedsParent  int ,Id_Link VARCHAR(10))
INSERT INTO @TEMP VALUES(1,'A' ,NULL, 'id_A')
INSERT INTO @TEMP VALUES(2,'B' ,NULL, 'id_B')
INSERT INTO @TEMP VALUES(3,'C' ,2, 'id_C')
INSERT INTO @TEMP VALUES(4,'D' ,3, 'id_D')
INSERT INTO @TEMP VALUES(5,'E' ,NULL, 'id_E')
SELECT * FROM @TEMP


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>