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

Assignment operator << for multiline sql statements not working #79

Open
Databoy555 opened this issue Mar 21, 2017 · 11 comments
Open

Assignment operator << for multiline sql statements not working #79

Databoy555 opened this issue Mar 21, 2017 · 11 comments

Comments

@Databoy555
Copy link

If I understand the documentation correctly in order for me to store the query output to the variable "works" I can use "<<" as done below:

%%sql works << select top 10 itemid from tblitemdata

But this results in an error message
(pyodbc.ProgrammingError) ('42000', "[42000] [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near '<'. (102) (SQLExecDirectW)") [SQL: u'works <<\nselect top 10 itemid\nfrom tblitemdata']

Single line queries seem to get assigned fine i.e.
works = %sql select top 10 itemid from tblitemdata

returns "Done".

Thanks

@Databoy555 Databoy555 changed the title Assignment operator for multiline sql statements not working Assignment operator << for multiline sql statements not working Mar 21, 2017
@younglee23
Copy link

Hi Databoy555,

I had the same issue and I think I solved it. The code for this feature is in v0.3.9 which released fairly recently. Unfortunately, updating the package from python or anaconda only updates you to v0.3.8. If you don't want to wait for the release to your preferred package source, you may clone/install directly from the git repository.

Hope this helps!

@younglee23
Copy link

BTW, this feature rocks! Thank you @catherinedevlin and @xiaochuanyu for everything. I use this package all the time. And now, with the assignment operator, it just became even more versatile.

@kianho
Copy link

kianho commented Apr 29, 2017

+1

I'm encountering the same problem, I'd love to have this feature fixed.
Agreed with @younglee23, this package is super useful!

@Databoy555
Copy link
Author

Databoy555 commented Apr 29, 2017

@younglee23 . Thanks. I will perform a manual update over the next few days and confirm.

Thanks for the awesome work

@esegal
Copy link

esegal commented Jul 17, 2017

I've installed v0.3.9 and I still get this error. Thanks!

@xiaochuanyu
Copy link
Contributor

@esegal can u show the query u doing where this fails as well as the error output?
It may help me in debugging this.

@esegal
Copy link

esegal commented Jul 18, 2017

cell 1:

# Testing parameters with multiline sql 
%load_ext sql
connection = 'postgresql://username:password@1.2.3.4/erezsegal'
from_clause = 'FROM my_table'
where_clause = '1=1'

cell 2:

%%sql $connection
SELECT * $from_clause WHERE $where_clause LIMIT 10;

The error:

(psycopg2.ProgrammingError) syntax error at or near "$"
LINE 1: SELECT * $from_clause WHERE $where_clause LIMIT 10;
                 ^
 [SQL: 'SELECT * $from_clause WHERE $where_clause LIMIT 10;']

@xiaochuanyu
Copy link
Contributor

@esegal This issue pertains to storing result of a query into a variable, it doesn't apply to what you are trying to do. Expanding variables inside cell magic (%%sql) is not supported right now.
However, expanding variables should work in all line magic not just %sql.
Example:
cell 1:

connection = 'postgresql://username:password@1.2.3.4/erezsegal'
%sql $connection

cell 2:

from_clause = 'FROM my_table'
where_clause = '1=1'
%sql SELECT * $from_clause WHERE $where_clause LIMIT 10;

@xiaochuanyu xiaochuanyu marked this as a duplicate of #80 Jul 18, 2017
@esegal
Copy link

esegal commented Jul 18, 2017

You're right, thanks, I was confused because of the duplicate issue thing with #80.

@Vermabhi16
Copy link

Is this issue resolved now?

@mengxi-ream
Copy link

Is this issue resolved now?

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',)]

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

No branches or pull requests

7 participants