I've been trying to join another table with the table name matching a value in a column from another table.
If I hard-code the table name to a matching table like __gun
it works fine but I cannot get the col value to be used + concat underscores to the beginning.
The problem lies in the left join
here:
left join CONCAT('__', b.related_table) c on b.related_id = c.id
I need the related_table column to be used in the join. With __ before it.
Attempts:
SELECT a.*, c.*, b.equipable, b.related_table FROM inventory a
inner join items b on a.item_id = b.id
left join CONCAT('__', b.related_table) c on b.related_id = c.id
WHERE 1=1
and a.id = :inventory_id
and a.user_id = :user_id
SELECT a.*, c.*, b.equipable, b.related_table FROM inventory a
inner join items b on a.item_id = b.id
left join '__'+b.related_table c on b.related_id = c.id
WHERE 1=1
and a.id = :inventory_id
and a.user_id = :user_id
SELECT a.*, c.*, b.equipable, b.related_table FROM inventory a
inner join items b on a.item_id = b.id
left join "__"+Cast(b.related_table as nvarchar(4000)) c on b.related_id = c.id
WHERE 1=1
and a.id = :inventory_id
and a.user_id = :user_id
Thank you for considering my question