Solved

database server name as a parameter for a stored procedure

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

16 Experts available now in Live!

Get 1:1 Help Now