dbcli / mssql-cli

A command-line client for SQL Server with auto-completion and syntax highlighting
BSD 3-Clause "New" or "Revised" License
1.35k stars 192 forks source link

variable declaration with cte #447

Open jtougas opened 4 years ago

jtougas commented 4 years ago

I'm having trouble declaring a variable and a cte in one statement. This is something that works with sqlcmd.

Statement

STMT=$(cat <<'EOF'
DECLARE @bar INT = 1
;with foo as (select @bar as foo)
select * from foo
GO
EOF
)

sqlcmd

$ sqlcmd -S sqlserver.corp.com -U username -P password -Q "${STMT}"
foo        
-----------
          1

(1 rows affected)

mssql-cli

$ mssql-cli --username username --password password --server sqlserver.corp.com --query "${STMT}"
Commands completed successfully.
Msg 137, Level 15, State 2, Line 2
Must declare the scalar variable "@bar".
ellbosch commented 4 years ago

I've repro'd the bug on my end. mssql-cli is able to print the scalar value as long as it's not referenced in a CTE.

Thank you @jtougas for letting us know about this bug!