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

bug(mssql): incorrect syntax for creating quoted temporary table #3399

Closed
gforsyth opened this issue May 2, 2024 · 2 comments · Fixed by #3401
Closed

bug(mssql): incorrect syntax for creating quoted temporary table #3399

gforsyth opened this issue May 2, 2024 · 2 comments · Fixed by #3401
Assignees

Comments

@gforsyth
Copy link
Contributor

gforsyth commented May 2, 2024

[ins] In [1]: import sqlglot as sg

[ins] In [2]: sg.__version__
Out[2]: '23.12.3.dev11'

[ins] In [3]: exp = sg.parse_one(
         ...:     "CREATE TEMPORARY TABLE 'temptest' (name VARCHAR);", dialect="duckdb"
         ...: )

[ins] In [4]: exp
Out[4]: 
Create(
  this=Schema(
    this=Table(
      this=Identifier(this=temptest, quoted=True)),
    expressions=[
      ColumnDef(
        this=Identifier(this=name, quoted=False),
        kind=DataType(this=Type.VARCHAR, nested=False))]),
  kind=TABLE,
  properties=Properties(
    expressions=[
      TemporaryProperty()]))

[ins] In [5]: exp.sql(dialect="tsql")
Out[5]: 'CREATE TABLE #[temptest] (name VARCHAR)'

#[temptest] is invalid syntax. I am not sure of how to use the # temp indicator with a quoted identifier -- it IS possible to create a temporary table that requires quoting but the only way I have figured out how to do it is by specifying the catalog and database explicitly, e.g.

CREATE TABLE tempdb.dbo.[quoted@identifer] ([name] VARCHAR(max))

Official Documentation
https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql?view=sql-server-ver16#syntax-options
https://learn.microsoft.com/en-us/sql/relational-databases/databases/database-identifiers?view=sql-server-ver16#classes-of-identifiers

xref: ibis-project/ibis#9095

@georgesittas
Copy link
Collaborator

This seems like a T-SQL syntax edge case, i.e. what you said here:

I am not sure of how to use the # temp indicator with a quoted identifier

I also couldn't find any way to make this work. I'll fix it up, thanks for the report.

@georgesittas georgesittas self-assigned this May 2, 2024
@georgesittas
Copy link
Collaborator

After some investigation, it turns out that quoted temp. table identifiers are possible, you just need to add the # inside of the quotes, like [#foo bar].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants