question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PostDeployment script with SQLCMD :R is failing build

See original GitHub issue
  • SqlPackage or DacFx Version: 0.1.3 -preview
  • .NET Framework (Windows-only) or .NET Core: 6.0.3
  • Environment (local platform and source/target platforms): Windows 10, Azure Data Studio

Steps to Reproduce:

  1. Create new SQL Server Database project in azure data studio.
  2. Add folder and files that is not supposed to be built.
  3. Change your sqlproj file to look something like this
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0">
  <Sdk Name="Microsoft.Build.Sql" Version="0.1.3-preview" />
  <PropertyGroup>
    <Name>DBName</Name>
    <ProjectGuid>{961FE558-959F-46CE-A911-5FE5B0779667}</ProjectGuid>
    <DSP>Microsoft.Data.Tools.Schema.Sql.Sql150DatabaseSchemaProvider</DSP>
    <ModelCollation>1033, CI</ModelCollation>
  </PropertyGroup>
  <Target Name="BeforeBuild">
    <Delete Files="$(BaseIntermediateOutputPath)\project.assets.json" />
  </Target>
  <ItemGroup>
    <PostDeploy Include="Script.PostDeployment.sql"/>
  </ItemGroup>
  <ItemGroup>
    <Build Remove="Users\somescript1.sql"/>
    <Build Remove="Users\somescript2.sql"/>
    <Build Remove="Users\somescript3.sql"/>
    <Build Remove="Users\somescript4.sql"/>
    <Build Remove="Users\somescript5.sql"/>
  </ItemGroup>
</Project>
  1. Add PostDeployment with SQLCMD :r targeting your files that is removed from the build.
  2. Try build project with: dotnet build /p:NetCoreBuild=true from cmd or build action from Azure Data Studio
  3. Build fails due to post deployment script.
  4. Output:
...DBName\Users\somescript1.sql(3,6,3,6): Build error SQL72008: Variable x is not defined. [...DBName\DBName.sqlproj]
...DBName\Users\somescript2.sql(12,25,12,25): Build error SQL72008: Variable x is not defined. [...DBName\DBName.sqlproj]
...DBName\Users\somescript3.sql(12,25,12,25): Build error SQL72008: Variable x is not defined. [...DBName\DBName.sqlproj]
...DBName\Users\somescript4.sql(12,25,12,25): Build error SQL72008: Variable x is not defined. [...DBName\DBName.sqlproj]
...DBName\Users\somescript5.sql(19,22,19,22): Build error SQL72007: The syntax check failed 'Incorrect syntax near ].' in the batch near: [...DBName\DBName.sqlproj]

(DacFx/SqlPackage/SSMS/Azure Data Studio)

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
zijchencommented, Aug 19, 2022

I haven’t had a chance to look into this yet, but I’ll prioritize this as it appears to be a common issue.

1reaction
ErikEJcommented, Jun 8, 2022

@zijchen A lot of work went into fixing this in your “sister” project: https://github.com/rr-wfm/MSBuild.Sdk.SqlProj/commit/01929bdf665fe79202ad2ac91a3376a857a6477c

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is :r SQLCMD command marked as wrong in Post ...
I have worked a couple of times with post deployment scripts and always, intuitively used the build action "PostDeploy", because that is what...
Read more >
Scripts referenced by :r in post-deployment script show error
I'm creating a DACPAC using a database project in Visual Studio 2017. I have a single post-deployment script (with a build action of...
Read more >
Working With Pre and Post Deployment Scripts
This section covers scripts that can be part of the database project deployment that get run ... PostDeployment.sql script using the :r command...
Read more >
Post-Deployment script: Incorrect syntax near : · Issue #99
I've added a Post-Deployment Sql script as follows ... Use SQLCMD syntax to include a file in the post-deployment script. Example: :r ....
Read more >
Azure sql database deployment error when use variable in ...
So I created a post-deployment script that runs some code according to the environment. the code in postdeployment script is like this. IF...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found