PGSSoft / terraform-provider-mssql

MS SQL Terraform provider
MIT License
17 stars 9 forks source link

mssql_script fails when GO statement is used #117

Open ibuiltthis opened 1 year ago

ibuiltthis commented 1 year ago

When creating a mssql_script resource that uses 'GO' in the script the resource fails to create.

The error is:

mssql_script.ecamap_setup: Creating...
╷
│ Error: Failed to execute SQL script
│
│   with mssql_script.ecamap_setup,
│   on 05_sql_database_setup.tf line 62, in resource "mssql_script" "setup":
│   62: resource "mssql_script" "setup" {
│
│ mssql: Incorrect syntax near 'GO'.
╵

Any help?

4iter4life commented 1 year ago

same here. I can confirm this behavior

tf plan to reproduce:

  + resource "mssql_script" "this" {
      + database_id   = "1"
      + delete_script = "ALTER DATABASE [lock] SET CHANGE_TRACKING = OFF"
      + id            = (known after apply)
      + read_script   = "SELECT COUNT(*) AS [is_enabled] FROM sys.change_tracking_databases WHERE database_id=8"
      + state         = {
          + "is_enabled" = "1"
        }
      + update_script = <<-EOT
            USE MASTER;

            DECLARE @database_name VARCHAR(256)
            DECLARE @sql_cmd VARCHAR(2000)

            SET @database_name = 'TEST'
            SET @sql_cmd = '
            CREATE DATABASE ' + @database_name + ' COLLATE Latin1_General_CI_AS
            '
            EXEC (@sql_cmd)
            GO
        EOT
    }

output:

│  203: resource "mssql_script" "this" {
│ 
│ mssql: Incorrect syntax near 'GO'.
pregress commented 1 year ago

GO is a batch separator and is not part of T-SQL, and is only interpreted by SSMS or other tools. https://learn.microsoft.com/en-us/sql/t-sql/language-elements/sql-server-utilities-statements-go?view=sql-server-ver16&redirectedfrom=MSDN