开发者

DataSet returns error "Must declare the scalar variable ..."

I'm trying to add a query to my dataset and getting an error "Must declare the scalar variable @searchstr". I find this a little odd because I've used the @variable to pass parameters before no problem, but for whatever reason it fails here.

select DISTINCT g.groupname, 
CASE WHEN s.siteguid = @searchstr THEN 1 ELSE 0 END AS doesitexist
from groups g left outer join sitegroups as sg on g.groupguid = sg.groupguid 
left outer join sites as s on sg.siteguid = s.siteguid

Found it fails in LINQPad as well, so add a little more info, though sadly this still doesn't work in a DataSet. :-( The below works in LINQPad, and SQL Server Management Studio.

declare @searchstr nvarchar(64);
set @searchstr = '21EC2020-3AEA-1069-A2DD-08002B30309D';
select DISTINCT g.groupname, 
CASE WHEN s.siteguid = @searchstr THEN 1开发者_开发百科 ELSE 0 END AS doesitexist
from groups g left outer join sitegroups as sg on g.groupguid = sg.groupguid 
left outer join sites as s on sg.siteguid = s.siteguid

What am I doing wrong that's tripping me up? Suspect WHEN is tripping me up?


I suggest to try this to force the parser to get the parameter

select DISTINCT g.groupname, 
CASE WHEN s.siteguid = @searchstr THEN 1 ELSE 0 END AS doesitexist
from groups g left outer join sitegroups as sg on g.groupguid = sg.groupguid 
left outer join sites as s on sg.siteguid = s.siteguid
WHERE (@searchstr = '' OR 1 = 1)


Perhaps it's not incorrect in the sql, but at another location (Parameter definition, etc.)?


It's probably confused because you're not using the parameter in the WHERE statement. At a glance this doesn't look like something you should be doing at the SQL Server anyway as it doesn't affect the specific rows returned.

Maybe give a bit more info on what you actually want to achieve with this SQL?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜