<?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>Procedure Running Dynamic Query in SQL Server - MSSQL Query</title>
	<atom:link href="https://mssqlquery.com/tag/procedure-running-dynamic-query-in-sql-server/feed" rel="self" type="application/rss+xml" />
	<link>https://mssqlquery.com</link>
	<description>MSSQL and TSQL Programming and TSQL Examples</description>
	<lastBuildDate>Wed, 29 Sep 2021 20:04:05 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.3.1</generator>

<image>
	<url>https://mssqlquery.com/wp-content/uploads/2023/06/cropped-mssql-query-icon-32x32.png</url>
	<title>Procedure Running Dynamic Query in SQL Server - MSSQL Query</title>
	<link>https://mssqlquery.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Procedure Running Dynamic Query in SQL Server</title>
		<link>https://mssqlquery.com/procedure-running-dynamic-query-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Wed, 29 Sep 2021 20:04:05 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Stored Procedures]]></category>
		<category><![CDATA[Dynamic Query in SQL Server]]></category>
		<category><![CDATA[Procedure Running Dynamic Query in SQL Server]]></category>
		<category><![CDATA[SQL Dynamic Query]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=79</guid>

					<description><![CDATA[<p>Hello to everyone, In this article, I will give you information about the procedure that runs dynamic queries in SQL Server. I don&#8217;t know if there is a need for such a thing in SQL Server, but I tried to write it saying that maybe I will need it in&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/procedure-running-dynamic-query-in-sql-server">Procedure Running Dynamic Query in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello to everyone,</p>
<p>In this article, I will give you information about the procedure that runs dynamic queries in SQL Server.</p>
<p>I don&#8217;t know if there is a need for such a thing in SQL Server, but I tried to write it saying that maybe I will need it in the future and I wrote it. I wrote it as a procedure.</p>
<p>It takes three parameters. Database name, Table Name and Schema name.</p>
<p>You can make it even more dynamic if you wish. This may vary completely depending on the job you are doing.</p>
<p>Create the following procedure and run the codes. I had Northwind and AdventureWorks databases as an example and I tried it on these databases. You can try it on any database you want.</p>
<pre class="line-numbers"><code class="language-sql">CREATE PROCEDURE GetDynamicQueryProcedure
    @DatabaseName VARCHAR(50),
    @TableName VARCHAR(50),
    @SchemaName VARCHAR(50)
AS
BEGIN
    DECLARE @SQLSentence VARCHAR(500);

    SET @SQLSentence = '
	SELECT  * 
        FROM ' + @DatabaseName + '.' + @SchemaName + '.' + @TableName + '';

    EXEC (@SQLSentence);

END;


--Use of the Procedure

EXEC dbo.GetDynamicQueryProcedure @DatabaseName = 'Northwind',
                               @TableName = 'Products',
                               @SchemaName = 'dbo';


EXEC dbo.GetDynamicQueryProcedure @DatabaseName = 'AdventureWorks',
                               @TableName = 'Department',
                               @SchemaName = 'HumanResources';</code></pre>
<p>Once you create and run the procedure, you will get the following results.</p>
<p><img decoding="async" fetchpriority="high" class="alignnone wp-image-81 size-full" src="https://mssqlquery.com/wp-content/uploads/2021/09/procedure-running-dynamic-query-in-sql-server-1.jpg" alt="Procedure Running Dynamic Query in SQL Server" width="700" height="539" srcset="https://mssqlquery.com/wp-content/uploads/2021/09/procedure-running-dynamic-query-in-sql-server-1.jpg 700w, https://mssqlquery.com/wp-content/uploads/2021/09/procedure-running-dynamic-query-in-sql-server-1-300x231.jpg 300w" sizes="(max-width: 700px) 100vw, 700px" /></p>
<p>As you can see, there are two different databases and I brought results from two different databases on the same screen.</p>
<p>Good luck to everyone in business and life.</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 461</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/procedure-running-dynamic-query-in-sql-server">Procedure Running Dynamic Query in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
