Skip to content

Commit

Permalink
Fix(tsql): single quotes in if not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Aug 24, 2023
1 parent b0d82ea commit 12bc916
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions sqlglot/dialects/tsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,10 @@ def create_sql(self, expression: exp.Create) -> str:
exists = expression.args.get("exists")

if exists and kind == "SCHEMA":
schema_name = self.sql(expression, "this")
return f"IF NOT EXISTS (SELECT * FROM information_schema.schemata WHERE SCHEMA_NAME = {schema_name}) EXEC('CREATE SCHEMA {schema_name}')"
schema_name = expression.this
string = self.sql(exp.Literal.string(schema_name.name))
identifier = self.sql(schema_name)
return f"""IF NOT EXISTS (SELECT * FROM information_schema.schemata WHERE SCHEMA_NAME = {string}) EXEC('CREATE SCHEMA {identifier}')"""

return super().create_sql(expression)

Expand Down
2 changes: 1 addition & 1 deletion tests/dialects/test_tsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ def test_types_bin(self):

def test_ddl(self):
self.validate_all(
"IF NOT EXISTS (SELECT * FROM information_schema.schemata WHERE SCHEMA_NAME = foo) EXEC('CREATE SCHEMA foo')",
"IF NOT EXISTS (SELECT * FROM information_schema.schemata WHERE SCHEMA_NAME = 'foo') EXEC('CREATE SCHEMA foo')",
read={
"": "CREATE SCHEMA IF NOT EXISTS foo",
},
Expand Down

0 comments on commit 12bc916

Please sign in to comment.