<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Posts on Henrique Almeida</title><link>https://alencarinosql.com.br/en/post/</link><description>Recent content in Posts on Henrique Almeida</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Mon, 23 Nov 2026 20:30:00 -0300</lastBuildDate><atom:link href="https://alencarinosql.com.br/en/post/index.xml" rel="self" type="application/rss+xml"/><item><title>XNU Linux: the macOS kernel running as a module inside Linux</title><link>https://alencarinosql.com.br/en/p/xnu-linux-macos-como-modulo-do-kernel-linux/</link><pubDate>Mon, 23 Nov 2026 20:30:00 -0300</pubDate><guid>https://alencarinosql.com.br/en/p/xnu-linux-macos-como-modulo-do-kernel-linux/</guid><description>&lt;img src="https://alencarinosql.com.br/p/xnu-linux-macos-como-modulo-do-kernel-linux/capa.jpg" alt="Featured image of post XNU Linux: the macOS kernel running as a module inside Linux" />&lt;p>Picture typing a single command on Linux, an &lt;code>insmod&lt;/code>, and watching the macOS login screen show up. No virtual machine in between. Apple&amp;rsquo;s kernel loaded like any other driver, sharing the same CPU with Linux. That&amp;rsquo;s what a project published on GitHub on November 20, 2026 is showing. And it made it all the way to Finder, the Dock and Safari.&lt;/p>
&lt;p>&lt;img src="https://alencarinosql.com.br/p/xnu-linux-macos-como-modulo-do-kernel-linux/capa.jpg"
width="1280"
height="800"
srcset="https://alencarinosql.com.br/p/xnu-linux-macos-como-modulo-do-kernel-linux/capa_hu_9845a578c9f5e6eb.jpg 480w, https://alencarinosql.com.br/p/xnu-linux-macos-como-modulo-do-kernel-linux/capa_hu_507b2da10b4f6804.jpg 1024w"
loading="lazy"
alt="Two terminals side by side: on the left macOS 27.2.1 with the Darwin kernel, on the right Ubuntu with the xnu and fki modules loaded"
class="gallery-image"
data-flex-grow="160"
data-flex-basis="384px"
>
&lt;em>Image: xnulinux project (Heyang Zhou and contributors) — &lt;a class="link" href="https://github.com/AFK-surf/xnulinux" target="_blank" rel="noopener"
>source&lt;/a>&lt;/em>&lt;/p>
&lt;h2 id="first-what-is-this-xnu-thing">First, what is this XNU thing
&lt;/h2>&lt;p>Every operating system has a kernel, the part that talks straight to the hardware: memory, processor, disk, network. On Linux, the kernel is Linux itself. On macOS, it&amp;rsquo;s called XNU, short for &amp;ldquo;X is Not Unix&amp;rdquo;.&lt;/p>
&lt;p>XNU was born at NeXT, for the NeXTSTEP system, and Apple has been developing it since December 1996. It&amp;rsquo;s a hybrid kernel. It combines Mach, a microkernel created at Carnegie Mellon University, with a good chunk of BSD code. The detail that matters today is that Apple publishes XNU&amp;rsquo;s source code as open source, under the APSL license. In other words, anyone can download it and compile it.&lt;/p>
&lt;p>But compiling the kernel is one thing. Getting it to run, bring up the whole macOS graphical interface and open apps is a very different thing.&lt;/p>
&lt;h2 id="what-the-xnulinux-project-pulled-off">What the xnulinux project pulled off
&lt;/h2>&lt;p>The repository is called xnulinux and lives under the AFK-surf account. The footer credits Heyang Zhou and contributors. According to the GitHub API, it was created on November 20, 2026. Three days later, it already had almost 2,000 stars and nearly 600 points on Hacker News.&lt;/p>
&lt;p>The pitch, in the README&amp;rsquo;s words, is to run macOS userspace on Linux by loading the macOS kernel into it. XNU is built from the code Apple publishes and becomes a loadable module, &lt;code>xnu.ko&lt;/code>. That module goes into an arm64 Linux kernel with no modifications at all. The author says that with the stock Ubuntu kernel, &lt;code>insmod xnu.ko&lt;/code> boots a macOS 27 system disk to the desktop, with loginwindow, WindowServer, Dock, Finder, Safari and Chrome.&lt;/p>
&lt;p>He tested two setups. The first is an Apple Silicon Mac running an arm64 Linux virtual machine. There, according to the project, macOS 27.2.1 reaches the desktop in 31 to 48 seconds. A regular macOS VM on the same Mac took 49. The author also reports a 30-minute stress test, with 40 rounds of opening Safari, Notes, Calculator and System Settings, without a single kernel panic.&lt;/p>
&lt;p>The second setup is the eye-catching one: an NVIDIA DGX Spark, with no virtualization at all, running Ubuntu directly on the machine. macOS boots with 4 cores and 8 GB of RAM reserved for XNU. There, for now, the screen is drawn by the CPU, because the GPU bridge only works with a virtual GPU.&lt;/p>
&lt;p>&lt;img src="https://alencarinosql.com.br/p/xnu-linux-macos-como-modulo-do-kernel-linux/02-dgx-spark-about.jpg"
width="1600"
height="1104"
srcset="https://alencarinosql.com.br/p/xnu-linux-macos-como-modulo-do-kernel-linux/02-dgx-spark-about_hu_88efc592336a7c6f.jpg 480w, https://alencarinosql.com.br/p/xnu-linux-macos-como-modulo-do-kernel-linux/02-dgx-spark-about_hu_1b06a4c383e8fcc0.jpg 1024w"
loading="lazy"
alt="The hosted macOS terminal showing processor details and the About This Mac window with the name xnulinux and 7.80 GB of memory"
class="gallery-image"
data-flex-grow="144"
data-flex-basis="347px"
>
&lt;em>Image: xnulinux project (Heyang Zhou and contributors) — &lt;a class="link" href="https://github.com/AFK-surf/xnulinux" target="_blank" rel="noopener"
>source&lt;/a>&lt;/em>&lt;/p>
&lt;p>An honest caveat: all of these numbers come from the repository itself. Phoronix also covered the project, but so far I haven&amp;rsquo;t seen anyone independent reproducing the tests.&lt;/p>
&lt;h2 id="how-two-kernels-share-the-same-machine">How two kernels share the same machine
&lt;/h2>&lt;p>This is the part I find most interesting. Normally, to run one system inside another, you use a hypervisor, the software that creates virtual machines. xnulinux doesn&amp;rsquo;t. XNU drives the CPU and the MMU, the unit that translates memory addresses, directly.&lt;/p>
&lt;p>The trick lives in a second module, &lt;code>fki.ko&lt;/code>. The name stands for &amp;ldquo;foreign kernel interface&amp;rdquo;. It&amp;rsquo;s what lends XNU the things that belong to Linux: memory, timers, files, network and GPU.&lt;/p>
&lt;p>In practice, it works like this. Each CPU macOS sees is actually a Linux kernel thread pinned to a physical core. XNU runs on that thread until it needs something from the host, like waiting on a timer or doing I/O. Then it hands control back. When a page fault or an exception happens in XNU code, &lt;code>fki.ko&lt;/code> forwards it to XNU&amp;rsquo;s own handler.&lt;/p>
&lt;p>Memory is one contiguous block that Linux sets aside right at the start, and XNU manages that block its own way. The macOS disk is a file on the Linux side. The network is a Linux network interface. Even Metal, Apple&amp;rsquo;s graphics API, gets translated to Vulkan and reaches the host GPU through a bridge.&lt;/p>
&lt;p>&lt;img src="https://alencarinosql.com.br/p/xnu-linux-macos-como-modulo-do-kernel-linux/04-commons-xnu.png"
width="1920"
height="1175"
srcset="https://alencarinosql.com.br/p/xnu-linux-macos-como-modulo-do-kernel-linux/04-commons-xnu_hu_cf149eb8263ac5bd.png 480w, https://alencarinosql.com.br/p/xnu-linux-macos-como-modulo-do-kernel-linux/04-commons-xnu_hu_65c657c8043d123a.png 1024w"
loading="lazy"
alt="Layered diagram of XNU: user mode on top; in kernel mode, the BSD layer with file systems and networking and, below it, Mach with virtual memory, IPC and scheduling"
class="gallery-image"
data-flex-grow="163"
data-flex-basis="392px"
>
&lt;em>Image: Robryecran77 / Wikimedia Commons, CC BY-SA 4.0 — &lt;a class="link" href="https://commons.wikimedia.org/wiki/File:The_XNU_Kernel_Graphic.svg" target="_blank" rel="noopener"
>source&lt;/a>&lt;/em>&lt;/p>
&lt;p>Then there are the licenses, which are both a technical and a legal piece. &lt;code>fki.ko&lt;/code> is GPL 2.0, the Linux license. &lt;code>xnu.ko&lt;/code> and the XNU patches stay under the APSL, Apple&amp;rsquo;s license. That split is on purpose. The idea of wrapping XNU in a Linux module isn&amp;rsquo;t even new: Darling, a compatibility layer that runs macOS programs on Linux, already does something similar, but there the module mostly handles communication between processes. xnulinux goes much further and boots the whole system.&lt;/p>
&lt;h2 id="why-this-matters-even-if-youll-never-run-macos-on-linux">Why this matters even if you&amp;rsquo;ll never run macOS on Linux
&lt;/h2>&lt;p>To start with, the project doesn&amp;rsquo;t ship anything from Apple. According to the README, you need your own macOS 27 installation, made with the official installer, and a Mac with Xcode to build the disk. You also need an arm64 Linux machine with at least 16 GB of RAM. So this isn&amp;rsquo;t something for your x86 PC today.&lt;/p>
&lt;p>What matters for developers and infrastructure folks is what it shows. A kernel doesn&amp;rsquo;t have to be the absolute owner of the machine. With a well-designed interface, you can put two kernels from different families side by side, each with its own processes. Today this looks like a lab experiment. But I&amp;rsquo;d bet ideas like this resurface down the road in lightweight virtualization and in testing tools.&lt;/p>
&lt;p>&lt;img src="https://alencarinosql.com.br/p/xnu-linux-macos-como-modulo-do-kernel-linux/03-safari-kernel-org.jpg"
width="1280"
height="800"
srcset="https://alencarinosql.com.br/p/xnu-linux-macos-como-modulo-do-kernel-linux/03-safari-kernel-org_hu_2de02b5fc729e7a3.jpg 480w, https://alencarinosql.com.br/p/xnu-linux-macos-como-modulo-do-kernel-linux/03-safari-kernel-org_hu_6d43622da7b39315.jpg 1024w"
loading="lazy"
alt="Safari running on the hosted macOS, open on kernel.org with the list of Linux kernel releases"
class="gallery-image"
data-flex-grow="160"
data-flex-basis="384px"
>
&lt;em>Image: xnulinux project (Heyang Zhou and contributors) — &lt;a class="link" href="https://github.com/AFK-surf/xnulinux" target="_blank" rel="noopener"
>source&lt;/a>&lt;/em>&lt;/p>
&lt;h2 id="my-take">My take
&lt;/h2>&lt;p>It&amp;rsquo;s impressive, but it&amp;rsquo;s a proof of concept, and the author makes that clear. In the docs, he lists what is still a stopgap. The macOS system partition loses its integrity seal. AMFI, the mechanism that checks code signatures, gets patched. SIP, System Integrity Protection, is relaxed. Kernel address randomization is turned off. And code that leans heavily on memory can be up to 1.6 times slower.&lt;/p>
&lt;p>My reading is that today it&amp;rsquo;s for learning, not for production. There&amp;rsquo;s also a question the README doesn&amp;rsquo;t answer: whether running macOS this way, especially on hardware that isn&amp;rsquo;t Apple&amp;rsquo;s, like the DGX Spark, complies with the macOS license. I&amp;rsquo;m not a lawyer. But before using this at a company, I&amp;rsquo;d ask that question.&lt;/p>
&lt;h2 id="what-now">What now?
&lt;/h2>&lt;p>If you have an Apple Silicon Mac and like tinkering with operating systems, the README is the place to go. It has the build scripts step by step and a page just for the current limitations. If you don&amp;rsquo;t, it&amp;rsquo;s still worth reading the section that explains how the project works. You rarely get to see, in a short text, how two kernels manage to live together.&lt;/p>
&lt;p>A kernel Apple built for Macs booting as a module inside Linux, three days into a public project. What would you use this for? Tell me in the comments.&lt;/p>
&lt;h2 id="sources">Sources
&lt;/h2>&lt;ul>
&lt;li>&lt;a class="link" href="https://github.com/AFK-surf/xnulinux" target="_blank" rel="noopener"
>xnulinux — official GitHub repository (README)&lt;/a>&lt;/li>
&lt;li>&lt;a class="link" href="https://github.com/AFK-surf/xnulinux/blob/main/docs/production.md" target="_blank" rel="noopener"
>xnulinux — docs/production.md (current limitations)&lt;/a>&lt;/li>
&lt;li>&lt;a class="link" href="https://github.com/apple-oss-distributions/xnu" target="_blank" rel="noopener"
>XNU source code published by Apple&lt;/a>&lt;/li>
&lt;li>&lt;a class="link" href="https://en.wikipedia.org/wiki/XNU" target="_blank" rel="noopener"
>XNU — Wikipedia&lt;/a>&lt;/li>
&lt;li>&lt;a class="link" href="https://en.wikipedia.org/wiki/Darling_%28software%29" target="_blank" rel="noopener"
>Darling (software) — Wikipedia&lt;/a>&lt;/li>
&lt;li>&lt;a class="link" href="https://www.phoronix.com/news/XNU-Linux" target="_blank" rel="noopener"
>Phoronix — XNU Linux Lets macOS Userspace Processes Run On Linux&lt;/a>&lt;/li>
&lt;/ul></description></item><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 Functions</title><link>https://alencarinosql.com.br/en/p/funcoes-sql-server/</link><pubDate>Sun, 27 Jul 2025 12:00:00 -0300</pubDate><guid>https://alencarinosql.com.br/en/p/funcoes-sql-server/</guid><description>&lt;h1 id="sql-server-functions-power-and-best-practices">SQL Server Functions: Power and Best Practices
&lt;/h1>&lt;p>Hi everyone! Today we&amp;rsquo;re digging into &lt;strong>functions in SQL Server&lt;/strong>. They&amp;rsquo;re great tools for wrapping up logic and reusing code, but, like everything in SQL, they need some care so they don&amp;rsquo;t turn into a performance bottleneck.&lt;/p>
&lt;h2 id="what-are-functions-in-sql-server">What Are Functions in SQL Server?
&lt;/h2>&lt;p>A function in SQL Server is a database object that takes input parameters, runs a set of actions (queries, calculations, and so on) and returns either a single value (scalar) or a table. They&amp;rsquo;re similar to stored procedures, with one crucial difference: &lt;strong>functions can be used inside &lt;code>SELECT&lt;/code> statements&lt;/strong>, &lt;code>WHERE&lt;/code>, &lt;code>HAVING&lt;/code>, and even inside other functions, which makes them extremely versatile.&lt;/p>
&lt;p>There are two main kinds of functions we can create:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Scalar Functions (Scalar-valued Functions - T-SQL)&lt;/strong>: Return a single value of any data type.&lt;/li>
&lt;li>&lt;strong>Table-valued Functions (TVFs)&lt;/strong>: Return a table. They come in two flavors:
&lt;ul>
&lt;li>&lt;strong>Inline Table-valued Functions (iTVFs)&lt;/strong>: Simpler and faster, because they essentially expand the function&amp;rsquo;s logic straight into the calling query, like a parameterized view.&lt;/li>
&lt;li>&lt;strong>Multi-statement Table-valued Functions (mTVFs)&lt;/strong>: Allow more complex logic, including multiple T-SQL statements, but can hurt performance significantly because of less aggressive optimizations and, often, the need to materialize data.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ol>
&lt;h2 id="why-use-functions">Why Use Functions?
&lt;/h2>&lt;ul>
&lt;li>&lt;strong>Code Reuse&lt;/strong>: Write the logic once and use it in many places.&lt;/li>
&lt;li>&lt;strong>Modularity&lt;/strong>: Break complex problems into smaller, more manageable pieces.&lt;/li>
&lt;li>&lt;strong>Consistency&lt;/strong>: Make sure a specific business rule is applied the same way across the whole database.&lt;/li>
&lt;li>&lt;strong>Simpler Maintenance&lt;/strong>: Changes to the logic only need to be made in one place.&lt;/li>
&lt;/ul>
&lt;h2 id="creating-functions-examples-and-best-practices">Creating Functions: Examples and Best Practices
&lt;/h2>&lt;p>Let&amp;rsquo;s look at some examples and, more importantly, the best practices!&lt;/p>
&lt;h3 id="1-simple-scalar-function">1. Simple Scalar Function
&lt;/h3>&lt;p>Imagine you need to calculate the total tax on a price.&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;span class="lnt">13
&lt;/span>&lt;span class="lnt">14
&lt;/span>&lt;span class="lnt">15
&lt;/span>&lt;span class="lnt">16
&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="k">FUNCTION&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">dbo&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">CalcularImposto&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="n">Preco&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nb">DECIMAL&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="mi">18&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="mi">2&lt;/span>&lt;span class="p">),&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="n">PercentualImposto&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nb">DECIMAL&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="mi">5&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="mi">2&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">RETURNS&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nb">DECIMAL&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="mi">18&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="mi">2&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">AS&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">BEGIN&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">RETURN&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="n">Preco&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">*&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="n">PercentualImposto&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">/&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="mi">100&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">END&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">GO&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">-- Usage 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>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="n">Produto&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">PrecoUnitario&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">dbo&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">CalcularImposto&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">PrecoUnitario&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="mi">10&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="mi">00&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">AS&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">ValorImposto&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">PrecoUnitario&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">+&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">dbo&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">CalcularImposto&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">PrecoUnitario&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="mi">10&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="mi">00&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">AS&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">PrecoTotal&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>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="n">MinhaTabelaDeProdutos&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>&lt;strong>Performance Tip&lt;/strong>: Scalar functions, especially ones that do complex work or access data, can cause performance problems when called for every row of large result sets (the dreaded &lt;strong>RBAR - Row By Agonizing Row&lt;/strong>). Ask yourself whether the logic could be handled directly in your main query, or whether an iTVF would be a better fit.&lt;/p>
&lt;h3 id="2-table-valued-function-inline-tvf">2. Table-valued Function (Inline TVF)
&lt;/h3>&lt;p>If you need to return a result set based on parameters, iTVFs are your best friends for performance. The query optimizer handles them very efficiently.&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;span class="lnt">13
&lt;/span>&lt;span class="lnt">14
&lt;/span>&lt;span class="lnt">15
&lt;/span>&lt;span class="lnt">16
&lt;/span>&lt;span class="lnt">17
&lt;/span>&lt;span class="lnt">18
&lt;/span>&lt;span class="lnt">19
&lt;/span>&lt;span class="lnt">20
&lt;/span>&lt;span class="lnt">21
&lt;/span>&lt;span class="lnt">22
&lt;/span>&lt;span class="lnt">23
&lt;/span>&lt;span class="lnt">24
&lt;/span>&lt;span class="lnt">25
&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="k">FUNCTION&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">dbo&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">ObterProdutosPorCategoria&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="n">Categoria&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">NVARCHAR&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="mi">50&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">RETURNS&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">TABLE&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">AS&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">RETURN&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="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">SELECT&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">ProdutoID&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">NomeProduto&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">Preco&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>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="n">Produtos&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">WHERE&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">Categoria&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="n">Categoria&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="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">GO&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">-- Usage 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>&lt;/span>&lt;span class="line">&lt;span class="cl">&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;span class="line">&lt;span class="cl">&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="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">p&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">Preco&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>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="n">dbo&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">ObterProdutosPorCategoria&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;Eletrônicos&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">AS&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">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">WHERE&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">p&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">Preco&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">&amp;gt;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="mi">500&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>&lt;strong>Why is an iTVF better than an mTVF in most cases?&lt;/strong> The query optimizer can &amp;ldquo;inline&amp;rdquo; (expand) the iTVF logic directly into the calling query, which allows broader optimizations and effective use of indexes. mTVFs, on the other hand, often materialize their results in a &lt;code>worktable&lt;/code>, which can block good optimizations.&lt;/p>
&lt;h3 id="3-table-valued-function-multi-statement-tvf---be-careful">3. Table-valued Function (Multi-statement TVF) - Be Careful!
&lt;/h3>&lt;p>Although they&amp;rsquo;re useful for complex logic involving multiple steps or variables, mTVFs should be used with caution because of their potential impact on performance.&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;span class="lnt">13
&lt;/span>&lt;span class="lnt">14
&lt;/span>&lt;span class="lnt">15
&lt;/span>&lt;span class="lnt">16
&lt;/span>&lt;span class="lnt">17
&lt;/span>&lt;span class="lnt">18
&lt;/span>&lt;span class="lnt">19
&lt;/span>&lt;span class="lnt">20
&lt;/span>&lt;span class="lnt">21
&lt;/span>&lt;span class="lnt">22
&lt;/span>&lt;span class="lnt">23
&lt;/span>&lt;span class="lnt">24
&lt;/span>&lt;span class="lnt">25
&lt;/span>&lt;span class="lnt">26
&lt;/span>&lt;span class="lnt">27
&lt;/span>&lt;span class="lnt">28
&lt;/span>&lt;span class="lnt">29
&lt;/span>&lt;span class="lnt">30
&lt;/span>&lt;span class="lnt">31
&lt;/span>&lt;span class="lnt">32
&lt;/span>&lt;span class="lnt">33
&lt;/span>&lt;span class="lnt">34
&lt;/span>&lt;span class="lnt">35
&lt;/span>&lt;span class="lnt">36
&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="c1">-- CAREFUL: This is an mTVF example; usually avoid it if an iTVF is possible.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="k">CREATE&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">FUNCTION&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">dbo&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">ObterRelatorioVendasMensal&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="n">Ano&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nb">INT&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="n">Mes&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nb">INT&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">RETURNS&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="n">Relatorio&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">TABLE&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="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">ProdutoID&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nb">INT&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">NomeProduto&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">NVARCHAR&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="mi">255&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">TotalVendas&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nb">DECIMAL&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="mi">18&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="mi">2&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">MediaVendasDiaria&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nb">DECIMAL&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="mi">18&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="mi">2&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="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">AS&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">BEGIN&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">INSERT&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">INTO&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="n">Relatorio&lt;/span>&lt;span class="w"> &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 class="n">NomeProduto&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">TotalVendas&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">SELECT&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">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;span class="line">&lt;span class="cl">&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="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">SUM&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">iv&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">Quantidade&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">*&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">iv&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">PrecoUnitario&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">FROM&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">Produtos&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">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">INNER&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">JOIN&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">ItensVenda&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">iv&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">ON&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="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">iv&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">ProdutoID&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>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="n">Vendas&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">v&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">ON&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">iv&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">VendaID&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">v&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">VendaID&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">WHERE&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">YEAR&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">v&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">DataVenda&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="n">Ano&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">AND&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">MONTH&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">v&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">DataVenda&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="n">Mes&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">GROUP&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">BY&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">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 class="n">p&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">NomeProduto&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="k">UPDATE&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="n">Relatorio&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">SET&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">MediaVendasDiaria&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">TotalVendas&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">/&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">DAY&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">EOMONTH&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="k">CAST&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="k">CAST&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="n">Ano&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">AS&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nb">VARCHAR&lt;/span>&lt;span class="p">)&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;-&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">+&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">CAST&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="n">Mes&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">AS&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nb">VARCHAR&lt;/span>&lt;span class="p">)&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;-01&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">AS&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nb">DATE&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="k">RETURN&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">END&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">GO&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">-- Usage 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="o">*&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">dbo&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">ObterRelatorioVendasMensal&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="mi">2023&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="mi">7&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>&lt;strong>When should you use an mTVF?&lt;/strong> Only when the logic is truly complex and can&amp;rsquo;t be expressed concisely in an iTVF, and after considering other options such as views, CTEs or procedures that return results.&lt;/p>
&lt;h2 id="essential-best-practices">Essential Best Practices
&lt;/h2>&lt;ol>
&lt;li>&lt;strong>Avoid Scalar Functions on Columns of Large Tables in &lt;code>WHERE&lt;/code> or &lt;code>JOIN&lt;/code> Clauses&lt;/strong>: This prevents index use and forces a table scan. For example, &lt;code>WHERE dbo.MinhaFuncao(Coluna) = 'Valor'&lt;/code> is a recipe for performance problems.&lt;/li>
&lt;li>&lt;strong>Prefer iTVFs over mTVFs&lt;/strong>: Whenever possible, shape your logic to fit an Inline Table-valued Function so you benefit from SQL Server&amp;rsquo;s optimizations.&lt;/li>
&lt;li>&lt;strong>Keep the Logic Simple&lt;/strong>: Overly complex functions become hard to debug and maintain, on top of potential performance issues.&lt;/li>
&lt;li>&lt;strong>Use Proper Schemas&lt;/strong>: Always prefix your functions with the right schema (e.g., &lt;code>dbo.MinhaFuncao&lt;/code>).&lt;/li>
&lt;li>&lt;strong>Understand the Impact on Optimization&lt;/strong>: Functions can &amp;ldquo;hide&amp;rdquo; intent from the query optimizer, leading to suboptimal execution plans. Use &lt;code>SET STATISTICS IO ON&lt;/code> and &lt;code>SET STATISTICS TIME ON&lt;/code>, and analyze the execution plan to understand what&amp;rsquo;s going on.&lt;/li>
&lt;li>&lt;strong>Test, Test, Test!&lt;/strong>: Test your functions with different data volumes and scenarios to make sure they perform well and return the expected results.&lt;/li>
&lt;/ol>
&lt;p>Functions are a core part of any SQL Server developer&amp;rsquo;s toolkit. Used correctly, they can greatly improve the efficiency and maintainability of your code. But remember: with great power comes great optimization responsibility!&lt;/p>
&lt;p>Have you run into an interesting challenge with functions in SQL Server? Share your experience in the comments!&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><item><title>Power BI News – July 2025</title><link>https://alencarinosql.com.br/en/p/novidadespowerbijulho2025/</link><pubDate>Fri, 25 Jul 2025 12:00:00 -0300</pubDate><guid>https://alencarinosql.com.br/en/p/novidadespowerbijulho2025/</guid><description>&lt;h1 id="-power-bi-news--july-2025">🚀 Power BI News – July 2025
&lt;/h1>&lt;h2 id="-main-highlights">🆕 Main Highlights
&lt;/h2>&lt;ul>
&lt;li>&lt;strong>Improved integration with Microsoft 365&lt;/strong>
&lt;ul>
&lt;li>Makes collaboration and the use of business features easier.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>Smarter Copilot&lt;/strong>
&lt;ul>
&lt;li>Generates automated, contextual insights.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>Support for Power BI Report Server and SQL Server 2025&lt;/strong>
&lt;ul>
&lt;li>Improves on-premises report distribution.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;hr>
&lt;h2 id="-technical-updates--power-bi-desktop">🛠️ Technical Updates – Power BI Desktop
&lt;/h2>&lt;p>&lt;strong>Current version:&lt;/strong> &lt;code>2.144.1378.0&lt;/code>
&lt;strong>Release date:&lt;/strong> &lt;code>July 3, 2025&lt;/code>&lt;/p>
&lt;h3 id="-june-2025-qfe-3">🔧 June 2025 QFE 3
&lt;/h3>&lt;ul>
&lt;li>Fixed an error when refreshing tables with an outdated &lt;code>model.abf&lt;/code>.&lt;/li>
&lt;li>Improved Copilot error message to point out an unsupported region.&lt;/li>
&lt;/ul>
&lt;h3 id="-june-2025-qfe-1">🔧 June 2025 QFE 1
&lt;/h3>&lt;ul>
&lt;li>Fix for incremental refresh validation with multiple sources.&lt;/li>
&lt;li>High-contrast UI adjustments in the &amp;ldquo;Manage Relationships&amp;rdquo; dialog.&lt;/li>
&lt;li>Fix for conditional formatting that removed colors by mistake.&lt;/li>
&lt;/ul>
&lt;hr>
&lt;h2 id="-official-video">📹 Official Video
&lt;/h2>&lt;p>Watch the video with the main new features on the official Power BI channel:&lt;/p>
&lt;p>➡️ &lt;a class="link" href="https://www.youtube.com/watch?v=RABiSIUiCH4" target="_blank" rel="noopener"
>Power BI Update – July 2025 Feature Summary&lt;/a>&lt;/p>
&lt;hr>
&lt;h2 id="-quick-summary">📌 Quick Summary
&lt;/h2>&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Category&lt;/th>
&lt;th>Update&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>&lt;strong>Microsoft 365&lt;/strong>&lt;/td>
&lt;td>Improved integration&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;strong>Copilot&lt;/strong>&lt;/td>
&lt;td>Smarter and more contextual&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;strong>Report Server&lt;/strong>&lt;/td>
&lt;td>Support for PBIRS and SQL Server 2025&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;strong>Fixes&lt;/strong>&lt;/td>
&lt;td>Stability in refresh, UI and visuals&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;strong>Current version&lt;/strong>&lt;/td>
&lt;td>&lt;code>2.144.1378.0&lt;/code> – July 3, 2025&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;hr>
&lt;h2 id="-recommended-update">📥 Recommended Update
&lt;/h2>&lt;p>The July version is the latest and the recommended one.
👉 &lt;a class="link" href="https://learn.microsoft.com/en-us/power-bi/fundamentals/desktop-get-the-desktop" target="_blank" rel="noopener"
>Click here to download Power BI Desktop&lt;/a>&lt;/p>
&lt;p>More details are available in the &lt;a class="link" href="https://learn.microsoft.com/en-us/power-bi/fundamentals/desktop-change-log" target="_blank" rel="noopener"
>full change log&lt;/a>.&lt;/p>
&lt;hr>
&lt;p>&lt;em>Source: &lt;a class="link" href="https://learn.microsoft.com/en-us/power-bi/fundamentals/desktop-latest-update" target="_blank" rel="noopener"
>Power BI Desktop – Latest Update&lt;/a>&lt;/em>&lt;/p></description></item></channel></rss>