?
Solved

database server name as a parameter for a stored procedure

Posted on 2011-09-06
1
Medium Priority
?
192 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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

801 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