1

Trying to pass multiline argument to Org code block. If make direct call of block then it's Ok. But when call it through noweb ref then Error raised

#+NAME: pr
#+BEGIN_SRC elisp :results value :var n="a\nb\nc"
"%p" n
#+END_SRC

#+RESULTS: pr
: a
: b
: c


#+BEGIN_SRC elisp :noweb yes :results value
<<pr("a\nb\nc")>>
#+END_SRC

eval: Symbol’s value as variable is void: a

I need this to be able to use pr block with another noweb call already in SQL like this

insert into table
select @SPID, <<pr("1\n2\n3\n4")>>

proposing to get string executed in sqlcmd

select @SPID, 1
select @SPID, 2
select @SPID, 3
select @SPID, 4
2

I understand your confusion.

The good news is that your code is already passing new lines correctly into your noweb call.

The reason you're seeing the eval: Symbol’s value as variable is void: a error is because of the generated elisp code.

If you execute the following code you'll see the same error message:

#+BEGIN_src elisp
a
b
c
#+END_src 

A convenient way to preview how code will be rendered by noweb is to put cursor inside you block and press C-c C-v v.

Another way to preview code is to render the code in multiple steps:

#+NAME: sql-stmt
#+BEGIN_SRC sql :noweb yes :eval never
  insert into table
  select @SPID, <<pr("1\n2\n3\n4")>>
#+END_SRC   

#+BEGIN_SRC css :noweb yes :wrap "src sql"
  <<sql-stmt>>
#+END_SRC

#+RESULTS:
#+BEGIN_src sql
insert into table
select @SPID, 1
select @SPID, 2
select @SPID, 3
select @SPID, 4
#+END_src 

Note You should be cautious when generating SQL statements this way because it is very easy to accidentally introduce a SQL injection vulnerability into your source code. Be sure to verify your data and sql statements before executing against your database.


This code was tested using:
GNU Emacs 25.3.1 (x86_64-apple-darwin16.7.0, NS appkit-1504.83 Version 10.12.6 (Build 16G29))
org-mode version: 9.1.2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.