Skip to content

Commit 1105044

Browse files
committed
feat(tsql): add alter table rename
1 parent 17f7eaf commit 1105044

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

sqlglot/dialects/tsql.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,3 +1052,14 @@ def _uncast_text(self, expression: exp.Expression, name: str) -> str:
10521052

10531053
def partition_sql(self, expression: exp.Partition) -> str:
10541054
return f"WITH (PARTITIONS({self.expressions(expression, flat=True)}))"
1055+
1056+
def altertable_sql(self, expression: exp.AlterTable) -> str:
1057+
actions = expression.args["actions"]
1058+
if isinstance(actions[0], exp.RenameTable):
1059+
table = self.sql(expression.this)
1060+
target = actions[0].this
1061+
target = self.sql(
1062+
exp.table_(target.this) if isinstance(target, exp.Table) else target
1063+
)
1064+
return f"EXEC sp_rename '{table}', '{target}'"
1065+
return super().altertable_sql(expression)

tests/dialects/test_duckdb.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,7 @@ def test_rename_table(self):
10751075
write={
10761076
"snowflake": "ALTER TABLE db.t1 RENAME TO db.t2",
10771077
"duckdb": "ALTER TABLE db.t1 RENAME TO t2",
1078+
"tsql": "EXEC sp_rename 'db.t1', 't2'",
10781079
},
10791080
)
10801081

0 commit comments

Comments
 (0)