betul Posted May 9, 2009 Share Posted May 9, 2009 Hi everyone, Below is my procedure for the Work and Files tables. It searches the records matching the given parameters. However, if a Work has a few Files, the same "work record" is repeated as many as the number of files. How can I stop it? Thanks. set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[SearchWork] @StartDate DateTime,@EndDate DateTime,@Type int,@FileName nvarchar, AS SELECT * FROM Work (nolock) w LEFT OUTER JOIN Files f ON w.WorkId=f.WorkId AND (f.FileName LIKE '%' + @FileName + '%' OR f.FileTitle LIKE '%' + @FileName+ '%' OR @FileName is NULL)WHERE ((w.WorkOpenDate BETWEEN @StartDate AND @EndDate) OR @StartDate is NULL OR @EndDate is NULL)and (w.WorkType=@Type OR @Type is null OR @Type=0)--and (w.WorkId in (SELECT WorkId FROM files f WHERE f.FileName LIKE '%' + @FileName+ '%' OR f.FileTitle LIKE '%' + @FileName + '%' OR @FileName is NULL)) Link to comment Share on other sites More sharing options...
betul Posted May 10, 2009 Author Share Posted May 10, 2009 I changed the select statement as; SELECT * FROM Work (nolock) w LEFT OUTER JOIN (SELECT DISTINCT WorkId FROM Files WHERE FileName LIKE '%' + @FileName + '%' OR FileTitle LIKE '%' + @FileName+ '%' OR @FileName is NULL) AS f ON w.WorkId=f.WorkId and it works without repeating the rows. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.