Hi All,I am running into a strange issue with a query I am trying to write I cannot figure out yet. I have re-written this query about 15 different ways at this point. The issue I am running into now is with the WHERE clause.It should be comparing a NOT Null Int column to an Int variable, and only returning the records from the query where that column equals the variable value:where col = @someValue I know the result set has the value, if I remove the where clause I can see it in the resultset. However adding the clause yields zero records:Declare @SiteId int Declare @ArsYear int = 2014Declare @OrganizationId int = 1510Declare @QuestionIds varchar(max) = '7114,7115'Declare @SurveyInstanceId int SET NOCOUNT ON; If(@SiteId is null) Begin Select @SurveyInstanceId = si.SurveyInstanceId from dbo.SurveyInstance si join dbo.Survey s on (si.SurveyId = s.SurveyId) where si.SiteId is null and si.OrganizationId = @OrganizationId and s.ArsYear = @ArsYear End Else Begin Select @SurveyInstanceId = si.SurveyInstanceId from dbo.SurveyInstance si join dbo.Survey s on (si.SurveyId = s.SurveyId) where si.SiteId = @SiteId and si.OrganizationId = @OrganizationId and s.ArsYear = @ArsYear End Select q.QuestionId, q.QuestionGlobalId, qs.QuestionStatusTypeId, qs.SurveyInstanceId from dbo.SurveyInstanceQuestionStatus qs Join dbo.SplitStrings_CTE(@QuestionIds, ',') qg on(qs.QuestionId = qg.Item) join dbo.Question q on (q.QuestionId = qs.Questionid) where qs.SurveyInstanceId = @SurveyInstanceId
Yields no resultsDeclare @SiteId int Declare @ArsYear int = 2014Declare @OrganizationId int = 1510Declare @QuestionIds varchar(max) = '7114,7115'Declare @SurveyInstanceId int SET NOCOUNT ON; If(@SiteId is null) Begin Select @SurveyInstanceId = si.SurveyInstanceId from dbo.SurveyInstance si join dbo.Survey s on (si.SurveyId = s.SurveyId) where si.SiteId is null and si.OrganizationId = @OrganizationId and s.ArsYear = @ArsYear End Else Begin Select @SurveyInstanceId = si.SurveyInstanceId from dbo.SurveyInstance si join dbo.Survey s on (si.SurveyId = s.SurveyId) where si.SiteId = @SiteId and si.OrganizationId = @OrganizationId and s.ArsYear = @ArsYear End Select q.QuestionId, q.QuestionGlobalId, qs.QuestionStatusTypeId, qs.SurveyInstanceId from dbo.SurveyInstanceQuestionStatus qs Join dbo.SplitStrings_CTE(@QuestionIds, ',') qg on(qs.QuestionId = qg.Item) join dbo.Question q on (q.QuestionId = qs.Questionid)
Yields the full resultset. I have tested by adding aSelect @SurveyInstanceIdline to see the value, and harcoding the value to something I know is in the resultset.Any ideas what could be causing that? Thanks so much in advance.