Solved

database server name as a parameter for a stored procedure

Posted on 2011-09-06
1
184 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
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now