how to make dynamic sql select query for the select query with condition 3 table with ?http://forums.asp.net/t/1807336.aspx/1?how+to+make+dynamic+sql+select+query+for+the+select+query+with+condition+3+table+with+Mon, 28 May 2012 12:29:20 -040018073364996775http://forums.asp.net/p/1807336/4996775.aspx/1?how+to+make+dynamic+sql+select+query+for+the+select+query+with+condition+3+table+with+how to make dynamic sql select query for the select query with condition 3 table with ? <p>here i used &nbsp;3 tables in that &nbsp;3 table i want to search a word whether it presented &nbsp;in the columns of the first table &nbsp;and &nbsp;similarly i just want to &nbsp;search conditions &nbsp;so &nbsp;i used &nbsp; cross split function also and another scalar function also included</p> <pre class="prettyprint">ALTER PROCEDURE [dbo].[SP_data] @CategoryId int, @Location [varchar](120), @VALUES varchar(8000), @AdType [char](1) AS BEGIN IF (@CategoryId&lt;&gt;'' and @AdType&lt;&gt;'' and @Location&lt;&gt;''and @VALUES&lt;&gt;'') BEGIN IF EXISTS ( SELECT * FROM sys.tables WHERE name LIKE '#solution1%') DROP TABLE #solution1 create table #solution1 ( ClassifiedId [int], MemberId [int], SubCategoryId [int], Email [varchar](120), City [varchar](120), Locality [varchar](120), AdType [char](1), AdOwnerType [char](1), OfferCondition [char](1), Price [decimal](18, 2), Title [varchar](100) , [Description] [varchar](1000), RelatedKeywords [varchar](150) , MobileNo [varchar](20), Weightage [smallint], CreateDate [datetime], IsActive [int], IsAuthorized [int], ClassifiedAdditionalDataId1 [varchar](max), ClassifiedAdditionalDataId2 [varchar](max), ClassifiedImageId [int], [FileName] [varchar](100), DATALEVEL [int] ) BEGIN insert into #solution1( ClassifiedId ,MemberId ,SubCategoryId ,Email ,City ,Locality ,AdType ,AdOwnerType ,OfferCondition ,Price ,Title ,[Description] ,RelatedKeywords ,MobileNo ,Weightage ,CreateDate ,IsActive ,IsAuthorized ,ClassifiedAdditionalDataId1 ,ClassifiedAdditionalDataId2 ,ClassifiedImageId ,[FILENAME] ) SELECT DISTINCT(csf.ClassifiedId), --csf.ClassifiedId, csf.MemberId, csf.SubCategoryId, csf.Email, csf.City, csf.Locality, csf.AdType, csf.AdOwnerType, csf.OfferCondition, csf.Price, csf.Title, csf.[Description], csf.RelatedKeywords, csf.MobileNo, csf.Weightage, csf.CreateDate, csf.IsActive, csf.IsAuthorized, (select dbo.FN_GetDataLevel1Value(csf.ClassifiedId))as'ClassifiedAdditionalData1', (select dbo.FN_GetDataLevel2Value(csf.ClassifiedId))as'ClassifiedAdditionalData2', (select TOP 1 ClassifiedImageId from tblClassifiedImage where ClassifiedId=CFI.ClassifiedId)as'ClassifiedImageId', (select TOP 1 [FILENAME] from tblClassifiedImage where ClassifiedId=CFI.ClassifiedId) as'[FILENAME]' FROM tblClassified csf LEFT OUTER JOIN tblAdSubCategory sb ON sb.SubCategoryId = csf.SubCategoryId LEFT OUTER JOIN tblClassifiedImage CFI ON csf.ClassifiedId = CFI.ClassifiedId cross apply dbo.characterSplit(@values, '') ds --Replace ',' with your actual delimter where csf.Email LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.City LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.Locality LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' --or --csf.AdType LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.AdOwnerType LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.OfferCondition LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.Price LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.Title LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.Description LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.RelatedKeywords LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.MobileNo LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.Weightage LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' select s.ClassifiedId ,s.MemberId ,s.SubCategoryId ,s.Email ,s.City ,s.Locality ,s.AdType ,s.AdOwnerType ,s.OfferCondition ,s.Price ,s.Title ,s.Description ,s.RelatedKeywords ,s.MobileNo ,s.Weightage ,s.CreateDate ,s.IsActive ,s.IsAuthorized ,s.ClassifiedAdditionalDataId1 ,s.ClassifiedAdditionalDataId2 ,s.ClassifiedImageId ,s.FileName from #solution1 s LEFT OUTER JOIN tblAdSubCategory sb ON sb.SubCategoryId = s.SubCategoryId LEFT OUTER JOIN tblAdCategory ac ON ac.CategoryId =sb.CategoryId --inner join -- tblClassified csf -- on csf.ClassifiedId=t.ClassifiedId where s.Locality=@Location and s.AdType=@AdType and sb.CategoryId= @CategoryId drop table #solution1 end end</pre> <p>similarly i want to check 15 condition in else if state ment<br> Else if (@CategoryId&lt;&gt;'' and @AdType&lt;&gt;'' and @VALUES&lt;&gt;'')<br> Else if (@CategoryId&lt;&gt;'' and @VALUES&lt;&gt;'')</p> <p>Else if (@CategoryId&lt;&gt;'' and @AdType&lt;&gt;'' )</p> <p>Else if (@Location&lt;&gt;''and @VALUES&lt;&gt;'')</p> <p>in the same proc</p> <p>i just tried a sample</p> <p>DECLARE @SQL as varchar(max)<br> <br> SET @SQL =&nbsp;</p> <p>DECLARE @SQL as varchar(max)<br> <br> SET @SQL ='SELECT<br> DISTINCT(csf.ClassifiedId),<br> csf.ClassifiedId,<br> &nbsp;FROM <br> tblClassified csf</p> <p>LEFT OUTER JOIN&nbsp;</p> <p>tblClassifiedImage CFI ON csf.ClassifiedId = CFI.ClassifiedId'</p> <p><br> <br> IF @AdSpaceInfo is not null<br> BEGIN<br> SET @SQL = @SQL &#43; ' where csf.ClassifiedId&nbsp;= ' &#43; CAST(@ClassifiedId as varchar(5))<br> END<br> <br> PRINT @SQL<br> <br> EXECUTE (@SQL)</p> 2012-05-25T06:08:17-04:004996831http://forums.asp.net/p/1807336/4996831.aspx/1?Re+how+to+make+dynamic+sql+select+query+for+the+select+query+with+condition+3+table+with+Re: how to make dynamic sql select query for the select query with condition 3 table with ? <p>HI, Defined Default values to parameter say NULL &amp; follow below process..</p> <ul> <li>Define Parameters Default Values in SP </li><li>Check Condition for NULL OR EMPTY Space on front end application, if condition match, don't pass parameter to call store procedure </li><li>Check NULL Condition (Default condtion first to execute select query. </li></ul> <p>This will give you exact results without using if else condtion..</p> <p>check below</p> <pre class="prettyprint">ALTER PROCEDURE [dbo].[SP_data] ( @CategoryId int=NULL, @Location [varchar](120)=NULL, @VALUES varchar(8000)=NULL, @AdType [char](1)=NULL ) AS IF ISNULL(@values,'')&lt;&gt;'' BEGIN IF EXISTS ( SELECT * FROM sys.tables WHERE name LIKE '#solution1%' ) DROP TABLE #solution1 create table #solution1 ( ClassifiedId [int],MemberId [int],SubCategoryId [int],Email [varchar](120), City [varchar](120),Locality [varchar](120),AdType [char](1),AdOwnerType [char](1), OfferCondition [char](1),Price [decimal](18, 2),Title [varchar](100) ,[Description] [varchar](1000), RelatedKeywords [varchar](150) ,MobileNo [varchar](20),Weightage [smallint],CreateDate [datetime], IsActive [int],IsAuthorized [int],ClassifiedAdditionalDataId1 [varchar](max),ClassifiedAdditionalDataId2 [varchar](max), ClassifiedImageId [int],[FileName] [varchar](100),DATALEVEL [int] ) insert into #solution1 ( ClassifiedId,MemberId,SubCategoryId,Email,City,Locality,AdType,AdOwnerType,OfferCondition,Price,Title, [Description],RelatedKeywords,MobileNo,Weightage,CreateDate,IsActive,IsAuthorized,ClassifiedAdditionalDataId1, ClassifiedAdditionalDataId2,ClassifiedImageId,[FILENAME] ) SELECT DISTINCT(csf.ClassifiedId),--csf.ClassifiedId, csf.MemberId,csf.SubCategoryId,csf.Email,csf.City,csf.Locality,csf.AdType,csf.AdOwnerType,csf.OfferCondition, csf.Price,csf.Title,csf.[Description],csf.RelatedKeywords,csf.MobileNo,csf.Weightage,csf.CreateDate, csf.IsActive,csf.IsAuthorized, (select dbo.FN_GetDataLevel1Value(csf.ClassifiedId))as'ClassifiedAdditionalData1', (select dbo.FN_GetDataLevel2Value(csf.ClassifiedId))as'ClassifiedAdditionalData2', (select TOP 1 ClassifiedImageId from tblClassifiedImage where ClassifiedId=CFI.ClassifiedId)as'ClassifiedImageId', (select TOP 1 [FILENAME] from tblClassifiedImage where ClassifiedId=CFI.ClassifiedId) as'[FILENAME]' FROM tblClassified csf LEFT OUTER JOIN tblAdSubCategory sb ON sb.SubCategoryId = csf.SubCategoryId LEFT OUTER JOIN tblClassifiedImage CFI ON csf.ClassifiedId = CFI.ClassifiedId cross apply dbo.characterSplit(@values, '') ds --Replace ',' with your actual delimter where csf.Email LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.City LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.Locality LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' --or csf.AdType LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.AdOwnerType LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.OfferCondition LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.Price LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.Title LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.Description LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.RelatedKeywords LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.MobileNo LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.Weightage LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' select s.ClassifiedId,s.MemberId,s.SubCategoryId,s.Email,s.City,s.Locality,s.AdType, s.AdOwnerType,s.OfferCondition,s.Price,s.Title,s.Description,s.RelatedKeywords, s.MobileNo,s.Weightage,s.CreateDate,s.IsActive,s.IsAuthorized, s.ClassifiedAdditionalDataId1,s.ClassifiedAdditionalDataId2,s.ClassifiedImageId, s.FileName from #solution1 s LEFT OUTER JOIN tblAdSubCategory sb ON sb.SubCategoryId = s.SubCategoryId LEFT OUTER JOIN tblAdCategory ac ON ac.CategoryId =sb.CategoryId --inner join tblClassified csf on csf.ClassifiedId=t.ClassifiedId where (@Location IS NULL OR s.Locality=@Location) and (@AdType IS NULL OR s.AdType=@AdType) and (@CategoryId IS NULL OR sb.CategoryId= @CategoryId) drop table #solution1 END</pre> <p><br> <br> </p> 2012-05-25T06:49:05-04:004999131http://forums.asp.net/p/1807336/4999131.aspx/1?Re+how+to+make+dynamic+sql+select+query+for+the+select+query+with+condition+3+table+with+Re: how to make dynamic sql select query for the select query with condition 3 table with ? <p>i am &nbsp;not getting the &nbsp;proper output &nbsp;from ur &nbsp;query &nbsp;plz tell some other ways</p> 2012-05-27T08:32:53-04:004999747http://forums.asp.net/p/1807336/4999747.aspx/1?Re+how+to+make+dynamic+sql+select+query+for+the+select+query+with+condition+3+table+with+Re: how to make dynamic sql select query for the select query with condition 3 table with ? <p></p> <blockquote><span class="icon-blockquote"></span> <h4>sivaganesh1234</h4> <p></p> <p>i am &nbsp;not getting the &nbsp;proper output &nbsp;from ur &nbsp;query &nbsp;plz tell some other ways</p> <p></p> </blockquote> <p></p> <p>Can you show us some data &amp; Expected output?</p> <p>Without knowing this, noone can judge regarding selectquery</p> 2012-05-28T05:14:35-04:004999842http://forums.asp.net/p/1807336/4999842.aspx/1?Re+how+to+make+dynamic+sql+select+query+for+the+select+query+with+condition+3+table+with+Re: how to make dynamic sql select query for the select query with condition 3 table with ? <pre class="prettyprint">/* EXEC [dbo].[SP_data] @CategoryId=N'247', @VALUES = N'Agra chennai numgambkm', @Location=N'chennai', @AdType =N'' */ ALTER PROCEDURE [dbo].[SP_data] ( @CategoryId int=NULL, @Location [varchar](120)=NULL, @VALUES varchar(8000)=NULL, @AdType [char](1)=NULL ) AS IF ISNULL(@values,'')&lt;&gt;'' BEGIN IF EXISTS ( SELECT * FROM sys.tables WHERE name LIKE '#solution1%' ) DROP TABLE #solution1 create table #solution1 ( ClassifiedId [int],MemberId [int],SubCategoryId [int],Email [varchar](120), City [varchar](120),Locality [varchar](120),AdType [char](1),AdOwnerType [char](1), OfferCondition [char](1),Price [decimal](18, 2),Title [varchar](100) ,[Description] [varchar](1000), RelatedKeywords [varchar](150) ,MobileNo [varchar](20),Weightage [smallint],CreateDate [datetime], IsActive [int],IsAuthorized [int],ClassifiedAdditionalDataId1 [varchar](max),ClassifiedAdditionalDataId2 [varchar](max), ClassifiedImageId [int],[FileName] [varchar](100),DATALEVEL [int] ) BEGIN insert into #solution1 ( ClassifiedId,MemberId,SubCategoryId,Email,City,Locality,AdType,AdOwnerType,OfferCondition,Price,Title, [Description],RelatedKeywords,MobileNo,Weightage,CreateDate,IsActive,IsAuthorized,ClassifiedAdditionalDataId1, ClassifiedAdditionalDataId2,ClassifiedImageId,[FILENAME] ) SELECT DISTINCT(csf.ClassifiedId),--csf.ClassifiedId, csf.MemberId,csf.SubCategoryId,csf.Email,csf.City,csf.Locality,csf.AdType,csf.AdOwnerType,csf.OfferCondition, csf.Price,csf.Title,csf.[Description],csf.RelatedKeywords,csf.MobileNo,csf.Weightage,csf.CreateDate, csf.IsActive,csf.IsAuthorized, (select dbo.FN_GetDataLevel1Value(csf.ClassifiedId))as'ClassifiedAdditionalData1', (select dbo.FN_GetDataLevel2Value(csf.ClassifiedId))as'ClassifiedAdditionalData2', (select TOP 1 ClassifiedImageId from tblClassifiedImage where ClassifiedId=CFI.ClassifiedId)as'ClassifiedImageId', (select TOP 1 [FILENAME] from tblClassifiedImage where ClassifiedId=CFI.ClassifiedId) as'[FILENAME]' FROM tblClassified csf LEFT OUTER JOIN tblAdSubCategory sb ON sb.SubCategoryId = csf.SubCategoryId LEFT OUTER JOIN tblAdCategory ac ON ac.CategoryId =sb.CategoryId LEFT OUTER JOIN tblClassifiedAdditionalData CAD ON CAD.ClassifiedId =csf.ClassifiedId LEFT OUTER JOIN tblSubCategoryAdditionalData SAD ON SAD.SubCategoryAdditionalDataId=CAD.SubCategoryAdditionalDataId LEFT OUTER JOIN tblClassifiedImage CFI ON csf.ClassifiedId = CFI.ClassifiedId cross apply dbo.characterSplit(@values, '') ds --Replace ',' with your actual delimter where csf.Email LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.City LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.Locality LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' --or csf.AdType LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.AdOwnerType LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.OfferCondition LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.Price LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.Title LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.Description LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.RelatedKeywords LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.MobileNo LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.Weightage LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' select s.ClassifiedId,s.MemberId,s.SubCategoryId,s.Email,s.City,s.Locality,s.AdType, s.AdOwnerType,s.OfferCondition,s.Price,s.Title,s.Description,s.RelatedKeywords, s.MobileNo,s.Weightage,s.CreateDate,s.IsActive,s.IsAuthorized, s.ClassifiedAdditionalDataId1,s.ClassifiedAdditionalDataId2,s.ClassifiedImageId, s.FileName from #solution1 s inner join tblAdSubCategory sb ON sb.SubCategoryId = s.SubCategoryId inner JOIN tblAdCategory ac ON sb.CategoryId =ac.CategoryId ----inner join tblClassified csf on csf.ClassifiedId=t.ClassifiedId where (@Location IS NULL OR s.Locality=@Location) and (@AdType IS NULL OR s.AdType=@AdType) or (@CategoryId IS NULL OR sb.CategoryId= @CategoryId) drop table #solution1 END END</pre> <p>output</p> <p>ClassifiedId MemberId SubCategoryId Email &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;City &nbsp; &nbsp; Locality &nbsp; AdType AdOwnerType OfferCondition Price &nbsp; Title &nbsp; &nbsp; &nbsp;Description&nbsp;</p> <p>1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 185 &nbsp; &nbsp; &nbsp; tsg@gmail.com Chennai Chennai &nbsp; &nbsp; &nbsp; &nbsp; b &nbsp; &nbsp; &nbsp; &nbsp; null &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;null &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.00 &nbsp; tablet should need it</p> <p>&nbsp;RelatedKeywords&nbsp; MobileNo Weightage CreateDate &nbsp;IsActive IsAuthorized ClassifiedAdditionalDataId1 ClassifiedAdditionalDataId2</p> <p>&nbsp; &nbsp; &nbsp;&nbsp;Rahman &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;59069 &nbsp; &nbsp; &nbsp;&nbsp;3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;2012-05-14 &nbsp;&nbsp;1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1|Music,2|Albums,3|stage &nbsp; &nbsp;&nbsp;&nbsp;NULL</p> <p>ClassifiedImageId &nbsp; &nbsp;FileName&nbsp;<br> &nbsp;1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;355img1030.png</p> 2012-05-28T06:30:22-04:004999858http://forums.asp.net/p/1807336/4999858.aspx/1?Re+how+to+make+dynamic+sql+select+query+for+the+select+query+with+condition+3+table+with+Re: how to make dynamic sql select query for the select query with condition 3 table with ? <p></p> <blockquote><span class="icon-blockquote"></span> <h4>sivaganesh1234</h4> <p></p> <p>output</p> <p>ClassifiedId MemberId SubCategoryId Email &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;City &nbsp; &nbsp; Locality &nbsp; AdType AdOwnerType OfferCondition Price &nbsp; Title &nbsp; &nbsp; &nbsp;Description&nbsp;</p> <p>1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 185 &nbsp; &nbsp; &nbsp; tsg@gmail.com Chennai Chennai &nbsp; &nbsp; &nbsp; &nbsp; b &nbsp; &nbsp; &nbsp; &nbsp; null &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;null &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.00 &nbsp; tablet should need it</p> <p>&nbsp;RelatedKeywords&nbsp; MobileNo Weightage CreateDate &nbsp;IsActive IsAuthorized ClassifiedAdditionalDataId1 ClassifiedAdditionalDataId2</p> <p>&nbsp; &nbsp; &nbsp;&nbsp;Rahman &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;59069 &nbsp; &nbsp; &nbsp;&nbsp;3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;2012-05-14 &nbsp;&nbsp;1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1|Music,2|Albums,3|stage &nbsp; &nbsp;&nbsp;&nbsp;NULL</p> <p>ClassifiedImageId &nbsp; &nbsp;FileName&nbsp;<br> &nbsp;1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;355img1030.png</p> <p></p> </blockquote> <p></p> <p>So what is wrong in query, which will not come into output of query?</p> 2012-05-28T06:37:48-04:005000053http://forums.asp.net/p/1807336/5000053.aspx/1?Re+how+to+make+dynamic+sql+select+query+for+the+select+query+with+condition+3+table+with+Re: how to make dynamic sql select query for the select query with condition 3 table with ? <p>yes &nbsp;</p> <p>&nbsp; &nbsp;yogi &nbsp;similarly &nbsp;i just &nbsp;want to find</p> <p>similarly i want to check 15 condition in else if state ment<br> Else if (@CategoryId&lt;&gt;'' and @AdType&lt;&gt;'' and @VALUES&lt;&gt;'')<br> Else if (@CategoryId&lt;&gt;'' and @VALUES&lt;&gt;'')</p> <p>Else if (@CategoryId&lt;&gt;'' and @AdType&lt;&gt;'' )</p> <p>Else if (@Location&lt;&gt;''and @VALUES&lt;&gt;'')</p> <p>in the same proc</p> <p>inorder to &nbsp;make shrort &nbsp; the store proc condition i asked for &nbsp;dynamic sql query &nbsp;i tried a sample &nbsp;sql query in my &nbsp; &nbsp;question it &nbsp;self i have mentioned</p> 2012-05-28T08:38:57-04:005000062http://forums.asp.net/p/1807336/5000062.aspx/1?Re+how+to+make+dynamic+sql+select+query+for+the+select+query+with+condition+3+table+with+Re: how to make dynamic sql select query for the select query with condition 3 table with ? <p></p> <blockquote><span class="icon-blockquote"></span> <h4>sivaganesh1234</h4> <p></p> <p>yes</p> <p>&nbsp; &nbsp;yogi</p> <p></p> </blockquote> <p></p> <p>Can you point out what is going wrong in output?</p> 2012-05-28T08:42:55-04:005000087http://forums.asp.net/p/1807336/5000087.aspx/1?Re+how+to+make+dynamic+sql+select+query+for+the+select+query+with+condition+3+table+with+Re: how to make dynamic sql select query for the select query with condition 3 table with ? <p>&nbsp;</p> <p>&nbsp; &nbsp;yogi &nbsp;similarly &nbsp;i just &nbsp;want to find</p> <p>similarly i want to check 15 condition in else if state ment<br> Else if (@CategoryId&lt;&gt;'' and @AdType&lt;&gt;'' and @VALUES&lt;&gt;'')<br> Else if (@CategoryId&lt;&gt;'' and @VALUES&lt;&gt;'')</p> <p>Else if (@CategoryId&lt;&gt;'' and @AdType&lt;&gt;'' )</p> <p>Else if (@Location&lt;&gt;''and @VALUES&lt;&gt;'')</p> <p>in the same proc</p> <p>inorder to &nbsp;make shrort &nbsp; the store proc condition i asked for &nbsp;dynamic sql query &nbsp;i tried a sample &nbsp;sql query in my &nbsp; &nbsp;question it &nbsp;self i have mentioned</p> <p>when i execute all parameter means it is showing result</p> <p>EXEC [dbo].[SP_data]<br> @CategoryId=N'247',<br> @VALUES = N'Agra chennai numgambkm',<br> @Location=N'chennai',<br> @AdType =N'b'</p> <p>but when i execute yhis mean &nbsp;iam &nbsp;not getting output</p> <p>EXEC [dbo].[SP_data]<br> @CategoryId=N'247',<br> @VALUES = N'Agra chennai numgambkm',<br> @Location=N'',<br> @AdType =N''</p> <p>similarly for this also</p> <p>EXEC [dbo].[SP_data]<br> @CategoryId=N'247',<br> @VALUES = N'',<br> @Location=N'',<br> @AdType =N''</p> 2012-05-28T08:55:54-04:005000102http://forums.asp.net/p/1807336/5000102.aspx/1?Re+how+to+make+dynamic+sql+select+query+for+the+select+query+with+condition+3+table+with+Re: how to make dynamic sql select query for the select query with condition 3 table with ? <p></p> <blockquote><span class="icon-blockquote"></span> <h4>sivaganesh1234</h4> <p></p> <p>similarly for this also</p> <p>EXEC [dbo].[SP_data]<br> @CategoryId=N'247',<br> @VALUES = N'',<br> @Location=N'',<br> @AdType =N''</p> <p></p> </blockquote> <p></p> <p>In order to work query with respect to blank parameter value, you must have to change following query, i have made little change</p> <p>&amp; run, than post if you do not get exact output</p> <pre class="prettyprint">/* EXEC [dbo].[SP_data] @CategoryId=N'247', @VALUES = N'Agra chennai numgambkm', @Location=N'chennai', @AdType =N'' */ ALTER PROCEDURE [dbo].[SP_data] ( @CategoryId int=NULL, @Location [varchar](120)=NULL, @VALUES varchar(8000)=NULL, @AdType [char](1)=NULL ) AS IF LTRIM(RTRIM(@AdType))='' BEGIN SET @AdType=NULL; END IF @CategoryId=0 BEGIN SET @CategoryId=NULL; END IF RTRIM(LTRIM(@Location))='' BEGIN SET @Location=NULL; END IF ISNULL(@values,'')&lt;&gt;'' BEGIN IF EXISTS ( SELECT * FROM sys.tables WHERE name LIKE '#solution1%' ) DROP TABLE #solution1 create table #solution1 ( ClassifiedId [int],MemberId [int],SubCategoryId [int],Email [varchar](120), City [varchar](120),Locality [varchar](120),AdType [char](1),AdOwnerType [char](1), OfferCondition [char](1),Price [decimal](18, 2),Title [varchar](100) ,[Description] [varchar](1000), RelatedKeywords [varchar](150) ,MobileNo [varchar](20),Weightage [smallint],CreateDate [datetime], IsActive [int],IsAuthorized [int],ClassifiedAdditionalDataId1 [varchar](max),ClassifiedAdditionalDataId2 [varchar](max), ClassifiedImageId [int],[FileName] [varchar](100),DATALEVEL [int] ) BEGIN insert into #solution1 ( ClassifiedId,MemberId,SubCategoryId,Email,City,Locality,AdType,AdOwnerType,OfferCondition,Price,Title, [Description],RelatedKeywords,MobileNo,Weightage,CreateDate,IsActive,IsAuthorized,ClassifiedAdditionalDataId1, ClassifiedAdditionalDataId2,ClassifiedImageId,[FILENAME] ) SELECT DISTINCT(csf.ClassifiedId),--csf.ClassifiedId, csf.MemberId,csf.SubCategoryId,csf.Email,csf.City,csf.Locality,csf.AdType,csf.AdOwnerType,csf.OfferCondition, csf.Price,csf.Title,csf.[Description],csf.RelatedKeywords,csf.MobileNo,csf.Weightage,csf.CreateDate, csf.IsActive,csf.IsAuthorized, (select dbo.FN_GetDataLevel1Value(csf.ClassifiedId))as'ClassifiedAdditionalData1', (select dbo.FN_GetDataLevel2Value(csf.ClassifiedId))as'ClassifiedAdditionalData2', (select TOP 1 ClassifiedImageId from tblClassifiedImage where ClassifiedId=CFI.ClassifiedId)as'ClassifiedImageId', (select TOP 1 [FILENAME] from tblClassifiedImage where ClassifiedId=CFI.ClassifiedId) as'[FILENAME]' FROM tblClassified csf LEFT OUTER JOIN tblAdSubCategory sb ON sb.SubCategoryId = csf.SubCategoryId LEFT OUTER JOIN tblAdCategory ac ON ac.CategoryId =sb.CategoryId LEFT OUTER JOIN tblClassifiedAdditionalData CAD ON CAD.ClassifiedId =csf.ClassifiedId LEFT OUTER JOIN tblSubCategoryAdditionalData SAD ON SAD.SubCategoryAdditionalDataId=CAD.SubCategoryAdditionalDataId LEFT OUTER JOIN tblClassifiedImage CFI ON csf.ClassifiedId = CFI.ClassifiedId cross apply dbo.characterSplit(@values, '') ds --Replace ',' with your actual delimter where csf.Email LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.City LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.Locality LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' --or csf.AdType LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.AdOwnerType LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.OfferCondition LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.Price LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.Title LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.Description LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.RelatedKeywords LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.MobileNo LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.Weightage LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' select s.ClassifiedId,s.MemberId,s.SubCategoryId,s.Email,s.City,s.Locality,s.AdType, s.AdOwnerType,s.OfferCondition,s.Price,s.Title,s.Description,s.RelatedKeywords, s.MobileNo,s.Weightage,s.CreateDate,s.IsActive,s.IsAuthorized, s.ClassifiedAdditionalDataId1,s.ClassifiedAdditionalDataId2,s.ClassifiedImageId, s.FileName from #solution1 s inner join tblAdSubCategory sb ON sb.SubCategoryId = s.SubCategoryId inner JOIN tblAdCategory ac ON sb.CategoryId =ac.CategoryId ----inner join tblClassified csf on csf.ClassifiedId=t.ClassifiedId where (@Location IS NULL OR s.Locality=@Location) and (@AdType IS NULL OR s.AdType=@AdType) or (@CategoryId IS NULL OR sb.CategoryId= @CategoryId) drop table #solution1 END END</pre> <p><br> <br> </p> 2012-05-28T09:01:02-04:005000472http://forums.asp.net/p/1807336/5000472.aspx/1?Re+how+to+make+dynamic+sql+select+query+for+the+select+query+with+condition+3+table+with+Re: how to make dynamic sql select query for the select query with condition 3 table with ? <p>if iam giving &nbsp;only one &nbsp;parameter &nbsp;value &nbsp;means &nbsp;the &nbsp;output was &nbsp;not coming out</p> <p></p> <p>for example</p> <p>if iam giving &nbsp; only &nbsp;</p> <p>&nbsp; &nbsp; &nbsp;<span>EXEC [dbo].[SP_data]</span><br> <span>@CategoryId=N'247',</span><br> <span>@VALUES = N'',</span><br> <span>@Location=N'',</span><br> <span>@AdType =N''</span></p> <p><span>and if iam giving only</span></p> <p><span><span>EXEC [dbo].[SP_data]</span><br> <span>@CategoryId=N'',</span><br> <span>@VALUES = N'',</span><br> <span>@Location=N'',</span><br> <span>@AdType =N'b'</span></span></p> <p><span><span>if iam giving &nbsp; only &nbsp;</span></span></p> <p><span><span><span>EXEC [dbo].[SP_data]</span><br> <span>@CategoryId=N'',</span><br> <span>@VALUES = N'',</span><br> <span>@Location=N'agra',</span><br> <span>@AdType =N''</span></span></span></p> <p><span><span><span>i am not finding the output</span></span></span></p> 2012-05-28T12:07:04-04:005000519http://forums.asp.net/p/1807336/5000519.aspx/1?Re+how+to+make+dynamic+sql+select+query+for+the+select+query+with+condition+3+table+with+Re: how to make dynamic sql select query for the select query with condition 3 table with ? <p></p> <blockquote><span class="icon-blockquote"></span> <h4>sivaganesh1234</h4> <p></p> <p>if iam giving &nbsp;only one &nbsp;parameter &nbsp;value &nbsp;means &nbsp;the &nbsp;output was &nbsp;not coming out</p> <p></p> <p>for example</p> <p>if iam giving &nbsp; only &nbsp;</p> <p>&nbsp; &nbsp; &nbsp;<span>EXEC [dbo].[SP_data]</span><br> <span>@CategoryId=N'247',</span><br> <span>@VALUES = N'',</span><br> <span>@Location=N'',</span><br> <span>@AdType =N''</span></p> <p><span>and if iam giving only</span></p> <p><span><span>EXEC [dbo].[SP_data]</span><br> <span>@CategoryId=N'',</span><br> <span>@VALUES = N'',</span><br> <span>@Location=N'',</span><br> <span>@AdType =N'b'</span></span></p> <p><span><span>if iam giving &nbsp; only &nbsp;</span></span></p> <p><span><span><span>EXEC [dbo].[SP_data]</span><br> <span>@CategoryId=N'',</span><br> <span>@VALUES = N'',</span><br> <span>@Location=N'agra',</span><br> <span>@AdType =N''</span></span></span></p> <p><span><span><span>i am not finding the output</span></span></span></p> <p></p> </blockquote> <p></p> <p>Now you must have to use this query which will work in all environment..</p> <pre class="prettyprint">ALTER PROCEDURE [dbo].[SP_data] ( @CategoryId int=NULL, @Location [varchar](120)=NULL, @VALUES varchar(8000)=NULL, @AdType [char](1)=NULL ) AS IF LTRIM(RTRIM(@VALUES))='' BEGIN SET @VALUES=NULL; END IF LTRIM(RTRIM(@AdType))='' BEGIN SET @AdType=NULL; END IF @CategoryId=0 BEGIN SET @CategoryId=NULL; END IF RTRIM(LTRIM(@Location))='' BEGIN SET @Location=NULL; END IF EXISTS ( SELECT * FROM sys.tables WHERE name LIKE '#solution1%' ) DROP TABLE #solution1 create table #solution1 ( ClassifiedId [int],MemberId [int],SubCategoryId [int],Email [varchar](120), City [varchar](120),Locality [varchar](120),AdType [char](1),AdOwnerType [char](1), OfferCondition [char](1),Price [decimal](18, 2),Title [varchar](100) ,[Description] [varchar](1000), RelatedKeywords [varchar](150) ,MobileNo [varchar](20),Weightage [smallint],CreateDate [datetime], IsActive [int],IsAuthorized [int],ClassifiedAdditionalDataId1 [varchar](max),ClassifiedAdditionalDataId2 [varchar](max), ClassifiedImageId [int],[FileName] [varchar](100),DATALEVEL [int] ) BEGIN IF ISNULL(@VALUES,'')!='' BEGIN insert into #solution1 ( ClassifiedId,MemberId,SubCategoryId,Email,City,Locality,AdType,AdOwnerType,OfferCondition,Price,Title, [Description],RelatedKeywords,MobileNo,Weightage,CreateDate,IsActive,IsAuthorized,ClassifiedAdditionalDataId1, ClassifiedAdditionalDataId2,ClassifiedImageId,[FILENAME] ) SELECT DISTINCT(csf.ClassifiedId),--csf.ClassifiedId, csf.MemberId,csf.SubCategoryId,csf.Email,csf.City,csf.Locality,csf.AdType,csf.AdOwnerType,csf.OfferCondition, csf.Price,csf.Title,csf.[Description],csf.RelatedKeywords,csf.MobileNo,csf.Weightage,csf.CreateDate, csf.IsActive,csf.IsAuthorized, (select dbo.FN_GetDataLevel1Value(csf.ClassifiedId))as'ClassifiedAdditionalData1', (select dbo.FN_GetDataLevel2Value(csf.ClassifiedId))as'ClassifiedAdditionalData2', (select TOP 1 ClassifiedImageId from tblClassifiedImage where ClassifiedId=CFI.ClassifiedId)as'ClassifiedImageId', (select TOP 1 [FILENAME] from tblClassifiedImage where ClassifiedId=CFI.ClassifiedId) as'[FILENAME]' FROM tblClassified csf LEFT OUTER JOIN tblAdSubCategory sb ON sb.SubCategoryId = csf.SubCategoryId LEFT OUTER JOIN tblAdCategory ac ON ac.CategoryId =sb.CategoryId LEFT OUTER JOIN tblClassifiedAdditionalData CAD ON CAD.ClassifiedId =csf.ClassifiedId LEFT OUTER JOIN tblSubCategoryAdditionalData SAD ON SAD.SubCategoryAdditionalDataId=CAD.SubCategoryAdditionalDataId LEFT OUTER JOIN tblClassifiedImage CFI ON csf.ClassifiedId = CFI.ClassifiedId cross apply dbo.characterSplit(@values, '') ds --Replace ',' with your actual delimter where csf.Email LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.City LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.Locality LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' --or csf.AdType LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.AdOwnerType LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.OfferCondition LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.Price LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.Title LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.Description LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.RelatedKeywords LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.MobileNo LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' or csf.Weightage LIKE '%'&#43; REPLACE(RTRIM(LTRIM(ds.Items)), ' ', '%' ) &#43; '%' END ELSE BEGIN insert into #solution1 ( ClassifiedId,MemberId,SubCategoryId,Email,City,Locality,AdType,AdOwnerType,OfferCondition,Price,Title, [Description],RelatedKeywords,MobileNo,Weightage,CreateDate,IsActive,IsAuthorized,ClassifiedAdditionalDataId1, ClassifiedAdditionalDataId2,ClassifiedImageId,[FILENAME] ) SELECT DISTINCT(csf.ClassifiedId),--csf.ClassifiedId, csf.MemberId,csf.SubCategoryId,csf.Email,csf.City,csf.Locality,csf.AdType,csf.AdOwnerType,csf.OfferCondition, csf.Price,csf.Title,csf.[Description],csf.RelatedKeywords,csf.MobileNo,csf.Weightage,csf.CreateDate, csf.IsActive,csf.IsAuthorized, (select dbo.FN_GetDataLevel1Value(csf.ClassifiedId))as'ClassifiedAdditionalData1', (select dbo.FN_GetDataLevel2Value(csf.ClassifiedId))as'ClassifiedAdditionalData2', (select TOP 1 ClassifiedImageId from tblClassifiedImage where ClassifiedId=CFI.ClassifiedId)as'ClassifiedImageId', (select TOP 1 [FILENAME] from tblClassifiedImage where ClassifiedId=CFI.ClassifiedId) as'[FILENAME]' FROM tblClassified csf LEFT OUTER JOIN tblAdSubCategory sb ON sb.SubCategoryId = csf.SubCategoryId LEFT OUTER JOIN tblAdCategory ac ON ac.CategoryId =sb.CategoryId LEFT OUTER JOIN tblClassifiedAdditionalData CAD ON CAD.ClassifiedId =csf.ClassifiedId LEFT OUTER JOIN tblSubCategoryAdditionalData SAD ON SAD.SubCategoryAdditionalDataId=CAD.SubCategoryAdditionalDataId LEFT OUTER JOIN tblClassifiedImage CFI ON csf.ClassifiedId = CFI.ClassifiedId END select s.ClassifiedId,s.MemberId,s.SubCategoryId,s.Email,s.City,s.Locality,s.AdType, s.AdOwnerType,s.OfferCondition,s.Price,s.Title,s.Description,s.RelatedKeywords, s.MobileNo,s.Weightage,s.CreateDate,s.IsActive,s.IsAuthorized, s.ClassifiedAdditionalDataId1,s.ClassifiedAdditionalDataId2,s.ClassifiedImageId, s.FileName from #solution1 s inner join tblAdSubCategory sb ON sb.SubCategoryId = s.SubCategoryId inner JOIN tblAdCategory ac ON sb.CategoryId =ac.CategoryId ----inner join tblClassified csf on csf.ClassifiedId=t.ClassifiedId where (@Location IS NULL OR s.Locality=@Location) and (@AdType IS NULL OR s.AdType=@AdType) or (@CategoryId IS NULL OR sb.CategoryId= @CategoryId) drop table #solution1</pre> <p><br> <br> </p> 2012-05-28T12:29:20-04:00