0
votes
SET @SQLScript = 'SELECT b.name
FROM tblBrand b 
JOIN tblStore s ON b.PK_BrandID = s.FK_BrandID
JOIN tblCustomReportTemp  CT on b.PK_BrandID = CT.BrandID
WHERE b.Active =1 '

if(@reportname is not null)
        set @SQLScript = @SQLScript + ' AND CT.ReportName = @reportname'

    if(@username is not null)
        set @SQLScript = @SQLScript + ' AND CT.UserName = @username' 

The above lines throw:

An expression of non-boolean type specified in a context where a condition is expected, near 'Repo'.

1
Your code is missing something here.. SELECT b.name, FROM tblBrand b this comma should be removed or add new parameter here..MarmiK
Can you please post the exec call for SqlScript ? because you're not planting the values of reportName and userName into the sql, but the rather as parameters.Amir Pelled

1 Answers

0
votes

First of all be sure you declared your @SQLScript variable as varchar(max).

Also I don't know if you are using sp_executesql to run that query but if you don't, you have to use concat function or concat operator to create your query and add simple quote for each parameter that are in varchar format.

SET @SQLScript = 'SELECT b.name
FROM tblBrand b 
JOIN tblStore s ON b.PK_BrandID = s.FK_BrandID
JOIN tblCustomReportTemp  CT on b.PK_BrandID = CT.BrandID
WHERE b.Active =1 '

if(@reportname is not null)
        set @SQLScript = @SQLScript + ' AND CT.ReportName = ''' + @reportname + ''''

    if(@username is not null)
        set @SQLScript = @SQLScript + ' AND CT.UserName = ''' + @username + '''' 

Should works.