<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>session variable &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/session-variable/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Tue, 03 Oct 2017 13:05:45 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>
	<item>
		<title>PostgreSQL: Use PSQL command line variable to make your SQL Queries Dynamic</title>
		<link>https://www.dbrnd.com/2017/06/postgresql-use-psql-command-line-variable-to-make-your-sql-queries-dynamic/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 19 Jun 2017 16:16:53 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Monitoring]]></category>
		<category><![CDATA[PostgreSQL Performance Tuning]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[psql]]></category>
		<category><![CDATA[session variable]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5428</guid>

					<description><![CDATA[In this post, I am sharing tips like how to use a psql session variable to make your queries more dynamic in PostgreSQL. As a DBA, we prepared lots of .sql files in our server for different purposes like check DB stats, get tables size, get object information. We are executing this scripts (.sql files) [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F06%2Fpostgresql-use-psql-command-line-variable-to-make-your-sql-queries-dynamic%2F&amp;linkname=PostgreSQL%3A%20Use%20PSQL%20command%20line%20variable%20to%20make%20your%20SQL%20Queries%20Dynamic" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F06%2Fpostgresql-use-psql-command-line-variable-to-make-your-sql-queries-dynamic%2F&amp;linkname=PostgreSQL%3A%20Use%20PSQL%20command%20line%20variable%20to%20make%20your%20SQL%20Queries%20Dynamic" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F06%2Fpostgresql-use-psql-command-line-variable-to-make-your-sql-queries-dynamic%2F&amp;linkname=PostgreSQL%3A%20Use%20PSQL%20command%20line%20variable%20to%20make%20your%20SQL%20Queries%20Dynamic" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F06%2Fpostgresql-use-psql-command-line-variable-to-make-your-sql-queries-dynamic%2F&amp;linkname=PostgreSQL%3A%20Use%20PSQL%20command%20line%20variable%20to%20make%20your%20SQL%20Queries%20Dynamic" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F06%2Fpostgresql-use-psql-command-line-variable-to-make-your-sql-queries-dynamic%2F&amp;linkname=PostgreSQL%3A%20Use%20PSQL%20command%20line%20variable%20to%20make%20your%20SQL%20Queries%20Dynamic" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F06%2Fpostgresql-use-psql-command-line-variable-to-make-your-sql-queries-dynamic%2F&amp;linkname=PostgreSQL%3A%20Use%20PSQL%20command%20line%20variable%20to%20make%20your%20SQL%20Queries%20Dynamic" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F06%2Fpostgresql-use-psql-command-line-variable-to-make-your-sql-queries-dynamic%2F&#038;title=PostgreSQL%3A%20Use%20PSQL%20command%20line%20variable%20to%20make%20your%20SQL%20Queries%20Dynamic" data-a2a-url="https://www.dbrnd.com/2017/06/postgresql-use-psql-command-line-variable-to-make-your-sql-queries-dynamic/" data-a2a-title="PostgreSQL: Use PSQL command line variable to make your SQL Queries Dynamic"></a></p><p>In this post, I am sharing tips like how to use a psql session variable to make your queries more dynamic in PostgreSQL.</p>
<p>As a DBA, we prepared lots of .sql files in our server for different purposes like check DB stats, get tables size, get object information.</p>
<blockquote><p>We are executing this scripts (.sql files) many times, but every time we have to change the schema name parameters because we are checking for different schemas and we hard coded the schema values in the SQL scripts.</p>
<p>I found one solution to resolve this problem, that is PSQL Command line variable. You can declare the variable in the PSQL Session and can use for that session.</p></blockquote>
<p>The benefit of this is, you do not require to update .sql files for again and again, and multiple people can use same SQL script at a time for different schemas.</p>
<p><em>Note: As this is a session variable so whenever you connect to PSQL, you have to declare this variable.</em></p>
<p><strong>Check the below demonstration:</strong></p>
<p><strong>Step 1: Connect to PSQL</strong></p>
<pre class="crayon-plain-tag">root-&gt; psql -d database_name</pre> </p>
<p><strong>Step 2: Create a session variable</strong></p>
<pre class="crayon-plain-tag">psql-&gt; \set SchemaVariable '\'my_schema_1\''</pre> </p>
<p><strong>Step 3: Embed that variable in your SELECT script of .sql file</strong></p>
<pre class="crayon-plain-tag">select       
       tablename
       ,attname      
       ,avg_width
from pg_stats
where schemaname = :SchemaVariable;</pre> </p>
<p>If you are using a list of schemas, you can also assign multiple schema names to a session variable.</p>
<p><strong>Declare session variable for multiple values:</strong></p>
<pre class="crayon-plain-tag">\set SchemaVariable '\'my_schema_1\',\'my_schema_2\''</pre> </p>
<p><strong>Use session variable in IN clause:</strong></p>
<pre class="crayon-plain-tag">select       
       tablename
       ,attname      
       ,avg_width
from pg_stats
where schemaname in (:SchemaVariable);</pre> </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>MySQL: Procedure Variable vs Session specific User Defined Variable (@variable vs variable)</title>
		<link>https://www.dbrnd.com/2016/02/mysql-procedure-variable-vs-session-specific-user-defined-variable-variable-vs-variable/</link>
					<comments>https://www.dbrnd.com/2016/02/mysql-procedure-variable-vs-session-specific-user-defined-variable-variable-vs-variable/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Wed, 17 Feb 2016 19:14:14 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[local variable]]></category>
		<category><![CDATA[MySQL Command]]></category>
		<category><![CDATA[MySQL Database Administrator]]></category>
		<category><![CDATA[MySQL Database Designing]]></category>
		<category><![CDATA[MySQL Database Programming]]></category>
		<category><![CDATA[MySQL Error]]></category>
		<category><![CDATA[MySQL Performance Tunning]]></category>
		<category><![CDATA[MySQL Query]]></category>
		<category><![CDATA[MySQL Tips and Tricks]]></category>
		<category><![CDATA[session variable]]></category>
		<category><![CDATA[user defined variable]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=1669</guid>

					<description><![CDATA[I found lots of questions and discussions on the difference between stored procedure local variable and session-specific user-defined variable of MySQL. MySQL provides session specific user defined variable to initialize and use during the session. The scope of this variable is for the specific session only and outside of that session another client connection cannot [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fmysql-procedure-variable-vs-session-specific-user-defined-variable-variable-vs-variable%2F&amp;linkname=MySQL%3A%20Procedure%20Variable%20vs%20Session%20specific%20User%20Defined%20Variable%20%28%40variable%20vs%20variable%29" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fmysql-procedure-variable-vs-session-specific-user-defined-variable-variable-vs-variable%2F&amp;linkname=MySQL%3A%20Procedure%20Variable%20vs%20Session%20specific%20User%20Defined%20Variable%20%28%40variable%20vs%20variable%29" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fmysql-procedure-variable-vs-session-specific-user-defined-variable-variable-vs-variable%2F&amp;linkname=MySQL%3A%20Procedure%20Variable%20vs%20Session%20specific%20User%20Defined%20Variable%20%28%40variable%20vs%20variable%29" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fmysql-procedure-variable-vs-session-specific-user-defined-variable-variable-vs-variable%2F&amp;linkname=MySQL%3A%20Procedure%20Variable%20vs%20Session%20specific%20User%20Defined%20Variable%20%28%40variable%20vs%20variable%29" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fmysql-procedure-variable-vs-session-specific-user-defined-variable-variable-vs-variable%2F&amp;linkname=MySQL%3A%20Procedure%20Variable%20vs%20Session%20specific%20User%20Defined%20Variable%20%28%40variable%20vs%20variable%29" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fmysql-procedure-variable-vs-session-specific-user-defined-variable-variable-vs-variable%2F&amp;linkname=MySQL%3A%20Procedure%20Variable%20vs%20Session%20specific%20User%20Defined%20Variable%20%28%40variable%20vs%20variable%29" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fmysql-procedure-variable-vs-session-specific-user-defined-variable-variable-vs-variable%2F&#038;title=MySQL%3A%20Procedure%20Variable%20vs%20Session%20specific%20User%20Defined%20Variable%20%28%40variable%20vs%20variable%29" data-a2a-url="https://www.dbrnd.com/2016/02/mysql-procedure-variable-vs-session-specific-user-defined-variable-variable-vs-variable/" data-a2a-title="MySQL: Procedure Variable vs Session specific User Defined Variable (@variable vs variable)"></a></p><p>I found lots of questions and discussions on the difference between stored procedure local variable and session-specific user-defined variable of MySQL. </p>
<p>MySQL provides session specific user defined variable to initialize and use during the session. </p>
<blockquote><p>The scope of this variable is for the specific session only and outside of that session another client connection cannot access this variable and once session terminates, it destroys automatically. </p></blockquote>
<p>This is loosely typed variables and we can initialize somewhere in a session.<br />
We can write @var_name to define a session specific variable. </p>
<blockquote><p>We also have one local variable, which we can define and use for specific stored objects like a stored procedure.<br />
The scope of this local variable is for that object execution purpose. Once a stored procedure executes, it destroys automatically. </p></blockquote>
<p><strong>Below is a small demonstration on use of @variable vs variable:</strong><br />
<strong>Create a sample stored procedure with session and local variable:</strong> </p>
<pre class="crayon-plain-tag">DELIMITER //
CREATE PROCEDURE usp_TestVariables()
BEGIN
    -- Declare Local Variable.....
    DECLARE MyVariable INT DEFAULT 1;
    SET MyVariable := MyVariable + 1;
    
    -- Define Session Specific Variable.....
    SET @MyVariable := @MyVariable + 1;
    
    SELECT  MyVariable, @MyVariable;
END;

-- Assign value to user defined variable.
SET @MyVariable = 1;</pre> </p>
<pre class="crayon-plain-tag">-- Execute this stored procedure multiple times to check a value of both the variables.
CALL usp_TestVariables();

--The result of first call.
MyVariable    @MyVariable
-----------  ----------------
    2	           2

--The result of second call.
MyVariable    @MyVariable
-----------  ----------------
    2	           3

--The result of third call.
MyVariable    @MyVariable
-----------  ----------------
    2	           4

--The result of fourth call.
MyVariable    @MyVariable
-----------  ----------------
    2	           5</pre> </p>
<p>You can check the above result, where @MyVariable is incrementing because it persists values until session end.<br />
A stored procedure local variable is not incrementing because it destroyed after every execution of the stored procedure. </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2016/02/mysql-procedure-variable-vs-session-specific-user-defined-variable-variable-vs-variable/feed/</wfw:commentRss>
			<slash:comments>7</slash:comments>
		
		
			</item>
	</channel>
</rss>
