Solved

database server name as a parameter for a stored procedure

Posted on 2011-09-06
1
190 Views
Last Modified: 2012-05-12
I want to create a stored proc that takes three paramaters (DBnamer name, ParentId and MNumberList) as in the attached code, but I keep getting error:

The DB server that I will use is added to the linked servers. The insert sql works if I use the actual values for servername.dbname, ParentID, and MNumber List instead of the variables.

Error Could not find server '@DbName' in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers.

I am not sure if I am using any of the parameter variables correctly.

Please advise corrections.

Thanks.
CREATE PROCEDURE CDExtract_PopulateME 	
	 @DbName Varchar(50), 
	 @ParentID INT ,
	 @MNumberList Varchar(50)
AS
BEGIN
	INSERT into mySchema.CData_Extract_ME
(MemberId, ClientMemberId, Enroll_Start, Enroll_End, HICNum, MNumber, IPA, LPO, ParentID)
select  A.memberid as MemberID,
		B.memberid as ClientMemberID,
		B.Enroll_Start,
		B.Enroll_End,
		B.HICNum,
		B.MNumber,		
		NULL,
		NULL,
		@ParentID as ParentID  
from pd.dbo.member A (nolock)
join [@DbName].dbo.ME B on A.DisplayKey = B.hicnum
left join (select max(pcp_end_date) as pcp_end_date,memberid,network,subnetwork from pd.dbo.m_pcp group by memberid, network,subnetwork) C on A.memberid = C.memberid
where B.mnumber in (@MNumberList)
END
GO

Open in new window


The following insert SQL works, i need to create the above sproc to run this query with parameters.
INSERT into mySchema.CData_Extract_ME
(MemberId, ClientMemberId, Enroll_Start, Enroll_End, HICNum, MNumber,  IPA, LPO,ParentID)
select  A.memberid as MemberID,
		B.memberid as ClientMemberID,
		B.Enroll_Start,
		B.Enroll_End,
		B.HICNum,
		B.MNumber,		
		NULL,
		NULL,
		'211' as parentid 
pd.dbo.member A (nolock)
join [alph-clus-03\sql3].Excellus.dbo.ME B on A.DisplayKey = B.hicnum
left join (select max(pcp_end_date) as pcp_end_date,memberid,network,subnetwork from prod.dbo.m_pcp group by memberid, network,subnetwork) C on A.memberid = C.memberid
where B.mnumber in ('H1234','H5678')

Open in new window


0
Comment
Question by:patd1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 36491699
You need to use dynamic sql. Something like this:

CREATE PROCEDURE CDExtract_PopulateME 	
	 @DbName Varchar(50), 
	 @ParentID INT ,
	 @MNumberList Varchar(50)
AS
BEGIN
        DECLARE @sql varchar(4000)
        SET @sql = '
	INSERT into mySchema.CData_Extract_ME
(MemberId, ClientMemberId, Enroll_Start, Enroll_End, HICNum, MNumber, IPA, LPO, ParentID)
select  A.memberid as MemberID,
		B.memberid as ClientMemberID,
		B.Enroll_Start,
		B.Enroll_End,
		B.HICNum,
		B.MNumber,		
		NULL,
		NULL,
		' + CAST(@ParentID AS VARCHAR(50)) + ' as ParentID  
from pd.dbo.member A (nolock)
join [' + @DbName + '].dbo.ME B on A.DisplayKey = B.hicnum
left join (select max(pcp_end_date) as pcp_end_date,memberid,network,subnetwork from pd.dbo.m_pcp group by memberid, network,subnetwork) C on A.memberid = C.memberid
where B.mnumber in (' + @MNumberList + ')'

EXEC (@sql)

END
GO

Open in new window


I hope this helps.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question