From mboxrd@z Thu Jan 1 00:00:00 1970 From: =?utf-8?Q?S=C3=A9bastien_Vauban?= Subject: Re: [Babel] How to pass variables to SQL blocks? Date: Wed, 01 Dec 2010 11:35:31 +0100 Message-ID: <80tyixlr0s.fsf@missioncriticalit.com> References: <80pqtoqfkh.fsf@missioncriticalit.com> <87ipzefcga.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Return-path: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org-mXXj517/zsQ@public.gmane.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org-mXXj517/zsQ@public.gmane.org To: emacs-orgmode-mXXj517/zsQ@public.gmane.org Hi Eric, "Eric Schulte" wrote: > S=C3=A9bastien Vauban writes: > > Babel's sql support did not have any variable handling. I just copied over > the variable support from ob-sqlite to ob-sql, so the following should now > work (notice: my code block is different from yours) > > ** sql variables > #+results: sql-param > | table | valueTable0 | > | column | valueColumn0 | > | type | valueType0 | > | nullability | valueNullability0 | > > I want to apply the values onto the following chunk of code: > > #+srcname: add-column-in-table-0 > #+begin_src sql :var table=3Dsql-param[0,1] :var column=3Dsql-param[1,1] = :var type=3Dsql-param[2,1] :var nullability=3Dsql-param[3,1] > -- add column `@column' (if column does not exist yet) > IF NOT EXISTS (SELECT * > FROM INFORMATION_SCHEMA.COLUMNS > WHERE TABLE_NAME =3D '@table' > AND COLUMN_NAME =3D '@column') > BEGIN > ALTER TABLE $table > ADD $column $type @nullability > END > #+end_src > > results in the following code block expansion (C-c C-v v) > #+begin_src sql > -- add column `@column' (if column does not exist yet) > IF NOT EXISTS (SELECT * > FROM INFORMATION_SCHEMA.COLUMNS > WHERE TABLE_NAME =3D '@table' > AND COLUMN_NAME =3D '@column') > BEGIN > ALTER TABLE valueTable0 > ADD valueColumn0 valueType0 @nullability > END > #+end_src For your information, this was the intended code snippet: #+srcname: add-column-in-table-0 #+begin_src sql :var table=3Dsql-param[0,1] :var column=3Dsql-param[1,1] :v= ar type=3Dsql-param[2,1] :var nullability=3Dsql-param[3,1] :tangle yes -- add column `$column' (if column does not exist yet) IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME =3D '$table' AND COLUMN_NAME =3D '$column') BEGIN ALTER TABLE $table ADD $column $type $nullability END #+end_src (I thought this feature would be implemented using "real" SQL vars, hence m= y @ prefix. But viewing the code shows the replacement is made outside of the S= QL snippet's knowledge.) Tested. Works as expected... Thanks a lot! Maybe the following should be put on some TODO list, even if not urgent? >> * No warning if block does not exist >> >> Please note that inexistent references are ignored, without further noti= ce. >> That's the case for such a block (where I forgot the number suffix): >> >> #+srcname: contains-inexistent-ref >> #+begin_src sql :noweb yes >> <> >> #+end_src Thanks. Best regards, Seb --=20 S=C3=A9bastien Vauban _______________________________________________ Emacs-orgmode mailing list Please use `Reply All' to send replies to the list. Emacs-orgmode-mXXj517/zsQ@public.gmane.org http://lists.gnu.org/mailman/listinfo/emacs-orgmode