Overview
In this article, we will talk about how we can get the database name using Stored Procedure based on the Customer ID and pass that Database Name to another stored procedure as a Parameter to load the records from that particular dataset using Functions.
So, now let’s get started!
Real-Life Scenario
I have the following dataset mapping table in the Database named “Mapping”.
In the above database, I have a stored procedure which will find the name of the database based on the Customer ID.
- USE [Mapping]
- GO
- /****** Object: StoredProcedure [dbo].[GetCustomerInformation] Script Date: 11/4/2019 6:53:10 AM ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- -- =============================================
- -- Author: <Author,,Name>
- -- Create date: <Create Date,,>
- -- Description: <Description,,>
- -- =============================================
- Create PROCEDURE [dbo].[GetCustomerInformation]
- -- Add the parameters for the stored procedure here
- @CID nvarchar(50)
- AS
- BEGIN
- -- SET NOCOUNT ON added to prevent extra result sets from
- -- interfering with SELECT statements.
- SET NOCOUNT ON;
- -- Insert statements for procedure here
- SELECT [Database] from [Mapping].[dbo].[MappingID] where [CID] = @CID
- END
If we pass “1” as a CustomerID, it will return the database name as “A”. Similarly, for “2” it will return “B”.
Below is the schema of the database table “TSICustomers” in database A and database B with a different value.


Download Code
The code can be downloaded from the GitHub.
Now, let’s get started!
Step 1: Create a Dynamic Parameter for Customer ID to get Database Name using Stored Procedure
1. Create SQL Server Connection using Get Data.
2. Add the Server name, database name and Execute Stored Procedure in a Static Manner
Below is the code to execute the Stored Procedure
- EXEC GetCustomerInformation @CID = '1'

3. Change “Query1” name as “Database”.

4. Click on Close and Apply and reopen Query Editor.
5. From “Manage Parameters”, select “New Parameter”.

6. Add the following information.
- Name = Name of the Parameter
- Type = Datatype of the parameter
- Suggested Values = Select “Any Values”
- Current Value = Pass any Static Value

















Join the conversation! Your thoughts help the community grow.