Urgent.News

What's breaking now, across thousands of outlets.

AI

SQL Database Projects and AI: A Match Made in Heaven

Hey lovely readers, If you have ever asked an AI tool to write some SQL for you, you have probably seen this happen. You ask for a query, you get something that looks great, and then you run it and find out it uses a column that does not exist. Or a function your SQL Server version does not support. Or it quietly drops something you really wanted to keep. I wrote before about how AI tools are…

If you have ever asked an AI tool to generate SQL code, chances are you have experienced the potential pitfalls. You provide a query, and it often overlooks columns that do not exist, or utilizes functions incompatible with your SQL Server version. Once deployed, you may find the database quietly disregarding your wishes. In C#, you typically receive more immediate feedback through the compiler.

However, database errors are less forgiving. This is precisely why I believe SQL Database projects and AI complement each other beautifully. Allow me to elaborate on the reasons behind this synergy. If you are already acquainted with SQL Database projects, feel free to jump straight to the section discussing the harmonious relationship between AI and SQL projects.

For those who are unfamiliar, let us begin from the ground up. What is a SQL Database Project? A SQL Database project serves as a localized replica of your database schema. It encompasses tables, views, stored procedures, and functions, all encapsulated within .sql files within a project directory on your machine, alongside your application code and within the same Git repository.

If you are a .NET developer, you can liken it to a .csproj, but tailored for your database. Within these files, you write your schema, build the project, and generate an output ready for deployment. Consider a simple table definition within a SQL project: CREATE TABLE [dbo].[Customers] ([Id] INT NOT NULL PRIMARY KEY, [Name] NVARCHAR(100) NOT NULL, [Email] NVARCHAR(256) NOT NULL); Nothing groundbreaking, is it?

The true value lies in how you modify the schema. Unlike traditional step-by-step approaches, SQL projects adopt a declarative methodology. Here, you explicitly declare the desired state of your database, not the sequence of steps to achieve it. Adding a phone number to your customers table is as simple as modifying the existing file: CREATE TABLE [dbo].[Customers] ([Id] INT NOT NULL PRIMARY KEY, [Name] NVARCHAR(100) NOT NULL, [Email] NVARCHAR(256) NOT NULL, [PhoneNumber] NVARCHAR(20) NULL); Picture a blueprint for a house.

You do not provide a builder with a list of alterations, such as "knock down this wall, then add a window here." Instead, you present them with a blueprint depicting the final structure, and they determine the necessary changes. Building and Deploying Deploying a SQL project involves two crucial steps, facilitated by the dotnet build command.

First, the project undergoes validation, ensuring that all referenced objects truly exist. A view cannot utilize a table or column absent from your project. Additionally, the build verifies your syntax against the target SQL version, preventing the use of unsupported features. For instance, attempting to employ SQL Server 2022 JSON functions within a project targeting SQL Server 2017 would be rejected.

Once validation is complete, you obtain a .dacpac file, the build artifact encapsulating your entire schema. Deployment of this file is accomplished using SqlPackage. Upon publishing a .dacpac to a new database, it constructs all components in the proper sequence, ensuring that tables with foreign keys are established after the tables they reference.

For updates to an existing database, SqlPackage compares the .dacpac with the current database, generating the required ALTER statements to implement changes. This process ensures a smooth, incremental deployment process. An important note regarding project formats warrants mention. Earlier iterations of SQL projects were based on .NET Framework and primarily associated with Visual Studio on Windows.

However, the contemporary format is SDK-style, leveraging the Microsoft.Build.Sql SDK. This modern format operates on contemporary .NET, supports cross-platform compatibility, integrates seamlessly with NuGet package references for database dependencies, and automatically incorporates all .sql files within your project directory.

Microsoft strongly recommends adopting this format for new development, as it will be the supported standard moving forward. Tooling support varies across IDEs; therefore, here is a brief overview of current options. SDK-style projects are generally available in the SQL Database Projects extension for VS Code. JetBrains Rider supports them since version 2025.2 via a bundled plugin, offering project templates, database import functionality, schema comparison, and publishing capabilities.

As of now, Visual Studio 2022 previews the SDK-style format as a component, while Visual Studio 2026 primarily supports the original format. To explore the SDK-style format, begin with: dotnet new install Microsoft.Build.Sql.Templates dotnet new sqlproj -n MyDatabase dotnet build. The synergy between AI tools and SQL Database projects lies in the contextual understanding provided by the project files.

AI tools thrive on context, and having your entire schema encapsulated in plain text files within a repository offers unparalleled clarity. An AI agent residing within your editor can effortlessly access Tables/Customers.sql to examine column definitions, data types, and foreign key relationships without the need for database connection strings.

This eliminates guesswork and the necessity for direct database access. Moreover, if you lack a SQL project for an existing database, extracting one is a straightforward process. You can achieve this using VS Code or through the command line: sqlpackage /Action:Extract /SourceConnectionString: your connection string /TargetFile:MyDatabase /p:ExtractTargetFolder=MyDatabase.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in AI

More from Thursday 24 September →