<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>T-SQL on Henrique Almeida</title><link>https://alencarinosql.com.br/en/tags/t-sql/</link><description>Recent content in T-SQL on Henrique Almeida</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Sun, 27 Jul 2025 12:00:00 -0300</lastBuildDate><atom:link href="https://alencarinosql.com.br/en/tags/t-sql/index.xml" rel="self" type="application/rss+xml"/><item><title>Linked Server</title><link>https://alencarinosql.com.br/en/p/linked-server/</link><pubDate>Sun, 27 Jul 2025 12:00:00 -0300</pubDate><guid>https://alencarinosql.com.br/en/p/linked-server/</guid><description>&lt;p>Hi everyone! As an old friend of SQL Server, I know we often need to integrate data that lives on different servers. That&amp;rsquo;s where the &lt;strong>Linked Server&lt;/strong> comes in, acting as a solid bridge between separate data environments. In this post, we&amp;rsquo;ll go deep into Linked Servers, from basic setup to advanced tips and troubleshooting. Get ready to see everything this tool can do!&lt;/p>
&lt;hr>
&lt;h2 id="what-is-a-linked-server-and-why-do-you-need-one">What Is a Linked Server and Why Do You Need One?
&lt;/h2>&lt;p>Imagine you have sales data on one server and customer information on another. Or maybe your main system runs on an older SQL Server, but you need to pull data from a financial system running on a newer instance, or even on Oracle, DB2 or MySQL. A Linked Server is exactly what you need!&lt;/p>
&lt;p>In simple terms, a &lt;strong>Linked Server is a SQL Server setting that lets one SQL Server instance run commands against another OLE DB data source&lt;/strong>. That data source can be another SQL Server instance (local or remote), Oracle, MySQL, DB2, and even an Excel or CSV file (yes, you read that right!). It works as a &amp;ldquo;shortcut&amp;rdquo; to external data, making it possible to run queries, updates and even create objects on remote servers, all from your local instance.&lt;/p>
&lt;p>&lt;strong>But why use a Linked Server?&lt;/strong> There are plenty of reasons:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Data Integration:&lt;/strong> Bring together data from different sources for reports, analysis or migrations.&lt;/li>
&lt;li>&lt;strong>Distributed Queries:&lt;/strong> Run queries that span multiple servers without having to move the data.&lt;/li>
&lt;li>&lt;strong>ETL Processes (Extract, Transform, Load):&lt;/strong> Make it easier to extract and load data between systems.&lt;/li>
&lt;li>&lt;strong>Centralized Management:&lt;/strong> Access data from different environments through a single interface.&lt;/li>
&lt;li>&lt;strong>Less Network Traffic:&lt;/strong> In some scenarios, it can be more efficient to query the data on the source server than to transfer large volumes.&lt;/li>
&lt;/ul>
&lt;hr>
&lt;h2 id="hands-on-configuring-a-linked-server">Hands-On: Configuring a Linked Server
&lt;/h2>&lt;p>Setting up a Linked Server can look intimidating at first, but I promise it&amp;rsquo;s simpler than it seems. Let&amp;rsquo;s go step by step, both in SQL Server Management Studio (SSMS) and with T-SQL.&lt;/p>
&lt;h3 id="using-sql-server-management-studio-ssms">Using SQL Server Management Studio (SSMS)
&lt;/h3>&lt;p>This is the most visual and intuitive way if you&amp;rsquo;re just getting started:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Connect with SSMS:&lt;/strong> Open SQL Server Management Studio and connect to the instance where you want to create the Linked Server.&lt;/li>
&lt;li>&lt;strong>Go to &amp;ldquo;Server Objects&amp;rdquo;:&lt;/strong> In Object Explorer, expand &amp;ldquo;Server Objects&amp;rdquo;.&lt;/li>
&lt;li>&lt;strong>Expand &amp;ldquo;Linked Servers&amp;rdquo;:&lt;/strong> Inside &amp;ldquo;Server Objects&amp;rdquo;, expand &amp;ldquo;Linked Servers&amp;rdquo;.&lt;/li>
&lt;li>&lt;strong>New Linked Server:&lt;/strong> Right-click &amp;ldquo;Linked Servers&amp;rdquo; and select &amp;ldquo;New Linked Server…&amp;rdquo;.&lt;/li>
&lt;li>&lt;strong>Fill in the Information:&lt;/strong> A new &amp;ldquo;New Linked Server&amp;rdquo; window opens. This is where the magic happens.
&lt;ul>
&lt;li>&lt;strong>General Page:&lt;/strong>
&lt;ul>
&lt;li>&lt;strong>Linked server:&lt;/strong> Give your Linked Server a meaningful name. This is the name you&amp;rsquo;ll use in your queries (e.g. &lt;code>MEU_SERVER_REMOTO&lt;/code>).&lt;/li>
&lt;li>&lt;strong>Server type:&lt;/strong>
&lt;ul>
&lt;li>&lt;strong>SQL Server:&lt;/strong> If you&amp;rsquo;re connecting to another SQL Server instance, select this option.&lt;/li>
&lt;li>&lt;strong>Other data source:&lt;/strong> For other data sources (Oracle, MySQL, Excel, etc.), select this one and choose the appropriate OLE DB provider. For SQL Server, this option is more generic and lets you specify the provider and connection string manually.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>Product name:&lt;/strong> For SQL Server, we usually fill in &amp;ldquo;SQL Server&amp;rdquo;. For others, the product name (e.g. &amp;ldquo;Oracle&amp;rdquo;).&lt;/li>
&lt;li>&lt;strong>Data source:&lt;/strong> The name or IP address of the remote server. If it&amp;rsquo;s a named instance (e.g. &lt;code>SEUSQL\INSTANCIA&lt;/code>), use &lt;code>SEUSQL\INSTANCIA&lt;/code>.&lt;/li>
&lt;li>&lt;strong>Provider string:&lt;/strong> Leave it blank for standard SQL Server. Other providers may need extra connection parameters.&lt;/li>
&lt;li>&lt;strong>Catalog:&lt;/strong> The default database name on the remote server for this Linked Server. Optional, but useful.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>Security Page:&lt;/strong> This one is crucial for authentication.
&lt;ul>
&lt;li>&lt;strong>Not be made:&lt;/strong> No security context. Not recommended for most scenarios.&lt;/li>
&lt;li>&lt;strong>Be made using the login&amp;rsquo;s current security context:&lt;/strong> Authentication uses the credentials of the user running the query on the local server. Useful in domain environments with integrated Windows authentication.&lt;/li>
&lt;li>&lt;strong>Be made using a security context that the login&amp;rsquo;s current security context does not support:&lt;/strong> For users that aren&amp;rsquo;t mapped.&lt;/li>
&lt;li>&lt;strong>Be made using this security context:&lt;/strong> This is the most common and flexible option. You specify a &lt;strong>remote login&lt;/strong> (user and password) that will be used to access the linked server.
&lt;ul>
&lt;li>&lt;strong>Remote login:&lt;/strong> The user name on the remote server.&lt;/li>
&lt;li>&lt;strong>With password:&lt;/strong> The password for the remote user.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>Add:&lt;/strong> Map specific local server logins to specific remote server logins. Very useful for fine-grained security.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>Server Options Page:&lt;/strong>
&lt;ul>
&lt;li>&lt;strong>Collation Compatible:&lt;/strong> If the servers have different collation settings and you need the Linked Server to handle that.&lt;/li>
&lt;li>&lt;strong>Data Access:&lt;/strong> Enables or disables data access.&lt;/li>
&lt;li>&lt;strong>RPC, RPC Out:&lt;/strong> Allow remote procedure calls. Essential for running remote stored procedures.&lt;/li>
&lt;li>&lt;strong>Use Remote Collation:&lt;/strong> Uses the remote server&amp;rsquo;s collation.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>Click OK:&lt;/strong> Your Linked Server is ready!&lt;/li>
&lt;/ol>
&lt;h3 id="using-t-sql-transact-sql">Using T-SQL (Transact-SQL)
&lt;/h3>&lt;p>If you prefer automation and full control, T-SQL is the way to go. The basic syntax is:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt"> 1
&lt;/span>&lt;span class="lnt"> 2
&lt;/span>&lt;span class="lnt"> 3
&lt;/span>&lt;span class="lnt"> 4
&lt;/span>&lt;span class="lnt"> 5
&lt;/span>&lt;span class="lnt"> 6
&lt;/span>&lt;span class="lnt"> 7
&lt;/span>&lt;span class="lnt"> 8
&lt;/span>&lt;span class="lnt"> 9
&lt;/span>&lt;span class="lnt">10
&lt;/span>&lt;span class="lnt">11
&lt;/span>&lt;span class="lnt">12
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-sql" data-lang="sql">&lt;span class="line">&lt;span class="cl">&lt;span class="k">EXEC&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">sp_addlinkedserver&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="n">server&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;MEU_SERVER_REMOTO&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="c1">-- Linked Server name
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="n">srvproduct&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;SQL Server&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="c1">-- Remote server product (e.g. &amp;#39;SQL Server&amp;#39;, &amp;#39;Oracle&amp;#39;)
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="n">provider&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;SQLNCLI11&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="c1">-- OLE DB provider (SQLNCLI11 for SQL Server 2012 and later, SQLOLEDB for older versions)
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="n">datasrc&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;NOME_OU_IP_DO_SERVER_REMOTO\INSTANCIA_REMOTO&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="c1">-- Data source (server name or IP, with instance if applicable)
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="k">EXEC&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">sp_addlinkedsrvlogin&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="n">rmtsrvname&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;MEU_SERVER_REMOTO&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="c1">-- Linked Server name
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="n">useself&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;FALSE&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="c1">-- &amp;#39;TRUE&amp;#39; to use the current security context, &amp;#39;FALSE&amp;#39; to specify a login
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="n">locallogin&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">NULL&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="c1">-- Local login (NULL for all local logins)
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="n">rmtuser&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;usuario_remoto&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="c1">-- Remote login
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="n">rmtpassword&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;sua_senha_secreta&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="c1">-- Remote login password
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>&lt;strong>Important notes:&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>&lt;code>@provider&lt;/code>: For SQL Server, &lt;code>SQLNCLI11&lt;/code> is the recommended provider for SQL Server 2012 and later. For older versions (up to SQL Server 2008 R2), &lt;code>SQLOLEDB&lt;/code> was the default. Make sure the provider is installed and registered.&lt;/li>
&lt;li>&lt;code>@locallogin&lt;/code>: If you want only one specific login on your local server to be able to use this Linked Server, replace &lt;code>NULL&lt;/code> with the local login name. Otherwise, &lt;code>NULL&lt;/code> lets any local login use the mapping.&lt;/li>
&lt;li>&lt;code>sp_addlinkedsrvlogin&lt;/code>: You can call &lt;code>sp_addlinkedsrvlogin&lt;/code> multiple times to map different local logins to different remote logins, or to map local logins to the current security context (&lt;code>@useself = 'TRUE'&lt;/code>).&lt;/li>
&lt;/ul>
&lt;hr>
&lt;h2 id="testing-and-using-your-linked-server">Testing and Using Your Linked Server
&lt;/h2>&lt;p>Once it&amp;rsquo;s configured, it&amp;rsquo;s time to test!&lt;/p>
&lt;h3 id="querying-data">Querying Data
&lt;/h3>&lt;p>The most common way to use a Linked Server is through &lt;strong>four-part name queries&lt;/strong>:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;span class="lnt">4
&lt;/span>&lt;span class="lnt">5
&lt;/span>&lt;span class="lnt">6
&lt;/span>&lt;span class="lnt">7
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-sql" data-lang="sql">&lt;span class="line">&lt;span class="cl">&lt;span class="k">SELECT&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">*&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="k">FROM&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">MEU_SERVER_REMOTO&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">NomeDoBancoDeDados&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">NomeDoSchema&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">NomeDaTabela&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="c1">-- Real example:
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="k">SELECT&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">c&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">NomeCliente&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">p&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">NomeProduto&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="k">FROM&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">MEU_SERVER_REMOTO&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">VendasDB&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">dbo&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">Clientes&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">c&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="k">INNER&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">JOIN&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">SEGUNDO_SERVER&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">EstoqueDB&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">dbo&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">Produtos&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">p&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">ON&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">c&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">ProdutoID&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">p&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">ProdutoID&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>You can also use the &lt;code>OPENQUERY&lt;/code> function for more complex queries or to improve performance, because it sends the query straight to the remote server to be executed there, not on the local server.&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-sql" data-lang="sql">&lt;span class="line">&lt;span class="cl">&lt;span class="k">SELECT&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">*&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="k">FROM&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">OPENQUERY&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">MEU_SERVER_REMOTO&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;SELECT ClienteID, NomeCliente FROM VendasDB.dbo.Clientes WHERE DataRegistro &amp;gt; GETDATE() - 30&amp;#39;&lt;/span>&lt;span class="p">);&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>The &lt;code>OPENDATASOURCE&lt;/code> function is similar, but it lets you specify the data source ad hoc, without a preconfigured Linked Server. Use it with care, because it&amp;rsquo;s less secure and more verbose.&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-sql" data-lang="sql">&lt;span class="line">&lt;span class="cl">&lt;span class="k">SELECT&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">*&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="k">FROM&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">OPENDATASOURCE&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;SQLNCLI11&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;Data Source=NOME_OU_IP_DO_SERVER_REMOTO\INSTANCIA_REMOTO;Initial Catalog=VendasDB;User ID=usuario_remoto;Password=sua_senha_secreta&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">VendasDB&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">dbo&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">Clientes&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;h3 id="running-remote-stored-procedures">Running Remote Stored Procedures
&lt;/h3>&lt;p>Yes, you can! This is really useful for orchestrating processes across different servers.&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-sql" data-lang="sql">&lt;span class="line">&lt;span class="cl">&lt;span class="k">EXEC&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">MEU_SERVER_REMOTO&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">NomeDoBancoDeDados&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">dbo&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">MinhaStoredProcedureRemota&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="n">Parametro1&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;Valor&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;hr>
&lt;h2 id="managing-and-troubleshooting-common-problems">Managing and Troubleshooting Common Problems
&lt;/h2>&lt;p>Even SQL Server experts run into challenges with Linked Servers. Here are some important points and troubleshooting tips:&lt;/p>
&lt;h3 id="security">Security
&lt;/h3>&lt;p>Security comes first.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Principle of Least Privilege:&lt;/strong> Grant only the permissions the remote login needs. Don&amp;rsquo;t use &lt;code>sa&lt;/code> or accounts with excessive privileges.&lt;/li>
&lt;li>&lt;strong>Windows vs. SQL Server Authentication:&lt;/strong> Windows authentication is more secure, especially in domain environments. If you use SQL Server authentication, make sure passwords are strong and rotated regularly.&lt;/li>
&lt;li>&lt;strong>Login Mapping:&lt;/strong> Use &lt;code>sp_addlinkedsrvlogin&lt;/code> to map local logins to specific remote logins, for finer-grained security.&lt;/li>
&lt;/ul>
&lt;h3 id="performance">Performance
&lt;/h3>&lt;p>Linked Server queries can be slower because of network latency and the query optimizer.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>OPENQUERY:&lt;/strong> Use &lt;code>OPENQUERY&lt;/code> whenever possible. It &amp;ldquo;pushes&amp;rdquo; the query to the remote server, minimizing network traffic and taking advantage of the source server&amp;rsquo;s query optimizer.&lt;/li>
&lt;li>&lt;strong>Indexes:&lt;/strong> Make sure the tables you&amp;rsquo;re querying on the remote server have appropriate indexes.&lt;/li>
&lt;li>&lt;strong>Simple Queries:&lt;/strong> Avoid complex queries or massive JOINs across servers if you can. Pull only the data you need.&lt;/li>
&lt;li>&lt;strong>Data Transfer:&lt;/strong> For large data volumes, consider replication, SSIS or other ETL tools instead of direct Linked Server queries.&lt;/li>
&lt;/ul>
&lt;h3 id="common-troubleshooting">Common Troubleshooting
&lt;/h3>&lt;ul>
&lt;li>&lt;strong>&amp;ldquo;Login failed for user…&amp;rdquo;&lt;/strong>: The most common error. Check the user name and password configured in &lt;code>sp_addlinkedsrvlogin&lt;/code> (or on the Security tab in SSMS). Make sure the remote user has permission to access the required database and tables.&lt;/li>
&lt;li>&lt;strong>&amp;ldquo;OLE DB provider &amp;lsquo;SQLNCLI11&amp;rsquo; for linked server &amp;lsquo;…&amp;rsquo; returned message &amp;lsquo;No process is on the other end of the pipe.&amp;rsquo;&amp;rdquo;&lt;/strong>: Usually points to a network connectivity problem (a firewall blocking port 1433), the SQL Server service on the remote server being stopped, or an incorrect server/instance name.&lt;/li>
&lt;li>&lt;strong>&amp;ldquo;The OLE DB provider &amp;lsquo;SQLNCLI11&amp;rsquo; for linked server &amp;lsquo;…&amp;rsquo; reported an error. Access denied.&amp;rdquo;&lt;/strong>: The remote user has access to the server, but not permission for the specific action (SELECT, INSERT, UPDATE, DELETE) on the table/view you&amp;rsquo;re trying to access.&lt;/li>
&lt;li>&lt;strong>&amp;ldquo;Cannot initialize the data source object of OLE DB provider &amp;lsquo;…&amp;rsquo; for linked server &amp;lsquo;…&amp;rsquo;&amp;rdquo;:&lt;/strong> A problem with the OLE DB provider. It may not be installed or registered correctly, or the connection string is wrong.&lt;/li>
&lt;li>&lt;strong>Timeout Errors:&lt;/strong> Queries that take too long to respond can be cut off by timeouts. Adjust the Linked Server timeout settings in the server options (Server Options -&amp;gt; Connection Timeout, Query Timeout) or with &lt;code>sp_serveroption&lt;/code>.&lt;/li>
&lt;/ul>
&lt;h3 id="dropping-a-linked-server">Dropping a Linked Server
&lt;/h3>&lt;p>If you need to remove a Linked Server:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-sql" data-lang="sql">&lt;span class="line">&lt;span class="cl">&lt;span class="k">EXEC&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">sp_dropserver&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;MEU_SERVER_REMOTO&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;droplogins&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>The &lt;code>'droplogins'&lt;/code> argument makes sure the associated login mappings are removed too.&lt;/p>
&lt;hr>
&lt;h2 id="final-thoughts-and-next-steps">Final Thoughts and Next Steps
&lt;/h2>&lt;p>The Linked Server is a powerful and versatile tool in any DBA&amp;rsquo;s or SQL Server developer&amp;rsquo;s toolkit. With it, you can build solid data integration solutions, consolidate information and streamline business processes.&lt;/p>
&lt;p>That said, like any powerful tool, a Linked Server calls for understanding and good judgment. Don&amp;rsquo;t use it for everything! Always weigh your project&amp;rsquo;s needs:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>For quick ad hoc queries across servers:&lt;/strong> A Linked Server is perfect.&lt;/li>
&lt;li>&lt;strong>For large-scale data integration or complex ETL processes:&lt;/strong> Consider tools like SQL Server Integration Services (SSIS), Azure Data Factory, or other data orchestration solutions.&lt;/li>
&lt;li>&lt;strong>For keeping synchronized copies of data:&lt;/strong> SQL Server Replication or Always On Availability Groups may be a better fit.&lt;/li>
&lt;/ul>
&lt;p>I hope this detailed guide has demystified Linked Servers for you. It&amp;rsquo;s an excellent way to connect data worlds and expand what your SQL Server environment can do.&lt;/p>
&lt;p>If you have any questions or suggestions, or want to share your experience with Linked Servers, leave a comment below! I&amp;rsquo;d love to hear how you&amp;rsquo;re using this tool in your projects.&lt;/p>
&lt;p>See you next time, and happy coding!&lt;/p></description></item><item><title>SQL Server Logins</title><link>https://alencarinosql.com.br/en/p/logins-sql-server/</link><pubDate>Sat, 26 Jul 2025 12:00:00 -0300</pubDate><guid>https://alencarinosql.com.br/en/p/logins-sql-server/</guid><description>&lt;img src="https://alencarinosql.com.br/p/logins-sql-server/1.jpg" alt="Featured image of post SQL Server Logins" />&lt;h1 id="configuring-logins-in-sql-server">Configuring Logins in SQL Server
&lt;/h1>&lt;h2 id="introduction">Introduction
&lt;/h2>&lt;p>Configuring logins in SQL Server is one of the essential tasks for making sure access to the database is properly controlled. There are two main kinds of logins in SQL Server: SQL Server logins and Windows logins. In this post, we&amp;rsquo;ll look at how to set up both kinds of logins and go over recommended practices for managing user access.&lt;/p>
&lt;h2 id="1-creating-sql-server-logins">1. Creating SQL Server Logins
&lt;/h2>&lt;p>In SQL Server, you can create logins through Management Studio or with T-SQL. Let&amp;rsquo;s start with a basic example of how to create a SQL Server login:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-sql" data-lang="sql">&lt;span class="line">&lt;span class="cl">&lt;span class="k">CREATE&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">LOGIN&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">NovoUsuario&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">WITH&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">PASSWORD&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;SenhaSegura123&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>This command creates a login named &lt;code>NovoUsuario&lt;/code> with an associated password. After creating it, you need to grant permissions to this login. That&amp;rsquo;s done by associating it with a user account inside a specific database.&lt;/p>
&lt;h3 id="example-of-creating-a-login-with-password-expiration-settings">Example of creating a login with password expiration settings:
&lt;/h3>&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;span class="lnt">4
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-sql" data-lang="sql">&lt;span class="line">&lt;span class="cl">&lt;span class="k">CREATE&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">LOGIN&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">NovoUsuario&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="k">WITH&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">PASSWORD&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;SenhaSegura123&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="n">CHECK_POLICY&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">ON&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="n">CHECK_EXPIRATION&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">ON&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>In this example, the password policy and password expiration are enabled, forcing the user to change the password after the set period.&lt;/p>
&lt;h2 id="2-creating-windows-logins">2. Creating Windows Logins
&lt;/h2>&lt;p>You can also create Windows-based logins, letting users access the database with their operating system credentials:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-sql" data-lang="sql">&lt;span class="line">&lt;span class="cl">&lt;span class="k">CREATE&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">LOGIN&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="n">DOMINIO&lt;/span>&lt;span class="err">\&lt;/span>&lt;span class="n">Usuario&lt;/span>&lt;span class="p">]&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">FROM&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">WINDOWS&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>In this case, the login is created using the Windows account &lt;code>DOMINIO\Usuario&lt;/code>.&lt;/p>
&lt;h3 id="windows-logins-vs-sql-server-logins">Windows Logins vs SQL Server Logins
&lt;/h3>&lt;ul>
&lt;li>&lt;strong>SQL Server logins&lt;/strong>: Useful in scenarios where users aren&amp;rsquo;t part of a domain, or when you need authentication that is exclusive to SQL Server.&lt;/li>
&lt;li>&lt;strong>Windows logins&lt;/strong>: Recommended when domain security and user account administration are already handled at the operating system level.&lt;/li>
&lt;/ul>
&lt;h2 id="3-mapping-logins-to-databases">3. Mapping Logins to Databases
&lt;/h2>&lt;p>After creating the login, the next step is to map it to a database. That&amp;rsquo;s done by creating a database user for the login:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-sql" data-lang="sql">&lt;span class="line">&lt;span class="cl">&lt;span class="n">USE&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">MeuBancoDeDados&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="k">CREATE&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">USER&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">NovoUsuario&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">FOR&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">LOGIN&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">NovoUsuario&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>The command above creates a user in the &lt;code>MeuBancoDeDados&lt;/code> database mapped to the &lt;code>NovoUsuario&lt;/code> login.&lt;/p>
&lt;h3 id="example-of-mapping-a-windows-login">Example of mapping a Windows login:
&lt;/h3>&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-sql" data-lang="sql">&lt;span class="line">&lt;span class="cl">&lt;span class="n">USE&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">MeuBancoDeDados&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="k">CREATE&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">USER&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="n">DOMINIO&lt;/span>&lt;span class="err">\&lt;/span>&lt;span class="n">Usuario&lt;/span>&lt;span class="p">]&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">FOR&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">LOGIN&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="n">DOMINIO&lt;/span>&lt;span class="err">\&lt;/span>&lt;span class="n">Usuario&lt;/span>&lt;span class="p">];&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;h2 id="4-granting-permissions">4. Granting Permissions
&lt;/h2>&lt;p>After mapping the login to the database, you need to grant the required permissions. For example, to grant read and write permissions:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-sql" data-lang="sql">&lt;span class="line">&lt;span class="cl">&lt;span class="k">GRANT&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">SELECT&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">INSERT&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">UPDATE&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">DELETE&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">ON&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">SCHEMA&lt;/span>&lt;span class="p">::&lt;/span>&lt;span class="n">dbo&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">TO&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">NovoUsuario&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>This gives the &lt;code>NovoUsuario&lt;/code> user read and write permissions on every table in the &lt;code>dbo&lt;/code> schema.&lt;/p>
&lt;h3 id="example-of-granting-permissions-to-windows-logins">Example of granting permissions to Windows logins:
&lt;/h3>&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-sql" data-lang="sql">&lt;span class="line">&lt;span class="cl">&lt;span class="k">GRANT&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">EXECUTE&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">ON&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">SCHEMA&lt;/span>&lt;span class="p">::&lt;/span>&lt;span class="n">dbo&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">TO&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="n">DOMINIO&lt;/span>&lt;span class="err">\&lt;/span>&lt;span class="n">Usuario&lt;/span>&lt;span class="p">];&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>In this case, the Windows login &lt;code>[DOMINIO\Usuario]&lt;/code> gets permission to execute stored procedures in the &lt;code>dbo&lt;/code> schema.&lt;/p>
&lt;h2 id="5-security-considerations">5. Security Considerations
&lt;/h2>&lt;ul>
&lt;li>&lt;strong>Strong passwords&lt;/strong>: Always use strong passwords for SQL Server logins. Weak passwords are one of the main attack vectors.&lt;/li>
&lt;li>&lt;strong>Least privilege&lt;/strong>: Avoid logins with administrative permissions unless absolutely necessary. Follow the principle of &amp;ldquo;least privilege&amp;rdquo;.&lt;/li>
&lt;li>&lt;strong>Windows logins&lt;/strong>: Whenever possible, use Windows logins, because they take advantage of the operating system&amp;rsquo;s security and authentication controls.&lt;/li>
&lt;li>&lt;strong>Auditing&lt;/strong>: It&amp;rsquo;s important to set up security auditing to monitor login activity, such as failed login attempts and logins with elevated privileges.&lt;/li>
&lt;/ul>
&lt;p>&lt;img src="https://alencarinosql.com.br/p/logins-sql-server/1.jpg"
width="705"
height="634"
srcset="https://alencarinosql.com.br/p/logins-sql-server/1_hu_d822462eef30d608.jpg 480w, https://alencarinosql.com.br/p/logins-sql-server/1_hu_3a4b96745022e00c.jpg 1024w"
loading="lazy"
alt="SQL Server Logins Configuration"
class="gallery-image"
data-flex-grow="111"
data-flex-basis="266px"
>&lt;/p>
&lt;h2 id="conclusion">Conclusion
&lt;/h2>&lt;p>Configuring logins in SQL Server is a fundamental task for ensuring security and access control over your data. By creating the right logins and applying security best practices, such as strong passwords and minimal permissions, you protect your database environment against unauthorized access. It&amp;rsquo;s also important to review permissions regularly and make sure logins are kept up to date and in line with your organization&amp;rsquo;s security policies.&lt;/p></description></item></channel></rss>