Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlserver__alter_column_type - Not working for column change #338

Closed
Tracked by #376
Stefan-Rann opened this issue Feb 24, 2023 · 1 comment · Fixed by #376
Closed
Tracked by #376

sqlserver__alter_column_type - Not working for column change #338

Stefan-Rann opened this issue Feb 24, 2023 · 1 comment · Fixed by #376

Comments

@Stefan-Rann
Copy link

Hi,
this macro generates an error on a simple length adjustment of a column.
I am using SQL Server 2022 (on prem) with dbt-core 1.3.2 and dbt-sqlserver 1.3.1.

To replicate setup an incremental model.

  1. Setup the your table to include a column like varchar(50), alter the column to include varchar (5000) sql below
  2. Run your model, which will trigger the alter_column_type macro
  3. It will fail

The change generates this code:

alter table "mydb"."myschema"."myschema_accounting_sequences_processed" add dwh_business_key__dbt_alter character varying(50);
update "mydb"."myschema"."myschema_accounting_sequences_processed" set dwh_business_key__dbt_alter = dwh_business_key;
alter table "mydb"."myschema"."myschema_accounting_sequences_processed" drop column dwh_business_key;
exec sp_rename '"mydb".myschema.myschema_accounting_sequences_processed.dwh_business_key__dbt_alter', 'dwh_business_key', 'column'

The code results in this error:

('42S22', '[42S22] [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Invalid column name "dwh_business_key__dbt_alter". (207) (SQLExecDirectW)')

It is the same error as the last comment of #169

In SQL Server it is not allowed to have an alter table statement dirctly followed by an update statement in the same batch.
You have to use GO in between.
So if you execute the statements line by line, everything works fine. But in a single batch they won't.
I am not sure what this change would mean for consitency and rollback scenarioes in case of errors.
But probably the suggestion from @panasenco in #169 would be a better implementation?

@sdebruyn
Copy link
Member

fixed in #376

@sdebruyn sdebruyn reopened this May 15, 2023
@sdebruyn sdebruyn modified the milestone: v1.4.1 May 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants