Select records from multiple Tables using Join in SQL Server

Last Reply 26 days ago By pandeyism

Posted 27 days ago

Hi i have 4 tables . i need to join all four tables and output. images of 4 tables and o/p is attached below.

Please Help

https://imgur.com/a/NYZ0Ag5

Posted 26 days ago Modified on 26 days ago

Hi suhaas121,

Refer below sample query.

SQL

DECLARE @tblBookNode_Amm AS TABLE(iNodeId INT, iBookId INT, iChapterId INT, iModuleId INT,vcFilePath VARCHAR(MAX), FileUploadedBy VARCHAR(20))
INSERT INTO @tblBookNode_Amm VALUES(1,1,1,1,'xjkjjksf65465465dsfhjsdh','Admin')
INSERT INTO @tblBookNode_Amm VALUES(2,1,1,2,'xjkjjksf65465465dsfhjsdh','Admin')
DECLARE @tblBookNode_Ipc AS TABLE(iNodeId INT, iBookId INT, iChapterId INT, iModuleId INT,vcFilePath VARCHAR(MAX), FileUploadedBy VARCHAR(20))
INSERT INTO @tblBookNode_Ipc VALUES(1,2,2,3,'xjkjjksf65465465dsfhjsdh','Admin')
INSERT INTO @tblBookNode_Ipc VALUES(2,2,2,4,'xjkjjksf65465465dsfhjsdh','Admin')
DECLARE @tblChapter AS TABLE(iBookId INT, iChapterId INT, vcChapterName VARCHAR(MAX))
INSERT INTO @tblChapter VALUES(1,1,'Chapter 00')
INSERT INTO @tblChapter VALUES(2,2,'IPC')
DECLARE @tblModule AS TABLE(iModuleId INT, iChapterId INT, vcModuleTitle VARCHAR(MAX))
INSERT INTO @tblModule VALUES(1,1,'XML and illustrations')
INSERT INTO @tblModule VALUES(2,1,'dummy')
INSERT INTO @tblModule VALUES(3,2,'Sample IPC')
INSERT INTO @tblModule VALUES(4,2,'Sample IPC2')
SELECT mod.vcModuleTitle,Ipc.iNodeId, Amm.iNodeId,Amm.vcFilePath,Amm.iChapterId, Ipc.vcFilePath,Ipc.iChapterId 
FROM @tblBookNode_Amm Amm
INNER JOIN @tblBookNode_Ipc Ipc ON Amm.iNodeId = Ipc.iNodeId
INNER JOIN @tblChapter chap on Ipc.iChapterId = chap.iChapterId
INNER JOIN @tblModule mod on Ipc.iModuleId = mod.iModuleId