Uwak Peter

Uwak Peter

  • 1.6k
  • 96
  • 7k

How do i pass database name as parameter to stored procedure

Jun 3 2014 8:54 AM
Hello Everyone,


I am trying pass database name as parameter to a stored procedure, but iam having issues doing it, please check my script, and let me know if i am missing anything:


set quoted_identifier off
go
ALTER procedure [dbo].[usp_accessdb] 
@SchoolAngelPortalDB varchar(128),
@GSC_NewSchoolCore varchar(128),
@schoolName varchar(50)= NULL
as 
declare @query varchar(1000)
set @query = 'SELECT pp.title, pp.surname,pp.firstname,pp.phone,pp.email,rcd.reg_code,pp.genCode
    FROM '+@SchoolAngelPortalDB+'.dbo.primaryparent pp
    LEFT JOIN '+ @GSC_NewSchoolCore +'.dbo.registration_codes rcd ON pp.genCode = rcd.reg_code
WHERE Sch.Name='+@schoolName
   
exec (@query)
go


When trying to execute the procedure,using the below script:


[usp_accessdb]"SchoolAngelPortalDB","GSC_NewSchoolCore","Greenwood House School"

 I have this error:

Incorrect syntax near 'House'.

Please any assistance will be appreciated.



Answers (1)