tag:blogger.com,1999:blog-33281234806098544022024-02-07T16:33:06.973-08:00Decoding CodeDo not go where the path may lead; go instead where there is no path and leave a trail~ Ralph Waldo EmersonSajjan Sarkarhttp://www.blogger.com/profile/07258575012965819548noreply@blogger.comBlogger9125tag:blogger.com,1999:blog-3328123480609854402.post-74628505101028162262012-11-07T07:37:00.001-08:002012-11-07T07:38:28.330-08:00Overriding Default Javascript Function Behaviors (e.g. : window.open)<p>Sometimes you might need to make a change to the default behavior of a Javascript function and want to apply it to all pages. An example could be to do some custom behavior replacing all new window popups with jQuery modal dialogs across an existing application. Instead of going through the code and finding all instances of window.open() and replacing with modal dialogs you could override window.open() itself and do it in one place.</p> <p>Here’s a neat technique to override default functions without corrupting the global namespace.</p> <p><strong>Code:</strong></p> <p><strong>Example 1: Override but do nothing, call window.open:</strong></p><pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #008000">//override globally</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #0000ff">window</span>.<span style="color: #0000ff">open</span> = <span style="color: #0000ff">function</span> (<span style="color: #0000ff">open</span>) {<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">function</span> (url, <span style="color: #0000ff">name</span>, features) { <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">// some browsers dont support the call() method, so call the method directly in such cases </span><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">open</span>.call ? <span style="color: #0000ff">open</span>.call(<span style="color: #0000ff">window</span>, url, <span style="color: #0000ff">name</span>, features):<span style="color: #0000ff">open</span>( url, <span style="color: #0000ff">name</span>, features);<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> };<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> }(<span style="color: #0000ff">window</span>.<span style="color: #0000ff">open</span>);<span style="color: #008000">// pass in the window.open function object as argument to the anonymous function</span></pre></pre><strong>Example 2: Override but do nothing, call showModalDialog function: (Leaving out the actual implementation of showModalDialog() for brevity)<pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #008000">//override globally</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #0000ff">window</span>.<span style="color: #0000ff">open</span> = <span style="color: #0000ff">function</span> (<span style="color: #0000ff">open</span>) {<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">function</span> (url, <span style="color: #0000ff">name</span>, features) { <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">// some browsers dont support the call() method, so call the method directly in such cases </span><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">open</span>.call ? showModalDialog.call(<span style="color: #0000ff">window</span>, url, <span style="color: #0000ff">name</span>, features):showModalDialog( url, <span style="color: #0000ff">name</span>, features);<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> };<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> }(<span style="color: #0000ff">window</span>.<span style="color: #0000ff">open</span>);<span style="color: #008000">// pass in the window.open function object as argument to the anonymous function</span></pre></pre></strong> Sajjan Sarkarhttp://www.blogger.com/profile/07258575012965819548noreply@blogger.com0tag:blogger.com,1999:blog-3328123480609854402.post-86023119009491797822012-11-07T07:12:00.001-08:002012-11-07T07:17:30.237-08:00jQuery Plugin to Convert TextBox to a HH:MM Timefield<p> </p> <p>Wrote a little jQuery plugin which converts a regular HTML textbox into a time-entry field with all the necessary key bindings and masks. <p><strong>Features: </strong> <p>· supports time entry in 12 or 24 format <p>· API user can pass custom handler in case user types invalid time. <p>· Tested in IE8,Chrome(Desktop and Android),Safari(Desktop and iPhone),Firefox and Opera. <p>The source JS file, examples and documentation are located at: <a href="http://sajjansarkar.zxq.net/jquery/timefield/">http://sajjansarkar.zxq.net/jquery/timefield/</a> <p>Hope it is useful and please let me know if u find any bugs/have any suggestions! <p><strong>Downloads,Demos and Documentation: <a href="http://sajjansarkar.zxq.net/jquery/timefield/">http://sajjansarkar.zxq.net/jquery/timefield/</a></strong> <p><strong>Usage:</strong><pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">$("<span style="color: #8b0000">#tbPlain</span>").timefield();<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">$("<span style="color: #8b0000">#tb12Hour</span>").timefield({ 'hourFormat': '12'});<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">$("<span style="color: #8b0000">#tb24Hour</span>").timefield({ 'hourFormat': '24'});<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">$("<span style="color: #8b0000">#tbWithInvalidHandler</span>").timefield({ <br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 'hourFormat': '24',<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 'onError' : <span style="color: #0000ff">function</span>(){<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">alert</span>(<span style="color: #0000ff">this</span>.value+' is not a valid time!');<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">this</span>.style.backgroundColor='red';<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> }<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> });<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">$("<span style="color: #8b0000">#tbOnOff</span>").timefield();<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">$("<span style="color: #8b0000">#btnTurnOff</span>").click(<span style="color: #0000ff">function</span>(){<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> $("<span style="color: #8b0000">#tbOnOff</span>").timefield('turnOffTimeField');<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> });</pre></pre><br /><p><strong>Code:<pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #008000">/**</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * @author Sajjan Sarkar</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> */</span></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">(<span style="color: #0000ff">function</span>($)</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">{</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> $.fn.timefield = <span style="color: #0000ff">function</span>(options)</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">//Public Methods</span></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">var</span> methods = {</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** Initializes the timefield */</span></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> init : <span style="color: #0000ff">function</span>(options)</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.each(<span style="color: #0000ff">function</span>()</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** Txt entry is from RIGHT TO LEFT */</span></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">this</span>.style.direction = "<span style="color: #8b0000">rtl</span>";</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** Cache for performance*/</span></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">var</span> $<span style="color: #0000ff">this</span> = $(<span style="color: #0000ff">this</span>); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** bind listeners */</span></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> $<span style="color: #0000ff">this</span>.bind('keydown.timefield', helpers.eventHandlers.keyDownHandler);</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> $<span style="color: #0000ff">this</span>.bind('keyup.timefield', helpers.eventHandlers.keyUpHandler);</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> $<span style="color: #0000ff">this</span>.bind('<span style="color: #0000ff">blur</span>.timefield', helpers.eventHandlers.blurHandler);</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> </pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> });</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> },</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** removes all timefield effects**/</span></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> turnOffTimeField : <span style="color: #0000ff">function</span>(content)</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.each(<span style="color: #0000ff">function</span>()</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">this</span>.style.direction = "<span style="color: #8b0000">ltr</span>";</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">var</span> $<span style="color: #0000ff">this</span> = $(<span style="color: #0000ff">this</span>);</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> $<span style="color: #0000ff">this</span>.unbind('.timefield');</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> });</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> }</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> </pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> };</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** Private methods */</span></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">var</span> helpers = {</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> eventHandlers : {</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** On key down, check if the pressed key is valid else dont show it.</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * acts as mask</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * */</span></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> keyDownHandler : <span style="color: #0000ff">function</span>(event)</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> </pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">if</span> (!helpers.utils.isValidKeyStroke(event))</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> event.preventDefault();</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> event.stopPropagation();</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> }</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> },</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/**</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * When this is called the keydown event would have already rendered the </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * key stroke, so apply the padding with zeros.</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * */</span></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> keyUpHandler : <span style="color: #0000ff">function</span>(event)</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">var</span> keyCode = event.keyCode;</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** check if a special key was pressed */</span></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">if</span> ($.inArray(keyCode, constants.KEYS.SPECIAL_KEYS_TO_IGNORE) != -1)</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">return</span>;</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** did this on key up instead of keydown as its easier coz the text has rendered */</span></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> helpers.utils.applyPadding(helpers.utils.getEventSource(event));</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> },</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/**</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * On blur, check the validity of the enterred value based on selected settings.</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * Could not do this on change as most browsers dont fire the change event </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * if the value has changed programatically.</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * If the time is invalid, call the specified(or default) onError handler</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * */</span></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> blurHandler : <span style="color: #0000ff">function</span>(event)</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> helpers.utils.getEventSource(event).value = jQuery.trim( helpers.utils.getEventSource(event).value );</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">if</span> (!helpers.utils.isValidTime(helpers.utils.getEventSource(event).value))</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> settings.onError.call(helpers.utils.getEventSource(event));</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> }</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> }</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> },</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> utils : {</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/**</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * This function returns true if the enterred key is a number, a colon or one of the allowed special keys.</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * */</span></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> isValidKeyStroke : <span style="color: #0000ff">function</span>(event)</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">var</span> keyCode = event.keyCode;</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** check if a special key was pressed */</span></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">if</span> ($.inArray(keyCode, constants.KEYS.SPECIAL_KEYS_TO_IGNORE) != -1)</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span>;</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** Only allow numbers and the colon key */</span></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">if</span> ((!event.shiftKey && (constants.KEYS.CHARACTER_ZERO <= keyCode </pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> && keyCode <= constants.KEYS.CHARACTER_NINE || constants.KEYS.KEYPAD_ZERO <= keyCode</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> && keyCode <= constants.KEYS.KEYPAD_NINE))</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> || (event.shiftKey && keyCode == constants.KEYS.COLON))</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span>;</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> }</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> },</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/**</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * This function pads the values with zeroes and colons.</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * */</span></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> applyPadding : <span style="color: #0000ff">function</span>(textBox)</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">var</span> val = textBox.value;</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">if</span> (val != "<span style="color: #8b0000"></span>")</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> val = <span style="color: #0000ff">parseInt</span>(textBox.value.replace("<span style="color: #8b0000">:</span>", "<span style="color: #8b0000"></span>"), 10) + "<span style="color: #8b0000"></span>";</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">if</span> (val.<span style="color: #0000ff">length</span> > constants.NO_OF_CHARACTERS_WITHOUT_COLON)</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> val = val.substring(val.<span style="color: #0000ff">length</span> - constants.NO_OF_CHARACTERS_WITHOUT_COLON);</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> }</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">switch</span> (val.<span style="color: #0000ff">length</span>)</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">case</span> 0:</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> val = val;</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">break</span>;</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">case</span> 1:</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> val = "<span style="color: #8b0000">00:0</span>" + val;</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">break</span>;</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">case</span> 2:</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> val = "<span style="color: #8b0000">00:</span>" + val;</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">break</span>;</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">case</span> 3:</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> val = "<span style="color: #8b0000">0</span>" + val.substring(0, 1) + "<span style="color: #8b0000">:</span>" + val.substring(1);</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">break</span>;</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">case</span> 4:</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> val = val.substring(0, 2) + "<span style="color: #8b0000">:</span>" + val.substring(2);</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">break</span>;</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> }</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> </pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">//$(textbox).blur();</span></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">//$(textbox).focus();</span></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/**</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * mobile browsers seem to not place the caret in the correct place</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * after the padding of zeroes, and IE8 doesnt support</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * setselection range but works fine without it.</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * The basic idea is to add an extra space after the last digit</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * and "select" it,ensuring that the caret is in the correct position</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * </pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * */</span></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">if</span>(textBox.setSelectionRange)</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> textBox.value = val+"<span style="color: #8b0000"> </span>";</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> textBox.setSelectionRange(5, 6);</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> }</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">else</span></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> textBox.value = val;</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> }</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">return</span>;</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> },</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/**</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * This function returns true if the enterred time is </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * a valid time based on the specified(or default) settings,</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * else returns false.</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * */</span></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> isValidTime : <span style="color: #0000ff">function</span>(strTime)</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">if</span> (strTime == "<span style="color: #8b0000"></span>")</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span>;</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">var</span> regex = constants.REGEX.TWENTY_FOUR;</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">switch</span> (settings.hourFormat)</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">case</span> constants.HOUR_FORMATS.TWELVE:</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> regex = constants.REGEX.TWELVE;</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">break</span>;</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">case</span> constants.HOUR_FORMATS.TWENTY_FOUR:</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> regex = constants.REGEX.TWENTY_FOUR;</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">break</span>;</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> }</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">return</span> regex.test(strTime);</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> },</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> getEventSource:<span style="color: #0000ff">function</span>(event)</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">return</span> event.target || event.srcElement;</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> }</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> }</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> };</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/**Constants*/</span></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">var</span> constants = {</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** Only 2 formats supported now.*/</span></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> HOUR_FORMATS : {</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> TWELVE : "<span style="color: #8b0000">12</span>",</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> TWENTY_FOUR : "<span style="color: #8b0000">24</span>"</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> },</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> KEYS : {</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> CHARACTER_ZERO : 48,</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> CHARACTER_NINE : 57,</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> KEYPAD_ZERO : 96,</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> KEYPAD_NINE : 105,</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> BACKSPACE : 8,</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> TAB : 9,</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> ENTER : 13,</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> COLON : 186,</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> SPECIAL_KEYS_TO_IGNORE : [ </pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 8, <span style="color: #008000">/**BACKSPACE*/</span></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 9, <span style="color: #008000">/**DELETE*/</span></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 46, <span style="color: #008000">/**TAB*/</span></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 40, <span style="color: #008000">/**DOWN ARROW*/</span></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 39, <span style="color: #008000">/**LEFT ARROW*/</span></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 38, <span style="color: #008000">/**UP ARROW*/</span></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 37 ,<span style="color: #008000">/**LEFT ARROW*/</span></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 16 <span style="color: #008000">/** SHIFT*/</span></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> ]</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> },</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> NO_OF_CHARACTERS_WITHOUT_COLON : 4,</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> REGEX : {</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> TWELVE : /^([0-1]?[0-2])([:][0-5]?[0-9])?$/,</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> TWENTY_FOUR : /^([2][0-3]|[0-1]?[0-9])([:][0-5]?[0-9])?$/</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> }</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> };</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">// Create some defaults, extending them with any options that were provided</span></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">var</span> settings = $.extend( {</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 'hourFormat' : '24',</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 'onError' : <span style="color: #0000ff">function</span>()</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">this</span>.value="<span style="color: #8b0000"></span>";</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> }</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> }, options);</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">// Method calling logic</span></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">if</span> (methods[options])</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">return</span> methods[options].apply(<span style="color: #0000ff">this</span>, <span style="color: #0000ff">Array</span>.<span style="color: #0000ff">prototype</span>.slice.call(<span style="color: #0000ff">arguments</span>, 1));</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> } <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (<span style="color: #0000ff">typeof</span> options === 'object' || !options)</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">return</span> methods.init.apply(<span style="color: #0000ff">this</span>, <span style="color: #0000ff">arguments</span>);</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> } <span style="color: #0000ff">else</span></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> $.error('Method ' + options + ' does not exist on jQuery.timefield');</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> }</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> };</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">})(jQuery);</pre></pre></strong> Sajjan Sarkarhttp://www.blogger.com/profile/07258575012965819548noreply@blogger.com0tag:blogger.com,1999:blog-3328123480609854402.post-24628054308076191192012-08-01T09:09:00.001-07:002012-08-01T09:20:29.986-07:00Generating Print Statements for Stored Procedure Arguments<p> </p> <p>Here’s a little utility I wrote which, given a target stored procedure name, generates PRINT statements for all the arguments. This can be included at the top of the target sproc and run to view the actual values passed at runtime.</p> <p>I was in two minds whether to post this at all, as it has very little use beyond trying to log runtime values for a target sproc which is called <em>internally</em> inside another sproc. For all other cases the SQL SERVER profiler should suffice. But, since we do have nested calls in our application and I thought maybe some one else does too, I am posting it.</p> <p><strong><font size="2">A word on TSQL_SPs Trace Template:</font></strong></p> <p>Now I know we can profile nested sproc calls using the TSQL_SPs Trace Template, but Profiler does not capture the actual values passed in to the nested call if they are dynamic.</p> <p>For example:</p> <p><strong>case 1: This gets profiled correctly</strong></p><pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CREATE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">CREATE</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=PROCEDURE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">PROCEDURE</a> OUTER_SPROC<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">(<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> :<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> :<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=EXEC&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">EXEC</a> INNER_SPROC '<span style="color: #8b0000">THIS</span>','<span style="color: #8b0000">IS</span>','<span style="color: #8b0000">HARD-CODED</span>'<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> :<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> :<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">)</pre>Profiler output:</pre><pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=EXEC&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">EXEC</a> INNER_SPROC '<span style="color: #8b0000">THIS</span>','<span style="color: #8b0000">IS</span>','<span style="color: #8b0000">HARD-CODED</span>'</pre></pre><br /><p><strong>case 2: This does not</strong></p><pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CREATE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">CREATE</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=PROCEDURE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">PROCEDURE</a> OUTER_SPROC<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">(<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DECLARE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">DECLARE</a> @VAR1 <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VARCHAR</a><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DECLARE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">DECLARE</a> @VAR2 <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VARCHAR</a><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DECLARE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">DECLARE</a> @VAR3 <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VARCHAR</a><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SET&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SET</a> @VAR1 = '<span style="color: #8b0000">THIS</span>'<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SET&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SET</a> @VAR1 = '<span style="color: #8b0000">IS</span>'<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SET&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SET</a> @VAR1 = '<span style="color: #8b0000">DYNAMIC</span>'<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> :<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=EXEC&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">EXEC</a> INNER_SPROC @VAR1,@VAR2,@VAR3<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> :<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> :<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">)</pre></pre><br /><div class="csharpcode"><pre class="alt"><span class="kwrd">EXEC</span> INNER_SPROC @VAR1,@VAR2,@VAR3</pre></div><br /><style type="text/css">.csharpcode, .csharpcode pre<br />{<br /> font-size: small;<br /> color: black;<br /> font-family: consolas, "Courier New", courier, monospace;<br /> background-color: #ffffff;<br /> /*white-space: pre;*/<br />}<br />.csharpcode pre { margin: 0em; }<br />.csharpcode .rem { color: #008000; }<br />.csharpcode .kwrd { color: #0000ff; }<br />.csharpcode .str { color: #006080; }<br />.csharpcode .op { color: #0000c0; }<br />.csharpcode .preproc { color: #cc6633; }<br />.csharpcode .asp { background-color: #ffff00; }<br />.csharpcode .html { color: #800000; }<br />.csharpcode .attr { color: #ff0000; }<br />.csharpcode .alt <br />{<br /> background-color: #f4f4f4;<br /> width: 100%;<br /> margin: 0em;<br />}<br />.csharpcode .lnum { color: #606060; }<br /></style><br /><br /><p></p><br /><p><strong><font size="3">And so, my little utility</font></strong></p><pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=USE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">USE</a> [UtilsDB]<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=GO&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">GO</a><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SET&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SET</a> ANSI_NULLS <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ON&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">ON</a><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=GO&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">GO</a><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SET&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SET</a> QUOTED_IDENTIFIER <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ON&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">ON</a><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=GO&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">GO</a><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #008000">-- =============================================</span><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #008000">-- Author: Sajjan Sarkar</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #008000">-- Create date: </span><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #008000">-- Description: Utility to generate print statements for a target sproc</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #008000">-- =============================================</span><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ALTER&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">ALTER</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=PROCEDURE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">PROCEDURE</a> [dbo].[usp_Utils_PrintArgs]<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> (<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> @DBName <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VARCHAR</a>(100) = '<span style="color: #8b0000"></span>' ,<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> @SprocName <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VARCHAR</a>(100) = '<span style="color: #8b0000"></span>'<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> )<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=AS&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">AS</a> <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=BEGIN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">BEGIN</a><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">-- SET NOCOUNT ON added to prevent extra result sets from</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">-- interfering with SELECT statements.</span><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SET&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SET</a> NOCOUNT <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ON&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">ON</a> ;<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DECLARE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">DECLARE</a> @execGetParms <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VARCHAR</a>(500)<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DECLARE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">DECLARE</a> @ArgName <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VARCHAR</a>(100)<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DECLARE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">DECLARE</a> @ArgType <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VARCHAR</a>(100)<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DECLARE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">DECLARE</a> @PrintString <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VARCHAR</a>(8000)<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">-- this string will contain the print statements</span><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SET&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SET</a> @PrintString = '<span style="color: #8b0000"></span>'<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">-- sp_procedure_params_rowset returns the arguments for a sproc. this has to be a dynamic </span><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">-- SQL so that we can run it for sprocs across databases</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SET&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SET</a> @execGetParms = '<span style="color: #8b0000">exec [</span>' + @DBName + '<span style="color: #8b0000">]..sp_procedure_params_rowset N''</span>' + @SprocName + '<span style="color: #8b0000">'',1,NULL,NULL</span>'<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">-- temp table to consume the results of the sp_procedure_params_rowset call</span><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CREATE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">CREATE</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=TABLE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">TABLE</a> #tmpArgs<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> (<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> PROCEDURE_CATALOG <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VARCHAR</a>(100) ,<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> PROCEDURE_SCHEMA <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VARCHAR</a>(100) ,<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> PROCEDURE_NAME <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VARCHAR</a>(100) ,<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> PARAMETER_NAME <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VARCHAR</a>(100) ,<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> ORDINAL_POSITION <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">INT</a> ,<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> PARAMETER_TYPE <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">INT</a> ,<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> PARAMETER_HASDEFAULT <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">INT</a> ,<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> PARAMETER_DEFAULT <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VARCHAR</a>(100) ,<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> IS_NULLABLE <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">INT</a> ,<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> DATA_TYPE <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">INT</a> ,<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> CHARACTER_MAXIMUM_LENGTH <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">INT</a> ,<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> CHARACTER_OCTET_LENGTH <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">INT</a> ,<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> NUMERIC_PRECISION <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">INT</a> ,<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> NUMERIC_SCALE <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">INT</a> ,<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> DESCRIPTION <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VARCHAR</a>(100) ,<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> TYPE_NAME <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VARCHAR</a>(100) ,<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> LOCAL_TYPE_NAME <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VARCHAR</a>(100)<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> )<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INSERT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">INSERT</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INTO&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">INTO</a> #tmpArgs<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> ( PROCEDURE_CATALOG ,<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> PROCEDURE_SCHEMA ,<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> PROCEDURE_NAME ,<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> PARAMETER_NAME ,<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> ORDINAL_POSITION ,<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> PARAMETER_TYPE ,<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> PARAMETER_HASDEFAULT ,<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> PARAMETER_DEFAULT ,<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> IS_NULLABLE ,<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> DATA_TYPE ,<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> CHARACTER_MAXIMUM_LENGTH ,<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> CHARACTER_OCTET_LENGTH ,<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> NUMERIC_PRECISION ,<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> NUMERIC_SCALE ,<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> DESCRIPTION ,<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> TYPE_NAME ,<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> LOCAL_TYPE_NAME<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> )<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=EXEC&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">EXEC</a> ( @execGetParms<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> )<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">-- loop thru each arg and build the string </span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DECLARE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">DECLARE</a> cursorDB <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CURSOR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">CURSOR</a> READ_ONLY<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FOR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">FOR</a><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SELECT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SELECT</a> PARAMETER_NAME ,<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> TYPE_NAME<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FROM&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">FROM</a> #tmpArgs <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=AS&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">AS</a> TA<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ORDER&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">ORDER</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=BY&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">BY</a> ORDINAL_POSITION <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ASC&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">ASC</a> <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=OPEN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">OPEN</a> cursorDB<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FETCH&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">FETCH</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NEXT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">NEXT</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FROM&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">FROM</a> cursorDB <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INTO&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">INTO</a> @ArgName, @ArgType<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=WHILE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">WHILE</a> ( @@fetch_status <> -1 ) <br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=BEGIN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">BEGIN</a><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=IF&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">IF</a> ( @@fetch_status <> -2 ) <br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=BEGIN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">BEGIN</a> <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=IF&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">IF</a> @ArgName != '<span style="color: #8b0000">@RETURN_VALUE</span>' <span style="color: #008000">-- ignore the default @RETURN_VALUE arg</span><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=BEGIN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">BEGIN</a><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">-- cast to string depending on type of arg</span><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SET&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SET</a> @PrintString = @PrintString + '<span style="color: #8b0000"> PRINT ''</span>' + @ArgName + '<span style="color: #8b0000">:''+ CASE WHEN </span>' + @ArgName + '<span style="color: #8b0000"> IS NULL THEN ''NULL'' ELSE </span>'<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> + <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CASE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">CASE</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=WHEN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">WHEN</a> @ArgType <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=IN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">IN</a> ( '<span style="color: #8b0000">int</span>', '<span style="color: #8b0000">bit</span>', '<span style="color: #8b0000">numeric</span>' ) <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=THEN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">THEN</a> '<span style="color: #8b0000"> CAST(</span>' + @ArgName + '<span style="color: #8b0000"> AS VARCHAR(100)) </span>'<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=WHEN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">WHEN</a> @ArgType <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=LIKE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">LIKE</a> '<span style="color: #8b0000">date%</span>' <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=THEN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">THEN</a> '<span style="color: #8b0000"> CAST(</span>' + @ArgName + '<span style="color: #8b0000"> AS VARCHAR)</span>'<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ELSE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">ELSE</a> @ArgName<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=END&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">END</a> + '<span style="color: #8b0000"> END </span>' + <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">CHAR</a>(13)<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=END&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">END</a> <br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=END&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">END</a> <br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FETCH&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">FETCH</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NEXT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">NEXT</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FROM&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">FROM</a> cursorDB <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INTO&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">INTO</a> @ArgName, @ArgType<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=END&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">END</a><span style="color: #008000">--WHILE (@@fetch_status <> -1) </span><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CLOSE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">CLOSE</a> cursorDB<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DEALLOCATE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">DEALLOCATE</a> cursorDB<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">-- print output</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=PRINT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">PRINT</a> @PrintString<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DROP&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">DROP</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=TABLE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">TABLE</a> #tmpArgs<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=END&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">END</a><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre></pre><br /><style type="text/css">.csharpcode, .csharpcode pre<br />{<br /> font-size: small;<br /> color: black;<br /> font-family: consolas, "Courier New", courier, monospace;<br /> background-color: #ffffff;<br /> /*white-space: pre;*/<br />}<br />.csharpcode pre { margin: 0em; }<br />.csharpcode .rem { color: #008000; }<br />.csharpcode .kwrd { color: #0000ff; }<br />.csharpcode .str { color: #006080; }<br />.csharpcode .op { color: #0000c0; }<br />.csharpcode .preproc { color: #cc6633; }<br />.csharpcode .asp { background-color: #ffff00; }<br />.csharpcode .html { color: #800000; }<br />.csharpcode .attr { color: #ff0000; }<br />.csharpcode .alt <br />{<br /> background-color: #f4f4f4;<br /> width: 100%;<br /> margin: 0em;<br />}<br />.csharpcode .lnum { color: #606060; }<br /></style><br /><br /><p><strong><font size="3">Example Usage:</font></strong></p><br /><div class="csharpcode"><pre class="alt"><span class="kwrd">exec</span> UtilsDB..usp_Utils_PrintArgs <span class="str">'MyTargetDB'</span> ,<span class="str">'INNER_SPROC'</span></pre></div><br /><style type="text/css">.csharpcode, .csharpcode pre<br />{<br /> font-size: small;<br /> color: black;<br /> font-family: consolas, "Courier New", courier, monospace;<br /> background-color: #ffffff;<br /> /*white-space: pre;*/<br />}<br />.csharpcode pre { margin: 0em; }<br />.csharpcode .rem { color: #008000; }<br />.csharpcode .kwrd { color: #0000ff; }<br />.csharpcode .str { color: #006080; }<br />.csharpcode .op { color: #0000c0; }<br />.csharpcode .preproc { color: #cc6633; }<br />.csharpcode .asp { background-color: #ffff00; }<br />.csharpcode .html { color: #800000; }<br />.csharpcode .attr { color: #ff0000; }<br />.csharpcode .alt <br />{<br /> background-color: #f4f4f4;<br /> width: 100%;<br /> margin: 0em;<br />}<br />.csharpcode .lnum { color: #606060; }<br /></style><br /><br /><p><strong>output</strong></p><pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=PRINT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">PRINT</a> '<span style="color: #8b0000">@VAR1:</span>'+ <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CASE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">CASE</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=WHEN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">WHEN</a> @VAR1 <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=IS&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">IS</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NULL&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">NULL</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=THEN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">THEN</a> '<span style="color: #8b0000">NULL</span>' <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ELSE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">ELSE</a> @VAR1 <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=END&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">END</a> <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=PRINT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">PRINT</a> '<span style="color: #8b0000">@VAR2:</span>'+ <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CASE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">CASE</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=WHEN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">WHEN</a> @VAR2 <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=IS&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">IS</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NULL&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">NULL</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=THEN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">THEN</a> '<span style="color: #8b0000">NULL</span>' <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ELSE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">ELSE</a> @VAR2 <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=END&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">END</a> <br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=PRINT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">PRINT</a> '<span style="color: #8b0000">@VAR2:</span>'+ <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CASE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">CASE</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=WHEN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">WHEN</a> @VAR2 <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=IS&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">IS</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NULL&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">NULL</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=THEN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">THEN</a> '<span style="color: #8b0000">NULL</span>' <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ELSE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">ELSE</a> @VAR2 <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=END&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">END</a> </pre><style type="text/css">.csharpcode, .csharpcode pre<br />{<br /> font-size: small;<br /> color: black;<br /> font-family: consolas, "Courier New", courier, monospace;<br /> background-color: #ffffff;<br /> /*white-space: pre;*/<br />}<br />.csharpcode pre { margin: 0em; }<br />.csharpcode .rem { color: #008000; }<br />.csharpcode .kwrd { color: #0000ff; }<br />.csharpcode .str { color: #006080; }<br />.csharpcode .op { color: #0000c0; }<br />.csharpcode .preproc { color: #cc6633; }<br />.csharpcode .asp { background-color: #ffff00; }<br />.csharpcode .html { color: #800000; }<br />.csharpcode .attr { color: #ff0000; }<br />.csharpcode .alt <br />{<br /> background-color: #f4f4f4;<br /> width: 100%;<br /> margin: 0em;<br />}<br />.csharpcode .lnum { color: #606060; }<br /></style></pre><br /><p>It also handles casting non-string arguments to their string forms, so if your arguments were String,INT and DATETIME</p><pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=PRINT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">PRINT</a> '<span style="color: #8b0000">@VAR1:</span>'+ <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CASE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">CASE</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=WHEN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">WHEN</a> @VAR1 <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=IS&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">IS</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NULL&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">NULL</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=THEN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">THEN</a> '<span style="color: #8b0000">NULL</span>' <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ELSE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">ELSE</a> @VAR1 <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=END&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">END</a> <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=PRINT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">PRINT</a> '<span style="color: #8b0000">@VAR2:</span>'+ <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CASE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">CASE</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=WHEN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">WHEN</a> @VAR2 <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=IS&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">IS</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NULL&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">NULL</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=THEN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">THEN</a> '<span style="color: #8b0000">NULL</span>' <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ELSE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">ELSE</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CAST&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">CAST</a>(@VAR2 <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=AS&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">AS</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VARCHAR</a>(100)) <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=END&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">END</a> <br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=PRINT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">PRINT</a> '<span style="color: #8b0000">@VAR3:</span>'+ <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CASE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">CASE</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=WHEN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">WHEN</a> @VAR3 <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=IS&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">IS</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NULL&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">NULL</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=THEN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">THEN</a> '<span style="color: #8b0000">NULL</span>' <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ELSE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">ELSE</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CAST&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">CAST</a>(@VAR3 <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=AS&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">AS</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VARCHAR</a>) <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=END&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">END</a> </pre></pre><br /><p>Now just paste the PRINT statements into INNER_SPROC and run the outer one :)</p> Sajjan Sarkarhttp://www.blogger.com/profile/07258575012965819548noreply@blogger.com0tag:blogger.com,1999:blog-3328123480609854402.post-8513483217227641882011-12-14T07:43:00.001-08:002011-12-14T07:57:36.332-08:00Quick Way to Disable Adobe Flash on Common Browsers<p><strong>Sometimes you might want to disable Flash to prevent annoying ads, to improve page speeds,to test your web code etc. Here are some quick and dirty ways to do it :)</strong></p> <p><strong>Internet Explorer:</strong></p> <p><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmIetghC3L3sowJZAYhCp9cxQFqJ_IfHxIKCUUFbJ_3O-_88pFzMsmIjn69-DF_Cl-1cae5jCt59nTA6v6ZR3kYcCIbEJMnR_JJG_hBahzHibkDfebFrTqTV9noxNVShhNd7A_7GWZ8Q_8/s1600-h/image%25255B11%25255D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEis_AtUtFhlWgjBMhvdGMnc9s8BBC68USmhvG4Np0e4sgD89Ou-makwg1xI9ARARVXM_Tf3d3xMCKepxMl-HS35aHgemLO1Dv4y6pD8M2GskCDt7XXn4k2dVsxjtiAiwMU5lJBlFCKRH0ks/?imgmax=800" width="174" height="176"></a> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQmFxnVtPNcGYeQvwnB59JS1BbM_Ud0wTkl72EYP6ETAtndYrz2EHEjebajcqw93Tfp0EALycuupjlujPa2XYNqiCihXrG9kWQ9a18AwJ-VpODVaiuDiSiSf-5ZFW5D6yFZvZZmTxCvc3V/s1600-h/image%25255B10%25255D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0VQRqcUnFwHu27HkQQvwnvu31cOjftV9UnQ0i6kOM0uX_BWP5J7q-D78NsPJwRZKFXj3JGTteD3g4Epwugx7bbBSO2ykhVdIvq1puWURh06-QcZQlSPNKFdJt9kt6HzmVt6-RIX7U16-d/?imgmax=800" width="650" height="176"></a> </p> <p></p> <p></p> <p>Right-click and say disable.</p> <p><strong>FireFox:</strong></p> <p>Press CTRL+SHIFT+A</p> <p><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9aRvYlgG8LiHLqg4wPrwZ5HdrE4xiVW-n-YwZkUujLgpA9Ne2J2aR_jaC_Z7xpEZbE0QrBKEuKUKs-Ct3nSS2ngu-ENrLxU3SmW1Nk461SODveZh39tIuj5qxuI8h08FPPNU1X-6i56BP/s1600-h/image%25255B15%25255D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiCityNLNBZuawXeFMH-HxPMwpn-IVcKDEQOs9xwx24UWRFI4HQHtZi7psWO2-CFHrubo9ODqQ9k3dC83bWk8SSbnBw5SgvoXFHfexle4d4QTSrNsHNDjDFZvZhFY0Mj6qXuydasJB7PlWC/?imgmax=800" width="460" height="256"></a> </p> <p><strong>Chrome:</strong></p> <p>Type about:plugins in the address bar and hit enter</p> <p><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2z7bRkMz1HBHugEo_maU0eI9DPY6XKiltmBD4x9GUV2eR00rMBbKo1T3gNyAmRIsSkFhZdca6ecfN4_tjUm-c79IWTcpVs5_ThJwYi02jCnfsLcGPGi_YOwy5357f_5nBsqoVVbT51ytV/s1600-h/image%25255B19%25255D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTobBPDl2R3DjtSCv216ubHXzZDvZbWHyZoOUhkm7Y2Tmo-qARB25FGBgGkF6HDQjPyShurjC6D0ubnUJC7Y-hrX01VTpDEjjhNYzykqJOsGcssUyt03xSsxzTItfofi3oddFg8semYPbA/?imgmax=800" width="315" height="168"></a> </p> <p><strong>Safari:</strong></p> <p><img src="http://0.tqn.com/d/browsers/1/0/L/M/-/-/change-default-browser-safari-4-2.png" width="199" height="239"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiThatBeTkpNYQ4MfuY52OLIA1evcN4ufBp426Emwcy70LGJPfhshmJeELnT-OZeojhSbC4xfFE3DQNtiOh7lA6w62cGxR8br99W-BGtHo5Eo-PlyDO_mxzBknyttdo4QMM5fwkIweQs-37/s1600-h/image%25255B27%25255D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3mb9YFBW0WZT42VWCcdgKnBEibHDl1fnKojH8oXEGkQrc7k8Pbo2l-lq6Dy_YEyd0XVbqeixIRTd5HNmwLftB1QPEPOZj4dBQtU0l-wwZQ_fXeN2WhROMpT9nvbBtQ-3rwOblqVXbbTcv/?imgmax=800" width="462" height="315"></a> Uncheck Enable Plugins.</p> <p> </p> <p><strong>Opera:</strong></p> <p>Hit F12 and uncheck Enable Plugin</p> <p></p> <p><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHjRm1ZuMkMgnbsM_OjuwKWS4feRpMfMeGXD202j0fm2Pe8ZW-7gYrQR4PTA3dxLYw8hpQPDmADssDXiPgz8zyUXyUrzcoUSF34y0o9NPobziHt3SjsmwwdNsYp7b5y0fBkqxPgWLYjq08/s1600-h/image%25255B30%25255D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgV-2i5lR9f2bh5AC2oeN8tW6lcGorDZAzHpnn7wvZa3F3xsFSG9BcHFNKS9eeLc_86NIPwwimFL5tpU1wVcDQWx6m2YPq4JoiNrcSwNKC1r6_OCFCCnv_B9QQnf_Aj08vKS_UX1c0ZuJwa/?imgmax=800" width="161" height="244"></a></p> Sajjan Sarkarhttp://www.blogger.com/profile/07258575012965819548noreply@blogger.com2tag:blogger.com,1999:blog-3328123480609854402.post-31045353141021973482011-10-21T14:16:00.001-07:002011-10-24T11:42:14.629-07:00jQuery Plugin for Zebra Striping tables including dynamic Row Insertion Deletion<p> </p> <p>This is a simple jQuery plugin which applies alternating CSS for rows in a table. <p><strong><em>Offical jQuery site :</em></strong> <blockquote> <p><strong><em><a href="http://plugins.jquery.com/project/ZebraStriping" target="_blank">http://plugins.jquery.com/project/ZebraStriping</a></em></strong></p></blockquote> <p><strong><em>Download (right-click and save as..):</em></strong> <blockquote> <p><strong><em><a href="http://sajjansarkar.zxq.net/jquery/zebra/jquery.zebra.js" target="_blank">jquery.zebra.js</a></em></strong></p></blockquote> <p><em><strong>Features:<br></strong></em>1. Alternate row CSS<br>2. Row hover CSS<br>3. Handles row insertion/deletion dynamically via javascript(does not work for IE as it does not support <font face="Courier New">DOMNodeInserted</font> event.)<br>4. Option to exclude header row<br>5. Works with THEAD and TBODY too. </p> <p>note: workaround for IE is the zebra plugin must be called again if a new row is added via javascript. <p>Example usage: <p>Save the code at the bottom of the page as jquery.zebra.js. <p>Include it in the HEAD section of your page like:<pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><script src="<span style="color: #8b0000">jquery-latest.js</span>"></script><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><script src="<span style="color: #8b0000">jquery.zebra.js</span>"></script></pre></pre><br /><p>And then call it on a table like:</p><pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">$("<span style="color: #8b0000">#targetTable,#targetTable2</span>").zebra({<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> excludeHeader:<span style="color: #0000ff">true</span>,<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> hover:'hoverClass',<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> odd:'oddClass',<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> even:'evenClass'<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> });</pre></pre><pre> </pre><br /><p>Plugin Code:</p><pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #008000">/** <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * Namespace to avoid variable name collision<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * Author: Sajjan Sarkar<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * */</span><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">(<span style="color: #0000ff">function</span>($)<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">{<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** Plugin starting point,options is optional but this plugin does nothing if options is null */</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> $.fn.zebra = <span style="color: #0000ff">function</span>(options)<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** Current settings */</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">var</span> settings = {<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> even : '',<span style="color: #008000">/** CSS class name for even rows */</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> odd : '',<span style="color: #008000">/** CSS class name for odd rows */</span><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> hover : '',<span style="color: #008000">/** CSS class name rows when hovered */</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> excludeHeader : <span style="color: #0000ff">false</span> <span style="color: #008000">/** true to exclude thead/ first row */</span><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> };<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** to maintain jQuery chainability */</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.each(<span style="color: #0000ff">function</span>()<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** local variables identifying whether the table has thead/tbody */</span><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">var</span> hasTHead = $("<span style="color: #8b0000">thead</span>", <span style="color: #0000ff">this</span>).<span style="color: #0000ff">length</span> == 0 ? <span style="color: #0000ff">false</span> : <span style="color: #0000ff">true</span>;<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">var</span> hasTBody = $("<span style="color: #8b0000">thead</span>", <span style="color: #0000ff">this</span>).<span style="color: #0000ff">length</span> == 0 ? <span style="color: #0000ff">false</span> : <span style="color: #0000ff">true</span>; <span style="color: #008000">// not used as of now</span><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** Variable to store reference to the table as the this keyword in the<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * apply() function would point to the DOMWindow object due closure.<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * <br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> */</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">var</span> gTable = <span style="color: #0000ff">this</span>;<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** Store the table header jQuery collection based on whether there is a THEAD , also for performance */</span><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">if</span> (hasTHead)<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">var</span> headerSelector = $("<span style="color: #8b0000">thead > tr</span>", <span style="color: #0000ff">this</span>);<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">else</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">var</span> headerSelector = $("<span style="color: #8b0000">tr:first-child</span>", <span style="color: #0000ff">this</span>);<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** If options exist, lets merge them with our default settings */</span><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">if</span> (options)<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> $.extend(settings, options);<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> }<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * For browsers that support this (IE does not support it), everytime the DOM structure is changed<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * we test if a new row is added to the table and apply the plugin to the entire table again<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * as the row could have been added in the middle.<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * Note: for IE, the zebra plugin must be called again if a new row is added<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> * */</span><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> $(<span style="color: #0000ff">this</span>).bind('DOMNodeInserted', <span style="color: #0000ff">function</span>(event)<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** Check if a row has been inserted */</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">if</span> (event.relatedNode.nodeName == "<span style="color: #8b0000">TR</span>")<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> apply(); <br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> }<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> });<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** internal function to apply the plugin logic. */</span><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">var</span> apply = <span style="color: #0000ff">function</span>()<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** Apply zebra striping to header row/s */</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">if</span> (hasTHead)<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> $("<span style="color: #8b0000">thead > tr:even</span>", gTable).removeClass(settings.even + "<span style="color: #8b0000"> </span>" + settings.odd).addClass(settings.even);<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> $("<span style="color: #8b0000">thead > tr:odd</span>", gTable).removeClass(settings.even + "<span style="color: #8b0000"> </span>" + settings.odd).addClass(settings.odd);<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> }<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** Apply zebra striping to body rows */</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> $("<span style="color: #8b0000">tbody > tr:even</span>", gTable).removeClass(settings.even + "<span style="color: #8b0000"> </span>" + settings.odd).addClass(settings.even);<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> $("<span style="color: #8b0000">tbody > tr:odd</span>", gTable).removeClass(settings.even + "<span style="color: #8b0000"> </span>" + settings.odd).addClass(settings.odd);<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** Apply hover logic to entire table */</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> $("<span style="color: #8b0000">tr</span>", gTable).hover(<span style="color: #0000ff">function</span>()<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> $(<span style="color: #0000ff">this</span>).addClass(settings.hover);<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> }, <span style="color: #0000ff">function</span>()<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> $(<span style="color: #0000ff">this</span>).removeClass(settings.hover);<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> });<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** Exclude header if option is set */</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #0000ff">if</span> (settings.excludeHeader)<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> {<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> headerSelector.removeClass(settings.even + "<span style="color: #8b0000"> </span>" + settings.odd);<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> headerSelector.unbind('mouseenter mouseleave');<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> }<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> };<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">/** Call it */</span><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> apply();<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <span style="color: #008000">// plugin code here</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> });<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> };<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">})(jQuery);<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre></pre><br /><p><strong>Sample:</strong></p><br /><p><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_EggKXq-JCGSCqCaht_9NhbCUbQGeKuPaCVxSne2gnTHEUVChjBM-UeWhHZ6AHz3RWwE4M40z-P-roixw_ZZhXTuP5N-LNadoLM1b90sImSjZuQio9aW7qxpTPz1t4K6XmpUCfjGq__7o/s1600-h/image%25255B12%25255D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRtCyOPRrcjP4kLxpHSOo2i2goMK4nV7ZzYdPpHdnrS_tOpwHPXno13yQUaK4Id7UQwfGjx7IN-cE0-951CQJ3ChiNwHicdVHHMU5SJTQ6l1XRMHLYtSNgZcE0LfBBvJsbk0lUeRDT93_a/?imgmax=800" width="885" height="230"></a></p> Sajjan Sarkarhttp://www.blogger.com/profile/07258575012965819548noreply@blogger.com0tag:blogger.com,1999:blog-3328123480609854402.post-91551768500046823052011-09-14T12:03:00.001-07:002011-09-14T12:11:38.121-07:00Simple SQL Server Split Function<p> </p> <p>I know there are a lot of solutions to this on the internet, here’s mine! </p> <p>Sample use-cases:</p> <p><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi4xu3EzLhZP5IvAC_K0fgyjM1gr-gbm6p8efUTZ8-kVyajFKlyU0Lb-0Fjgc1kv8aMDQ7zJp6rha8lQjHeyjOdL1ksKgqTlCG9O-rFlqQU7ZErHkpxPpifCg-p0wEdCYjjFJHwjtcH_NI2/s1600-h/untitled%25255B1%25255D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="untitled" border="0" alt="untitled" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOJcMpqvlgbiGjMvs9cSvRdz19z_ZpOm3Qkw7T8JtaMxzkfiay0sAjRrJS1ojI4vBrHBc2XRslAb95ntZWnKH87LdS7A1CpkVj3STIE4Rt4BVHURSeZGP4yMIgPEMcqtmLPqfPkXoXhlS8/?imgmax=800" width="605" height="483"></a> </p> <p> </p> <p>Code:</p> <p> </p><pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CREATE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">CREATE</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FUNCTION&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">FUNCTION</a> [dbo].[Split]<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> (<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> @list <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NTEXT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">NTEXT</a> ,<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> @Delim <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">CHAR</a>(1) = '<span style="color: #8b0000">,</span>'<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> )<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=RETURNS&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">RETURNS</a> @tbl <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=TABLE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">TABLE</a><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> (<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> items <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VARCHAR</a>(4000) <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NOT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">NOT</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NULL&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">NULL</a><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> )<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=AS&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">AS</a> <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=BEGIN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">BEGIN</a><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DECLARE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">DECLARE</a> @pos <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">INT</a> ,<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> @textpos <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">INT</a> ,<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> @chunklen <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SMALLINT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SMALLINT</a> ,<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> @str <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NVARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">NVARCHAR</a>(4000) ,<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> @tmpstr <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NVARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">NVARCHAR</a>(4000) ,<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> @leftover <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NVARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">NVARCHAR</a>(4000)<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=IF&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">IF</a> @Delim <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=IS&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">IS</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NULL&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">NULL</a> <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=BEGIN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">BEGIN</a><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INSERT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">INSERT</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INTO&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">INTO</a> @tbl<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> ( items )<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VALUES&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VALUES</a> ( <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CAST&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">CAST</a>(@list <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=AS&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">AS</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VARCHAR</a>) <span style="color: #008000">-- items - varchar(4000)</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> )<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=RETURN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">RETURN</a><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=END&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">END</a><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SET&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SET</a> @textpos = 1<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SET&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SET</a> @leftover = '<span style="color: #8b0000"></span>'<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=WHILE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">WHILE</a> @textpos <= DATALENGTH(@list) / 2 <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=BEGIN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">BEGIN</a><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SET&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SET</a> @chunklen = 4000 - DATALENGTH(@leftover) / 2<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SET&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SET</a> @tmpstr = LTRIM(@leftover + <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SUBSTRING&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SUBSTRING</a>(@list, @textpos,<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> @chunklen))<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SET&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SET</a> @textpos = @textpos + @chunklen<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SET&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SET</a> @pos = CHARINDEX(@Delim, @tmpstr)<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=WHILE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">WHILE</a> @pos > 0 <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=BEGIN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">BEGIN</a><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SET&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SET</a> @str = <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SUBSTRING&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SUBSTRING</a>(@tmpstr, 1, @pos - 1)<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=IF&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">IF</a> LTRIM(RTRIM(@str)) <> '<span style="color: #8b0000"></span>' <br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INSERT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">INSERT</a> @tbl<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> ( items )<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VALUES&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VALUES</a> ( @str )<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SET&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SET</a> @tmpstr = LTRIM(<a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SUBSTRING&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SUBSTRING</a>(@tmpstr, @pos + 1,<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> LEN(@tmpstr)))<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SET&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SET</a> @pos = CHARINDEX(@Delim, @tmpstr)<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=END&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">END</a><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SET&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SET</a> @leftover = @tmpstr<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=END&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">END</a><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=IF&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">IF</a> LTRIM(RTRIM(@leftover)) <> '<span style="color: #8b0000"></span>' <br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INSERT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">INSERT</a> @tbl<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> ( items )<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VALUES&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VALUES</a> ( @leftover )<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=RETURN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">RETURN</a><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=END&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">END</a><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre></pre> Sajjan Sarkarhttp://www.blogger.com/profile/07258575012965819548noreply@blogger.com1tag:blogger.com,1999:blog-3328123480609854402.post-59664228163852313802011-04-26T13:26:00.001-07:002012-01-10T06:13:28.079-08:00Searching Stored Procedures Across SQL Server Databases<div style="text-align: left" dir="ltr" trbidi="on"><br><span style="color: grey">Here’s a little utility SP I wrote which searches all stored procedures in all databases in your SQL Server Instance for an input string.</span><br><span style="color: grey">In SQL Server Management Studio 2008 there is an option to do filtered searches but it only look inside the selected database. Now honestly I am not sure you should be in a position where you would want to search stored procedures across Databases, but if you do find the need, hope this is useful!</span><br><strong><span style="color: grey">Example:</span><br></strong><span style="color: grey">If you compile this utility into DB1, then simply say :</span><br></div> <blockquote style="text-align: left" dir="ltr" trbidi="on"><pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=EXEC&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99"><span style="color: grey">EXEC</span></a><span style="color: grey"> DB1..sp_SearchInSPs '<span style="color: darkred">mySearchString</span>'</span></pre></pre></blockquote><br /><div style="text-align: left" dir="ltr" trbidi="on"><strong>[UPDATE:]Download Link:</strong></div><br /><div style="text-align: left" dir="ltr" trbidi="on"><strong><br></strong><a href="http://sajjansarkar.zxq.net/sql/sp_SearchInSPs.sql">sp_SearchInSPs.sql</a></div><br /><div style="text-align: left" dir="ltr" trbidi="on"><br><strong><span style="color: grey">Code:</span><br><br></strong></div><br /><div style="text-align: left" dir="ltr" trbidi="on"><pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 1: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SET&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SET</a> ANSI_NULLS <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ON&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">ON</a></pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 2: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=GO&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">GO</a></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 3: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SET&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SET</a> QUOTED_IDENTIFIER <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ON&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">ON</a></pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 4: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=GO&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">GO</a></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 5: <span style="color: green">-- =============================================</span></pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 6: <span style="color: green">-- Author: Sajjan Sarkar with help of SungUngKim</span></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 7: <span style="color: green">-- Create date: April 26, 2011</span></pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 8: <span style="color: green">-- Description: stored procedure to search all SPs for a given string in ALL databases. </span></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 9: <span style="color: green">-- =============================================</span></pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 10: <span style="color: green">-- Sample to run</span></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 11: <span style="color: green">--EXEC DB1..sp_SearchInSPs 'BillingOvertimeCalcFactor'</span></pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 12: </pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 13: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ALTER&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">ALTER</a> <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=PROCEDURE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">PROCEDURE</a> [dbo].[sp_SearchInSPs] </pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 14: (</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 15: <span style="color: green">-- Add the parameters for the stored procedure here</span></pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 16: @SearchString <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VARCHAR</a>(100) = '<span style="color: darkred"></span>' </pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 17: ) </pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 18: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=AS&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">AS</a> </pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 19: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=BEGIN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">BEGIN</a></pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 20: <span style="color: green">--Declare </span></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 21: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DECLARE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">DECLARE</a> @SString <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NVARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">NVARCHAR</a>(50)</pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 22: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DECLARE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">DECLARE</a> @getdbname SYSNAME</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 23: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DECLARE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">DECLARE</a> @sqlstm <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NVARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">NVARCHAR</a>(1000)</pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 24: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CREATE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">CREATE</a> <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=TABLE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">TABLE</a> #tmp</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 25: (</pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 26: DBName <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VARCHAR</a>(100) ,</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 27: SPName <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VARCHAR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">VARCHAR</a>(100)</pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 28: )</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 29: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DECLARE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">DECLARE</a> dbname <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CURSOR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">CURSOR</a></pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 30: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FOR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">FOR</a></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 31: <span style="color: green">--get all the names of the Databases in order by name</span></pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 32: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SELECT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SELECT</a> '<span style="color: darkred">[</span>' + name + '<span style="color: darkred">]</span>'</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 33: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FROM&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">FROM</a> master.dbo.sysdatabases </pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 34: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ORDER&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">ORDER</a> <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=BY&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">BY</a> name </pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 35: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=OPEN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">OPEN</a> dbname</pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 36: </pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 37: <span style="color: green">--Get the first Name</span></pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 38: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FETCH&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">FETCH</a> <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NEXT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">NEXT</a> <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FROM&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">FROM</a> dbname <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INTO&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">INTO</a> @getdbname</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 39: </pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 40: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=WHILE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">WHILE</a> @@FETCH_STATUS = 0 </pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 41: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=BEGIN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">BEGIN</a></pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 42: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=PRINT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">PRINT</a> @getdbname</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 43: <span style="color: green">--set the search string</span></pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 44: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SET&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SET</a> @SString = @SearchString</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 45: </pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 46: <span style="color: green">--append the search pattern</span></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 47: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SET&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SET</a> @SString = '<span style="color: darkred">%</span>' + @SString + '<span style="color: darkred">%</span>'</pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 48: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SET&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SET</a> @sqlstm = '<span style="color: darkred">SELECT DISTINCT ''</span>' + @getdbname + '<span style="color: darkred">'',</span></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 49: s.name </pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 50: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FROM&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">FROM</a> '<span style="color: darkred"> + @getdbname + </span>'.dbo.syscomments c</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 51: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INNER&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">INNER</a> <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=JOIN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">JOIN</a> '<span style="color: darkred"> + @getdbname + </span>'.dbo.sysobjects s</pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 52: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ON&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">ON</a> c.id = s.id</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 53: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=WHERE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">WHERE</a> s.type <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=IN&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">IN</a> ( '<span style="color: darkred"></span>'p'<span style="color: darkred"></span>', '<span style="color: darkred"></span>'tr'<span style="color: darkred"></span>' )</pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 54: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=AND&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">AND</a> c.<a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=text&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">text</a> <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=LIKE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">LIKE</a> '<span style="color: darkred"></span>'%'<span style="color: darkred">+@SString+</span>'%'<span style="color: darkred"></span>'</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 55: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ORDER&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">ORDER</a> <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=BY&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">BY</a> [Name]'<span style="color: darkred"><br></span></pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 56: </pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 57: </pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 58: <span style="color: green">--Execute the Query</span></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 59: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INSERT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">INSERT</a> <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INTO&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">INTO</a> #tmp</pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 60: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=EXEC&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">EXEC</a> ( @sqlstm</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 61: )</pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 62: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FETCH&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">FETCH</a> <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NEXT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">NEXT</a> <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FROM&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">FROM</a> dbname <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INTO&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">INTO</a> @getdbname</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 63: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=END&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">END</a></pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 64: </pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 65: <span style="color: green">--Close the Cursor and Deallocate it from memory</span></pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 66: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CLOSE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">CLOSE</a> dbname</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 67: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DEALLOCATE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">DEALLOCATE</a> dbname </pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 68: </pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 69: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SELECT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SELECT</a> *</pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 70: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FROM&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">FROM</a> #tmp <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=AS&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">AS</a> T</pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 71: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DROP&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">DROP</a> <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=TABLE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">TABLE</a> #tmp</pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 72: </pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 73: <a style="color: blue" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=END&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">END</a></pre><pre style="background-color: white; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 74: </pre></pre></div> Sajjan Sarkarhttp://www.blogger.com/profile/07258575012965819548noreply@blogger.com5tag:blogger.com,1999:blog-3328123480609854402.post-59085219272294729392010-08-13T09:13:00.001-07:002010-08-13T11:11:05.010-07:00Autosuggest Example Using ColdFusion<p>So I got a chance to try this one out. I’ve written auto-suggest boxes in GXT and plain old JS and while its a little harder to get the same level of control and customization ,CF8’s autosuggest is pretty neat for most simple use-cases and for those wanting to whip up an app quickly. I know CF9 has much better handles to the autosuggest object (looking forward to that <a href="http://www.adobe.com/support/documentation/en/coldfusion/901/cf901features.pdf" target="_blank">here's a preview</a>), but till then ..here goes!</p> <p>Here’s an example of a user looking up an employee by last name.</p> <p>In this example I use 2 files: one (loopkup.cfm) to display the autosuggest box and the other (a CFC ,MCRequestServer.cfc) to fetch data for it. </p> <p>First the auto-suggest box code:</p><pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 1: <!doctype html public "<span style="color: #8b0000">-//w3c//dtd html 4.01 transitional//en</span>" "<span style="color: #8b0000">http://www.w3.org/tr/html4/loose.dtd</span>"><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 2: <html><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 3: <head><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 4: <meta http-equiv="<span style="color: #8b0000">content-type</span>" content="<span style="color: #8b0000">text/html; charset=iso-8859-1</span>"><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 5: <title>Search Trainees</title><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 6: <link href="<span style="color: #8b0000">AppStyles.css</span>" rel="<span style="color: #8b0000">stylesheet</span>" type="<span style="color: #8b0000">text/css</span>" /><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 7: <script><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 8: <span style="color: #008000">/**<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 9: * This method handles all bind errors. I'm just alerting.<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 10: * args:<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 11: * i. HTTPStatusCode- Error Code.number<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 12: * ii.message- any message you want to display<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 13: */</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 14: function handleBindError(HTTPStatusCode,message)<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 15: {<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 16: alert("<span style="color: #8b0000">Error Code: </span>"+HTTPStatusCode+"<span style="color: #8b0000">:\n</span>"+message);<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 17: }<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 18: <span style="color: #008000">/**<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 19: * This method attaches an itemSelect event handler to handle<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 20: * selections (using both mouse and keys).Called by ajaxOnLoad below.<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 21: */</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 22: var init = function()<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 23: {<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 24: <span style="color: #008000">/** get the autosuggest object */</span><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 25: autosuggestobj = ColdFusion.objectCache['TraineeSearchBox'];<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 26: <span style="color: #008000">/** attache an itemselect event handler*/</span><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 27: autosuggestobj.itemSelectEvent.subscribe(handleSelection);<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 28: }<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 29: <span style="color: #008000">/**<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 30: * This method handles all selections from the autosuggest list<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 31: * arg-<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 32: * i. event-the event name. in this case "itemSelect"<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 33: * ii.args- an array with the follwing data<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 34: * args[0]- Name/ID of the autocomplete component<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 35: * args[1]- The actual <LI> element that the user selected<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 36: * args[2]- The value selected. <br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 37: */</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 38: var handleSelection = function(event,args)<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 39: {<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 40: <span style="color: #008000">/** Display selection, you could do ur custom code here */</span><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 41: document.getElementById("<span style="color: #8b0000">SearchTraineeSpan</span>").innerHTML=args[2];<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 42: }<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 43: </script><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 44: </head><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 45: <body bgcolor="<span style="color: #8b0000">beige</span>"> <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 46: <cfform><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 47: <cfinput type="<span style="color: #8b0000">text</span>" <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 48: name="<span style="color: #8b0000">TraineeSearchBox</span>" <br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 49: autosuggest="<span style="color: #8b0000">cfc:MCRequestServer.getEmployeesByLastName({cfautosuggestvalue})</span>"<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 50: onbinderror="<span style="color: #8b0000">handleBindError</span>"<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 51: showautosuggestloadingicon="<span style="color: #8b0000">true</span>"<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 52: autoSuggestMinLength="<span style="color: #8b0000">1</span>"<br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 53: tooltip="<span style="color: #8b0000">Type employee last name</span>" <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 54: onclick="<span style="color: #8b0000">this.value=''</span>" <br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 55: /><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 56: <!--- Causes the specified JavaScript function to run when the page loads. ---><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 57: <cfset ajaxOnLoad("<span style="color: #8b0000">init</span>")><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 58: <br><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 59: <span id="<span style="color: #8b0000">SearchTraineeSpan</span>"></span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 60: </cfform><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 61: <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 62: </body><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 63: </html><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 64: </pre></pre><br /><p>Couple of points to note:</p><br /><p>1. The below line binds the textbox to a CFC called MCRequestServer and more specifically, a function called getEmployeesByLastName(). The argument cfautosuggestvalue is a CF keyword which represents the text the user entered in the textbox.</p><pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 1: autosuggest="<span style="color: #8b0000">cfc:MCRequestServer.getEmployeesByLastName({cfautosuggestvalue})</span>"</pre></pre><br /><style type="text/css">.csharpcode, .csharpcode pre<br />{<br /> font-size: small;<br /> color: black;<br /> font-family: consolas, "Courier New", courier, monospace;<br /> background-color: #ffffff;<br /> /*white-space: pre;*/<br />}<br />.csharpcode pre { margin: 0em; }<br />.csharpcode .rem { color: #008000; }<br />.csharpcode .kwrd { color: #0000ff; }<br />.csharpcode .str { color: #006080; }<br />.csharpcode .op { color: #0000c0; }<br />.csharpcode .preproc { color: #cc6633; }<br />.csharpcode .asp { background-color: #ffff00; }<br />.csharpcode .html { color: #800000; }<br />.csharpcode .attr { color: #ff0000; }<br />.csharpcode .alt <br />{<br /> background-color: #f4f4f4;<br /> width: 100%;<br /> margin: 0em;<br />}<br />.csharpcode .lnum { color: #606060; }<br /></style><br /><br /><p>2. The below line causes the specified JavaScript function init() to run when the page loads. This is to avoid timing issues.</p><pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 1: <cfset ajaxOnLoad("<span style="color: #8b0000">init</span>")></pre></pre><br /><p>And here is the second file MCRequestServer.cfc. It just returns an array fo employee names.</p><pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 1: <cfcomponent displayname="<span style="color: #8b0000">MCRequestServer</span>" hint="<span style="color: #8b0000">Handles all bind requests</span>" output="<span style="color: #8b0000">false</span>"><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 2: <br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 3: <cffunction name="<span style="color: #8b0000">getEmployeesByLastName</span>" displayname="<span style="color: #8b0000">getEmployeesByLastName</span>" access="<span style="color: #8b0000">remote</span>" output="<span style="color: #8b0000">false</span>" returntype="<span style="color: #8b0000">array</span>"><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 4: <cfargument name="<span style="color: #8b0000">suggestvalue</span>" required="<span style="color: #8b0000">true</span>" /><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 5: <!--- The function must return suggestions as an array. ---><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 6: <cfset var EmplNamesArray = ArrayNew(1) /><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 7: <cftry><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 8: <!--- Get all unique last names that match the typed characters. ---><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 9: <cfstoredproc datasource="<span style="color: #8b0000">DB</span>" procedure="<span style="color: #8b0000">getMatchingEmployees</span>" > <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 10: <cfprocparam type="<span style="color: #8b0000">in</span>" dbvarname="<span style="color: #8b0000">@LastName</span>" cfsqltype="<span style="color: #8b0000">CF_SQL_VARCHAR</span>" value="<span style="color: #8b0000">#arguments.suggestvalue#</span>" ><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 11: <!--- Out variable ---><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 12: <cfprocresult name="<span style="color: #8b0000">rsEmplnames</span>"><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 13: </cfstoredproc><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 14: <!--- Convert the query to an array. ---><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 15: <cfloop query="<span style="color: #8b0000">rsEmplnames</span>"><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 16: <cfset arrayAppend(EmplNamesArray, Lastname & "<span style="color: #8b0000">, </span>" & FirstName ) /><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 17: </cfloop><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 18: <cfreturn EmplNamesArray /><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 19: <cfcatch type="<span style="color: #8b0000">any</span>"><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 20: <cfset arrayAppend(EmplNamesArray, "<span style="color: #8b0000">Error occurred</span>") /><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 21: <cfreturn EmplNamesArray /><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 22: </cfcatch><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 23: </cftry><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 24: </cffunction><br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 25: <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 26: <br /></pre><pre style="background-color: #fff2ec; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 27: </cfcomponent><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 28: </pre></pre> Sajjan Sarkarhttp://www.blogger.com/profile/07258575012965819548noreply@blogger.com1tag:blogger.com,1999:blog-3328123480609854402.post-76702236971096254442010-07-09T12:31:00.001-07:002010-07-09T13:49:00.866-07:00Simple JavaScript Programming Techniques<p><font color="#ff8000"><strong></strong></font> <p><em><font color="#c0c0c0">So I have been working on JavaScript for a little while now and gathered up a few points I thought I would share .This is my first blog post ever, so please forgive (but do let me know) any defects. I have tried to not get too detailed as I am afraid it will overload readers. Needless to say, I will be more than happy to discuss and revise the content to further improve it.</font></em> <p><font color="#ff8000"><strong>1. Declare commonly used objects globally</strong></font><font color="#c0c0c0"> to improve performance and to reduce your file size. </font> <p><font color="#c0c0c0">eg.: Declare the document object and window objects globally and use the references in your code.<i>(I generally like to start my global variable names with a '$' sign,but that's just my naming convention. So at the top of my JS file I would say:</i> </font> <p><i><font color="#c0c0c0"> var $doc = document;var $win=window;)</font></i> <p><font color="#ff8000"><strong>2. Wrap functions in logical namespaces: </strong></font><font color="#c0c0c0">For example if you have a bunch of utility functions , you could wrap them in a "Utility" namespace (as shown below) and call the functions using Namespace.function() syntax.</font> <p><font color="#c0c0c0">[So in this example to call <font face="Courier New">replaceAll</font>(), you would say:<font face="Courier New"> Utilities.replaceAll("arg1","arg2","arg3")</font> ]</font> <pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 1: <span style="color: #0000ff">var</span> Utilities = {<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 2: stopEventPropagation : <span style="color: #0000ff">function</span>(e) <br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 3: { <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 4: <span style="color: #0000ff">if</span> (!e)<br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 5: e = <span style="color: #0000ff">window</span>.event; <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 6: <span style="color: #0000ff">if</span> (e.cancelBubble)<br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 7: e.cancelBubble = <span style="color: #0000ff">true</span>;<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 8: <span style="color: #0000ff">else</span><br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 9: e.stopPropagation();<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 10: <br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 11: },<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 12: formatText : <span style="color: #0000ff">function</span>(text,color,isBold)<br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 13: {<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 14: <span style="color: #008000">// implementation.. </span><br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 15: },<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 16: replaceAll :<span style="color: #0000ff">function</span> (Source,stringToFind,stringToReplace)<br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 17: {<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 18: <span style="color: #008000">// implementation..</span><br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 19: <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 20: }<br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 21: }<br /></pre></pre><font color="#c0c0c0">This has the following advantages: </font><br /><ul><br /><li><font color="#c0c0c0">Avoid variable/function name clashes with (potentially) similarly-named variables in other JS libraries/files. </font><br /><li><font color="#c0c0c0"> Makes the code much more readable for other people.(Imagine if your HTML file references 5 JS files and you encounter a method call like validate(), you would have to go look in all to find the where validate() is implemented.I personally try to place one namespace per file,so Utilities namespace would be in Utilities.js, so anyone would know exactly where to look.) </font><br /><li><font color="#c0c0c0"> Provides an elegant way to create grouped constants,which improves readability.</font></li></ul><pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 1: <span style="color: #0000ff">var</span> XHROReadystates {<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 2: UNINITIALIZED :0, <span style="color: #008000">/** The initial value. */</span><br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 3: OPEN :1, <span style="color: #008000">/** The open() method has been successfully called, */</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 4: SENT :2, <span style="color: #008000">/** The UA successfully completed the request, but no data hes yet been received. */</span><br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 5: RECEIVING :3, <span style="color: #008000">/** Immediately before receiving the message body (if any). All HTTP headers have been received. */</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 6: LOADED :4 <span style="color: #008000">/** The data transfer has been completed. */</span><br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 7: }</pre></pre><br /><p><b><font color="#ff8000">3. Using User-Defined Attributes:</font></b> <font color="#c0c0c0">HTML allows elements to have user-defined attributes. They can be accessed/modified in the same way as standard attributes.This is a powerful mechanism and can be used for both UI and functionality. </font><font color="#c0c0c0">A note on accessing attributes, IE allows access to the attributes using the (.) dot operator but this is not the W3C Standard and does not work on DOM compliant browsers like FireFox and Safari.Always use the W3C standards-compliant <font face="Courier New">get/setAttribute</font> methods to use attributes. </font></p><br /><p><font color="#c0c0c0">Here is an example of a user defined attribute and how it may be used. </font><br /><p><font color="#c0c0c0">eg:</font> <pre><pre style="background-color: #fbfbfb; margin: 0em; width: 43.24%; font-family: consolas,'Courier New',courier,monospace; height: 17px; font-size: 12px"> 1: <span style="color: #0000ff"><</span><span style="color: #800000">input</span> <span style="color: #ff0000">type</span>=<span style="color: #0000ff">"text"</span> <span style="color: #ff0000">required</span>=<span style="color: #0000ff">"true"</span> <span style="color: #ff0000">id</span>=<span style="color: #0000ff">"myTB"</span>.... </pre></pre><pre><font color="#c0c0c0"><font face="Verdana">Here required is not a standard HTML attribute <em>(for simplicity's sake lets not consider HTML5's required attribute)</em> <br />Here is a way to use it to perform simple validation:</font></font></pre><pre> </pre><pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 1: isRequiredFieldsFilled : <span style="color: #0000ff">function</span> ()<span style="color: #008000">/* Function to test if "required" input elements have been filled */</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 2: {<br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 3: <span style="color: #008000">/* initialize result flag */</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 4: <span style="color: #0000ff">var</span> isValid = <span style="color: #0000ff">true</span>;<br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 5: <span style="color: #008000">/* We are going to test the following element types */</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 6: <span style="color: #0000ff">var</span> types = <span style="color: #0000ff">new</span> <span style="color: #0000ff">Array</span>("<span style="color: #8b0000">SELECT</span>","<span style="color: #8b0000">INPUT</span>","<span style="color: #8b0000">TEXTAREA</span>");<br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 7: <span style="color: #008000">/* For each type */</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 8: <span style="color: #0000ff">for</span>(<span style="color: #0000ff">var</span> i=0;i<types.<span style="color: #0000ff">length</span>;i++)<br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 9: {<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 10: <span style="color: #008000">/* Get all elements of that type */</span><br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 11: <span style="color: #0000ff">var</span> elems = <span style="color: #0000ff">document</span>.getElementsByTagName(types[i]);<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 12: <span style="color: #008000">/* For each of these elements */</span><br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 13: <span style="color: #0000ff">for</span>(<span style="color: #0000ff">var</span> j=0;j<elems.<span style="color: #0000ff">length</span>;j++)<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 14: {<br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 15: <span style="color: #008000">/* Ignore hidden fields */</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 16: <span style="color: #0000ff">if</span>(elems[j].type=='hidden')<br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 17: <span style="color: #0000ff">continue</span>;<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 18: <span style="color: #008000">/* Reset color */</span><br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 19: Utilities.setInvalidColor(elems[j], <span style="color: #0000ff">false</span>);<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 20: <span style="color: #008000">/* Test if the required attribute exists,if so check if it is empty */</span><br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 21: <span style="color: #0000ff">if</span>(elems[j].getAttribute("<span style="color: #8b0000">required</span>")!=<span style="color: #0000ff">null</span> <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 22: && (elems[j].getAttribute("<span style="color: #8b0000">required</span>")==<span style="color: #0000ff">true</span> <br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 23: && elems[j].value=="<span style="color: #8b0000"></span>")<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 24: {<br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 25: <span style="color: #008000">/* Mark as unfilled */</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 26: Utilities.setInvalidColor(elems[j], <span style="color: #0000ff">true</span>);<br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 27: <span style="color: #008000">/* Set result flag */</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 28: isValid=<span style="color: #0000ff">false</span>;<br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 29: }<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 30: }<br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 31: }<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 32: <span style="color: #008000">/* After checking all elements,now check if anything has set the result flag */</span><br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 33: <span style="color: #0000ff">if</span> (!isValid) {<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 34: <span style="color: #008000">/* Alert the user */</span><br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 35: <span style="color: #0000ff">alert</span>('Please fill required fields!');<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 36: }<br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 37: <span style="color: #0000ff">return</span> isValid;<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 38: <br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 39: }</pre></pre><pre><font color="#c0c0c0"><font face="Verdana"></font></font><b><font color="#ff8000" face="Verdana">4. Do Feature-detects, NOT browser detects</font></b></pre><br /><p><font color="#c0c0c0">Doing this allows you to be be detached from the changes made by the browser vendor.Consider the example below, which tests if the browser is IE and if so executes <font face="Courier New">someIEMethod</font> else it executes a DOM compliant <font face="Courier New">someDOMMethod</font>. Consider the future possibility that IE may decide to be DOM compliant and provide implementation for the DOM method <font face="Courier New">someDOMMethod</font>() and in time,deprecate <font face="Courier New">someIEMethod</font> (). This code would break then. </font><br /><p><pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 1: <span style="color: #0000ff">if</span> (navigator.userAgent.indexO(”MSIE”) > —1)<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 2: someIEMethod();<span style="color: #008000">/* say, a non-DON coin1iant function */</span><br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 3: <span style="color: #0000ff">else</span> <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 4: someDOMMethod();<span style="color: #008000">/* say, a DOM compliant function */</span><br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 5: </pre></pre><br /><p><font color="#c0c0c0"></font><font color="#c0c0c0">Here is a more real-world example which prevents event propagation depending on the browser.Consider evt to be the event object. This is not a good way to do it:</font> <pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 1: <span style="color: #0000ff">if</span> (navigator.userAgent.indexOf('MSIE') > —1)<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 2: evt.cancelBubble = <span style="color: #0000ff">true</span>;<span style="color: #008000">/* lE specific */</span><br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 3: <span style="color: #0000ff">else</span> <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 4: evt.stopPropagation();<span style="color: #008000">/* DOM compliant */</span><br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 5: <br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 6: </pre></pre><br /><p><font color="#c0c0c0">A better way is:</font> <pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 1: <span style="color: #0000ff">if</span> (evt.cancelBubble)<br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 2: evt.cancelBubble = <span style="color: #0000ff">true</span>;<br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 3: <span style="color: #0000ff">else</span><br /></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 4: evt. stopPropagation();<br /></pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 5: </pre></pre><br /><style type="text/css">.csharpcode, .csharpcode pre<br />{<br /> font-size: small;<br /> color: black;<br /> font-family: consolas, "Courier New", courier, monospace;<br /> background-color: #ffffff;<br /> /*white-space: pre;*/<br />}<br />.csharpcode pre { margin: 0em; }<br />.csharpcode .rem { color: #008000; }<br />.csharpcode .kwrd { color: #0000ff; }<br />.csharpcode .str { color: #006080; }<br />.csharpcode .op { color: #0000c0; }<br />.csharpcode .preproc { color: #cc6633; }<br />.csharpcode .asp { background-color: #ffff00; }<br />.csharpcode .html { color: #800000; }<br />.csharpcode .attr { color: #ff0000; }<br />.csharpcode .alt <br />{<br /> background-color: #f4f4f4;<br /> width: 100%;<br /> margin: 0em;<br />}<br />.csharpcode .lnum { color: #606060; }<br /></style><br /><br /><p><font color="#c0c0c0">[A note: For some situations a browser detect may still be required, most typically to apply browser specific CSS like opacity etc.] </font><br /><p></p><br /><div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:470aa4b0-6b46-4167-8687-2dc153f9be49" class="wlWriterEditableSmartContent">Technorati Tags: <a href="http://technorati.com/tags/javascript+programming+techniques+best+practices+namespaces" rel="tag">javascript programming techniques best practices namespaces</a></div> Sajjan Sarkarhttp://www.blogger.com/profile/07258575012965819548noreply@blogger.com5