microsoft / azuredatastudio

Azure Data Studio is a data management and development tool with connectivity to popular cloud and on-premises databases. Azure Data Studio supports Windows, macOS, and Linux, with immediate capability to connect to Azure SQL and SQL Server. Browse the extension library for more database support options including MySQL, PostgreSQL, and MongoDB.
https://learn.microsoft.com/sql/azure-data-studio
MIT License
7.5k stars 883 forks source link

Azure Data Studio Build error SQL70001: This statement is not recognized in this context #25713

Open panos23kar opened 1 week ago

panos23kar commented 1 week ago

Im using the Azure Data Studio to build an .sqlproj which later on I use it in order to build and deploy a .dacpac file via Azure Pipelines to an Azure Database (SQL Server).

I'm creating user, roles, permissions .... Everything was going smoothly.

At some point I created a folder xxxxx and created an .sql file in it.

The file looks like that:

IF NOT EXISTS (SELECT * FROM sys.database_principals WHERE name = N'xxxxxx')
BEGIN
PRINT 'creating user: [xxxxxxx]...'
CREATE USER [xxxx] FROM EXTERNAL PROVIDER;
GRANT yyyyyy TO [xxxxxxxx];
PRINT 'adding [xxxxxxxx] to role: [zzzzzzzz]'
ALTER ROLE [zzzzzzzzz] ADD MEMBER [xxxxxxxxx];
END
.
.
.

From this point on when building the database project, I started receiving a

Build error SQL70001: This statement is not recognized in this context.

The .sqlproj contains the line which points to the location where the .sql file is:

<ItemGroup>
    ...
    <Folder Include="xxxx" />
    ...
</ItemGroup>

I realized from this issue (https://github.com/microsoft/azuredatastudio/issues/24125):

Only CREATE statements are supported in sql projects

Neverheless, its not complaining with an .sql script file which seems like this:

GRANT SELECT ON SCHEMA::[xxxx] TO [yyyyy]
GO
.
.
.

What even context means in this case? Is there any documentation on what is allowed and what not? Any ideas on what it can go wrong?

I also saw this issue (https://github.com/microsoft/DacFx/issues/88) but its not clear to me what needs to be done.

It seems that despite it creates schema objects (but initially checks if the object is already there), this statements need to go the Script.PostDeployment1.sql script.