question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to use variables in multiline statements (%%sql)

See original GitHub issue

Variables can be used normally with the following syntax: [1] table = 'my_fancy_table' [2] %sql SELECT * FROM $table Unfortunately, when I want to create multiline sql statements (like the following) variables are not possible to use: [1] table = 'my_fancy_table' [2] %%sql SELECT name, firstname FROM $table Outputs: (psycopg2.ProgrammingError) syntax error at or near “$” LINE 1: SELECT * FROM $table;

Does I have to use a different variables syntax for multiline statements or do multiline statements simply not support variables?

Thanks in advance.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
Lucaszwcommented, Jan 17, 2020

Status on this?

This problem has been resolved in 0.3.9

Just use : before the variable

For multiline SQL

name = 'Countess'

%%sql
select description 
from character 
where charname = :name
# output: [(u'mother to Bertram',)]

You can also use : in single line SQL

name = 'Countess'

%sql select description from character where charname = :name
# output: [(u'mother to Bertram',)]

Using : solves a similar but not quite the same things as $. : works for parameters but not for extending the query.

IE

part_of_query = "where sign_in_count > 10"
%sql select count(name) from users $part_of_query

works with $ but not for :

2reactions
Lucaszwcommented, Jan 20, 2020

I made a PR for this, and I think it should resolve your issue @mrjoseph84 .

Try:

db_table = "my_schema.my_table"
%%sql 
CREATE TABLE {db_table} AS
SELECT x, CAST (y AS INT) as duration
FROM abc.def
Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to execute a multiline query read from a file when ...
Scenario: The sql query is stored in file. When I'm just executing the script as a string in powershell the results are displayed...
Read more >
How to pass multiline as input to & — oracle-tech
You can't enter multiple lines of text for a SQL*Plus substitution ... To use a bind variable in SQL*Plus, you use the VARIABLE...
Read more >
Database Engine events and errors - SQL Server
Consult this MSSQL error code list to find explanations for error messages for SQL Server database engine events.
Read more >
MySQL 8.0 Reference Manual :: 5.1.8 Server System Variables
To see the current values used by a running server, use the SHOW VARIABLES statement or the Performance Schema system variable tables. See...
Read more >
Work with multi-statement queries | BigQuery - Google Cloud
Use variables in a multi-statement query ... in DDL and DML queries. For example, you can't use a system variable as a project...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found