title: Need a query to view certain columns of multiple tables and stored procedure in Excel sheet
body: <p>Need a query to view certain columns of multiple tables and stored procedures in a single Excel sheet as a table, which is connected to the database <code>IDS_SAF</code> in SQL Server 2014. </p>
  
  <p>I have two question here, </p>
  
  <ol>
  <li><p>How to write a stored procedure to generate the output as stated in the screenshot.</p></li>
  <li><p>How to link that stored procedure to the Excel sheet, so that I get all the data based on the stored procedure written.</p></li>
  </ol>
  
  <p>The <code>Select</code> query for each column are provided below. I don't know how to join those separate statements to make a single query. The application used is Excel 2016.</p>
  
  <p>The columns in the screenshot below are the ones I want in Excel:</p>
  
  <p><a href="https://i.stack.imgur.com/RKqYe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RKqYe.png" alt="These are the columns I want together"></a>.. </p>
  
  <p>As said, I'll attach the SQL select query for each of the columns. </p>
  
  <pre><code>SELECT 
      [VV_Code],
      [Vessel_Code],
      [Arrival_Date],
      [Terminal_code]
  FROM
      [IDS_SAF].[dbo].[Vessel_Voyage]
  </code></pre>
  
  <p>From the <code>[dbo].[Vessel_Voyage]</code> table, we have <code>Vessel_Code</code> instead of <code>Vessel_Name</code>. There is a separate table for <code>Vessel_Name</code> which is based on the <code>Vessel_Code</code>. But we need to get only the <code>Vessel_Name</code> column in the Excel. </p>
  
  <pre><code>SELECT 
      [Vessel_Code],
      [Vessel_Name]
  FROM
      [IDS_SAF].[dbo].[Vessel]
  </code></pre>
  
  <p>The select query for <code>VIR NOS</code> is listed below,</p>
  
  <pre><code>SELECT 
      [VV_CODE],
      [VIR_NO]
  FROM
      [IDS_SAF].[dbo].[IGM]
  </code></pre>
  
  <p>The total index column is defined to be <code>BL_Count</code> and it doesn't have a table but a stored procedure. The code is below,</p>
  
  <pre><code>USE [IDS_SAF]
  GO
  
  SET ANSI_NULLS ON
  GO
  
  SET QUOTED_IDENTIFIER OFF
  GO
  
  
  CREATE PROC [dbo].[BL_COUNT]
      @VV_CODE VARCHAR(20)
  AS
      SELECT
          'Total Bill Of Ladings' as 'BL Count',
          COUNT(BL_NO) as 'Total BLs'
      FROM
          BL_DATA
      WHERE
          VV_CODE = @VV_CODE AND
          ISNULL(UNUSED_FLAG, '') &lt;&gt; '1'
  </code></pre>
  
  <p>Same way, the Imp containers column have two sections of count details in them based on the container size (20FT and 40FT). The Total Number of Containers must be the total of these both. We have stored procedure for this as well.</p>
  
  <pre><code>USE [IDS_SAF]
  GO
  
  SET ANSI_NULLS ON
  GO
  
  SET QUOTED_IDENTIFIER OFF
  GO
  
  
  CREATE PROC [dbo].[BL_CONTAINER_GET_NO_OF_CONTAINERS]
      @VV_CODE VARCHAR(10),
      @BL_NO VARCHAR(30)
  AS
      SELECT
          CONTAINER_SIZE_DESCRIPTION,
          (SELECT COUNT(CONTAINER_SIZE_CODE) FROM BL_CONTAINER
          WHERE BL_NO = BC.BL_NO AND CONTAINER_SIZE_CODE = BC.CONTAINER_SIZE_CODE) AS SIZE_COUNT
  
      FROM
          BL_CONTAINER BC, CONTAINER_SIZE CS
      WHERE
  
      BL_NO = @BL_NO AND
      VV_CODE = @VV_CODE AND
      BC.CONTAINER_SIZE_CODE = CS.CONTAINER_SIZE_CODE
      GROUP BY CONTAINER_SIZE_DESCRIPTION, BL_NO, BC.CONTAINER_SIZE_CODE
  
  GO
  </code></pre>
  
  <p>Based on these select statements of tables and stored procedures, I am in need of a query which helps in getting these columns together as a table. </p>
  
  <p>Kindly help me please. Thanks in advance..!!</p>