This is a bit confusing because it worked before and I added 1 little change and I get this error message in my web application. (I do have other cases, I just took out the other cases for simplicity)
Original code that worked before:
ALTER PROCEDURE [dbo].[GetRoles]
(@reportid Decimal, @dom varchar(10))
AS
DECLARE @sql varchar(2000)
SELECT @sql =
Case @reportid
WHEN 1 THEN
'select u.id as userId, u.domain, u.isAdmin, u.email, u.canReport, a.[site], a.bldgNum, a.dataCenterNum, l.shortName, l.[description], a.canApprove, a.canComplete
from locAdmin a
inner join location l on (a.site=l.site and a.bldgNum = l.bldgNum and a.dataCenterNum = l.dataCenterNum)
right outer join [user] u on u.id=a.userId and u.domain=a.domain
where u.isAdmin = 1'
End
EXEC (@sql)
The only change I did was adding
and u.domain = @dom'
after where u.isAdmin = 1′ at the end so it looks like this
where u.isAdmin = 1 and u.domain = @dom'
You need to add
@domas a parameter when you execute your dynamic SQL.Replace
EXEC (@sql)withexec sp_executesql @sql, N'@dom varchar(10)', @domand changeDECLARE @sql varchar(2000)toDECLARE @sql nvarchar(2000).