{"id":979,"date":"2022-03-09T12:07:26","date_gmt":"2022-03-09T05:07:26","guid":{"rendered":"https:\/\/www.bagi2info.com\/?p=979"},"modified":"2022-09-11T14:02:04","modified_gmt":"2022-09-11T07:02:04","slug":"k2-smartform-filter-pencarian-dengan-sql-server-stored-procedure","status":"publish","type":"post","link":"https:\/\/www.bagi2info.com\/en\/k2-smartform-filter-stored-procedure-search-in-sql-server\/","title":{"rendered":"K2 SmartForm Filter Stored Procedure Search in SQL Server"},"content":{"rendered":"<p>Because the default search filter provided by K2 Smartform is not preferred, after searching for other alternatives, finally found the use of stored procedure(SP) as an alternative to search filters.<\/p>\n<p>Here are the steps to create a custom search filter with a stored procedure<\/p>\n<p><strong>1. Create a Student smart object with the following table:<br \/>\n<\/strong><br \/>\nid int,<br \/>\ncodemhs string,<br \/>\nstring name,<br \/>\ndate of birth date,<br \/>\nString major<\/p>\n<p><strong>2. Create a stored procedure with the following code and run it in a sql query:<\/strong><\/p>\n<p>CREATE PROCEDURE [dbo].[StudentGetList]<br \/>\n@pkodemhs nvarchar(100) = &#8221;,<br \/>\n@pnama nvarchar(100) = &#8221;,<br \/>\n@ptgl_born date = &#8216;1900-01-01&#8217;,<br \/>\n@pjurusan nvarchar(100) = &#8221;,<br \/>\n@SOP nvarchar(100) = &#8216;=-=-=-=&#8217;<br \/>\nUS<br \/>\nSELECT TOP 1 ID, code, name, date of birth, major<br \/>\nFROM [dbo].[Student]<br \/>\nwhere 1 = 1<\/p>\n<p><strong>3. Do Refresh Service Instance<br \/>\n<\/strong><br \/>\n<strong><a href=\"https:\/\/www.bagi2info.com\/wp-content\/uploads\/2017\/01\/refresh-service-instance.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-988\" src=\"https:\/\/www.bagi2info.com\/wp-content\/uploads\/2017\/01\/refresh-service-instance-300x188.png\" alt=\"\" width=\"300\" height=\"188\" srcset=\"https:\/\/www.bagi2info.com\/wp-content\/uploads\/2017\/01\/refresh-service-instance-300x188.png 300w, https:\/\/www.bagi2info.com\/wp-content\/uploads\/2017\/01\/refresh-service-instance-768x480.png 768w, https:\/\/www.bagi2info.com\/wp-content\/uploads\/2017\/01\/refresh-service-instance-1024x640.png 1024w, https:\/\/www.bagi2info.com\/wp-content\/uploads\/2017\/01\/refresh-service-instance.png 1440w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/strong><\/p>\n<p><strong>4. Then Create Smartobjects<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-982\" src=\"https:\/\/www.bagi2info.com\/wp-content\/uploads\/2017\/01\/publishso.jpg\" alt=\"publishso\" width=\"821\" height=\"783\" srcset=\"https:\/\/www.bagi2info.com\/wp-content\/uploads\/2017\/01\/publishso.jpg 821w, https:\/\/www.bagi2info.com\/wp-content\/uploads\/2017\/01\/publishso-300x286.jpg 300w, https:\/\/www.bagi2info.com\/wp-content\/uploads\/2017\/01\/publishso-768x732.jpg 768w\" sizes=\"auto, (max-width: 821px) 100vw, 821px\" \/><\/strong><br \/>\n<strong><br \/>\n5. Perform an Alter Stored Procedure with the following code:<\/strong><\/p>\n<p>CREATE PROCEDURE [dbo].[StudentGetList]<br \/>\n@pkodemhs nvarchar(100) = &#8221;,<br \/>\n@pnama nvarchar(100) = &#8221;,<br \/>\n@ptgl_born date = &#8216;1900-01-01&#8217;,<br \/>\n@pjurusan nvarchar(100) = &#8221;,<br \/>\n@SOP nvarchar(100) = &#8216;=-=-=-=&#8217;<br \/>\nUS<\/p>\n<p>set @pkodemhs = LTRIM(RTRIM(@pkodemhs))<br \/>\nset @name = LTRIM(RTRIM(@name))<br \/>\nset @pjurusan = LTRIM(RTRIM(@pjurusan))<\/p>\n<p>set @sql = &#8216;SELECT ID, code number, name, date of birth, major<br \/>\nFROM [dbo].[Student]<br \/>\nwhere 1 = 1&#8217;<\/p>\n<p>if (LEN(@pkodemhs) &gt; 0) begin<br \/>\nset @OP = REVERSE(PARSENAME(REPLACE(REVERSE(@SOP), &#8216;-&#8216;, &#8216;.&#8217;), 1))<\/p>\n<p>if @OP = &#8216;=&#8217;<br \/>\nset @sql = @sql + &#8216; and codemhs &#8216; + @OP + &#8216; &#8221;&#8217; + @pkodemhs + &#8221;&#8221;<\/p>\n<p>if @OP = &#8216;Start With&#8217;<br \/>\nset @sql = @sql + &#8216; and codemhs Like &#8221;&#8217; + @pkodemhs + &#8216;%&#8221;&#8217;<\/p>\n<p>if @OP = &#8216;Like&#8217;<br \/>\nset @sql = @sql + &#8216; andkodemhs &#8216; + @OP + &#8216; &#8221;%&#8217; + @pkodemhs + &#8216;%&#8221;&#8217;<br \/>\nend<br \/>\nif (LEN(@name) &gt; 0) begin<br \/>\nset @OP = REVERSE(PARSENAME(REPLACE(REVERSE(@SOP), &#8216;-&#8216;, &#8216;.&#8217;), 2))<\/p>\n<p>if @OP = &#8216;=&#8217;<br \/>\nset @sql = @sql + &#8216; and name &#8216; + @OP + &#8216; &#8221;&#8217; + @name + &#8221;&#8221;<br \/>\nif @OP = &#8216;Start With&#8217;<br \/>\nset @sql = @sql + &#8216; and name Like &#8221;&#8217; + @name + &#8216;%&#8221;&#8217;<br \/>\nif @OP = &#8216;Like&#8217;<br \/>\nset @sql = @sql + &#8216; and name &#8216; + @OP + &#8216; &#8221;%&#8217; + @name + &#8216;%&#8221;&#8217;<br \/>\nend<br \/>\nif (LEN(@ptgl_born ) &gt; 0) and (@ptgl_born &lt;&gt; &#8216;1900-01-01&#8217;) begin<br \/>\nset @OP = REVERSE(PARSENAME(REPLACE(REVERSE(@SOP), &#8216;-&#8216;, &#8216;.&#8217;), 3))<\/p>\n<p>if @OP = &#8216;=&#8217;<br \/>\nset @sql = @sql + &#8216; and date of birth &#8216; + @OP + &#8221;&#8221; + convert(varchar(10), @ptgl_birth ) + &#8221;&#8221;<br \/>\nif @OP = &#8216;Start With&#8217;<br \/>\nset @sql = @sql + &#8216; and date of birth Like &#8221;&#8217; + convert(varchar(10), @ptgl_birth ) + &#8216;%&#8221;&#8217;<br \/>\nif @OP = &#8216;Like&#8217;<br \/>\nset @sql = @sql + &#8216; and date of birth &#8216; + @OP + &#8216; &#8221;%&#8217; + convert(varchar(10), @ptgl_birth ) + &#8216;%&#8221;&#8217;<br \/>\nend<\/p>\n<p>if (LEN(@pjurusan) &gt; 0) begin<br \/>\nset @OP = REVERSE(PARSENAME(REPLACE(REVERSE(@SOP), &#8216;-&#8216;, &#8216;.&#8217;), 4))<\/p>\n<p>if @OP = &#8216;=&#8217;<br \/>\nset @sql = @sql + &#8216; and major &#8216; + @OP + &#8221;&#8221; + @department + &#8221;&#8221;<br \/>\nif @OP = &#8216;Start With&#8217;<br \/>\nset @sql = @sql + &#8216; and majors Like &#8221;&#8217; + @pjurusan + &#8216;%&#8221;&#8217;<br \/>\nif @OP = &#8216;Like&#8217;<br \/>\nset @sql = @sql + &#8216; and majors &#8216; + @OP + &#8216; &#8221;%&#8217; + @department + &#8216;%&#8221;&#8217;<br \/>\nend<\/p>\n<p>execute (@sql)<\/p>\n<p><strong>6.Then create a form with the following view:<\/strong><\/p>\n<p><a href=\"https:\/\/www.bagi2info.com\/wp-content\/uploads\/2017\/01\/mhsform.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-991\" src=\"https:\/\/www.bagi2info.com\/wp-content\/uploads\/2017\/01\/mhsform-1024x393.jpg\" alt=\"\" width=\"940\" height=\"361\" srcset=\"https:\/\/www.bagi2info.com\/wp-content\/uploads\/2017\/01\/mhsform-1024x393.jpg 1024w, https:\/\/www.bagi2info.com\/wp-content\/uploads\/2017\/01\/mhsform-300x115.jpg 300w, https:\/\/www.bagi2info.com\/wp-content\/uploads\/2017\/01\/mhsform-768x295.jpg 768w, https:\/\/www.bagi2info.com\/wp-content\/uploads\/2017\/01\/mhsform.jpg 1414w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/a><br \/>\n<strong><br \/>\n7. On the Search button add a List method with input parameters as shown<\/strong><\/p>\n<p><a href=\"https:\/\/www.bagi2info.com\/wp-content\/uploads\/2017\/01\/getlistconfig.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-992\" src=\"https:\/\/www.bagi2info.com\/wp-content\/uploads\/2017\/01\/getlistconfig-1024x547.jpg\" alt=\"\" width=\"940\" height=\"502\" srcset=\"https:\/\/www.bagi2info.com\/wp-content\/uploads\/2017\/01\/getlistconfig-1024x547.jpg 1024w, https:\/\/www.bagi2info.com\/wp-content\/uploads\/2017\/01\/getlistconfig-300x160.jpg 300w, https:\/\/www.bagi2info.com\/wp-content\/uploads\/2017\/01\/getlistconfig-768x410.jpg 768w, https:\/\/www.bagi2info.com\/wp-content\/uploads\/2017\/01\/getlistconfig.jpg 1432w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/a><\/p>\n<p>K2 by default will detect whether the stored procedure has a List method or an Execute method, but there are problems in recognizing the Stored Procedure which is a little advanced in number 5, causing K2 to provide an Execute method even though what we want is a List method.<\/p>\n<p>Therefore, to get the <strong>List Method<\/strong>, the first time we outsmart K2 with simple sql coding at the beginning, namely number 2, after <strong>Refreshing Service Instances<\/strong> on <strong>Service Objects<\/strong> in <strong>SmartObjects<\/strong> &#8211; <strong>Services<\/strong> &#8211; <strong>Tester<\/strong> and getting <strong>List Method<\/strong> then do alter SP again at number The 5th without doing a Refresh Service Instance, so K2 will assume that the SP uses the List Method.<\/p>\n<p>The weakness in this method is that it enforces the List method if you do a <strong>Refresh service Instance<\/strong>, then K2 will give the Execute method to the SP, and we have to outsmart the SP with a simple SP again.<\/p>\n<p>The sample file above can be downloaded <a href=\"https:\/\/www.bagi2info.com\/wp-content\/uploads\/2017\/01\/MahasiswaGetList.zip\">here<\/a><\/p>\n<h2>Newest way to display List Method<\/h2>\n<p>After contacting K2, here are the steps to display the List Method for complex SP<\/p>\n<p>Create Parent SP that accesses SP StudentGetList<\/p>\n<p>CREATE PROCEDURE [dbo].[StudentGetListParent]<br \/>\n@pkodemhs nvarchar(100) = &#8221;,<br \/>\n@pnama nvarchar(100) = &#8221;,<br \/>\n@ptgl_born date = &#8216;1900-01-01&#8217;,<br \/>\n@pjurusan nvarchar(100) = &#8221;,<br \/>\n@SOP nvarchar(100) = &#8216;=-=-=-=&#8217;<br \/>\nUS<\/p>\n<p>IF 1=0 BEGIN<br \/>\nSET FMTONLY OFF<br \/>\nEND<br \/>\nexec [StudentGetList] @pkodemhs,@pnama,@ptgl_lahir,@pjurusan,@SOP;<br \/>\n&#8211;SET FMTONLY OFF<\/p>\n<p>Testing environment using<\/p>\n<p>K2 version 11<br \/>\nSQL Server<br \/>\nSQL Server Management Studio<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Because the default search filter provided by K2 Smartform is not preferred, after searching for other alternatives, finally found the use of stored procedure(SP) as an alternative to search filters. Here are the steps&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":988,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-979","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-komputer"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/posts\/979","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/comments?post=979"}],"version-history":[{"count":0,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/posts\/979\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/media\/988"}],"wp:attachment":[{"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/media?parent=979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/categories?post=979"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/tags?post=979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}