i have a sql query and i am using distinct statement
CREATE proc SProc_SelectPhotographersByLocation
@locationID varchar(500)
as
begin
DECLARE @TEST varchar(1000)
DECLARE @SQLQuery AS NVARCHAR(1000)
SET @TEST = @locationID
SET @SQLQuery = 'select distinct ContributerSubCategoryMapping.ContributorID, PhotographerContributors_tbl.* from ContributerSubCategoryMapping
left outer join PhotographerContributors_tbl on PhotographerContributors_tbl.ContributorId=ContributerSubCategoryMapping.ContributorID
left outer join tbl_City on tbl_City.CityID=PhotographerContributors_tbl.Location
where
PhotographerContributors_tbl.Location IN('+ @locationID +') and PhotographerContributors_tbl.IsActive=''1'' order by Newid()'
EXECUTE(@SQLQuery)
end
i am getting error on the query when i use NEWID() on that query.
Error is
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
please help me for this issue
NEWID()does not appear in this query, you should post the query that actually causes the error. 3. The error message seems fairly self explanatory, you cannot use distinct, and order by NEWID() without also selectingNEWID(), which will make the distinct meaningless, instead move your query into a subquery with select distinct, then order this subquery byNEWID(). 4. Why are you even ordering byNEWID()