Forum

General discussion on using the Kognitio Analytical Platform.
Contributor
Offline
User avatar
Posts: 27
Joined: Thu Jun 26, 2014 3:23 pm

Is it possible to put a variable in quotes?

by William » Tue Jul 21, 2015 1:15 pm

Hi,

I'm using a Kognitio variable to hold a table name in my script and would like to be able to put the table name in a column of my output table.

So I'm doing something like:

Code: Select all

set var tablename = my_schema.my_table

select '$tablename' as input_table,
         min(x) as min_x,
         max(x) as max_x,
         count(*) as n
from $tablename
The problem is that my output puts $tablename in the input_table field and I would like it to be my_schema.my_table.
Other languages I've used have a specific way of adding quotation marks around variables.
Does Kognitio?

Thanks,

Will
Reply with quote Top
Contributor
Offline
User avatar
Posts: 386
Joined: Thu May 23, 2013 4:48 pm

Re: Is it possible to put a variable in quotes?

by markc » Tue Jul 21, 2015 2:23 pm

Assuming this is scripting with wxsubmit, there's a better way of setting variables which uses a bash-like syntax. With this you can create a variable $sq which contains only a single quote. Then you can put references to $sq either side of $tablename, and wxsubmit will substitute $tablename because it doesn't look like it's in quotes.

Here's how it looks using a test table:

>set var tablename mytable;
>sq='''';
>select $sq$tablename$sq as input_table, min(x) as min_x, max(x) as max_x, count(*) as n from $tablename;
INPUT_T| MIN_X| MAX_X| N
mytable| 1| 5| 5
Query 1 1 row ---- 0:00.1 0:00.1 0:00.1

Note that sq='''' contains four single quote characters. The outer two are to enclose a string, and the middle two represent an escaped single quote in a single-quoted string, which makes $sq contain one single quote.
Reply with quote Top
Contributor
Offline
Posts: 184
Joined: Wed May 29, 2013 2:10 pm
Location: Bracknell

Re: Is it possible to put a variable in quotes?

by MikeAtkinson » Tue Jul 21, 2015 2:44 pm

In Kog scripts, you can do the following:

Code: Select all

tablename = "my_schema.my_table"
quote = "'"
select $quote$tablename$quote as input_table,
         min(x) as min_x,
         max(x) as max_x,
         count(*) as n
from $tablename;
Reply with quote Top
Contributor
Offline
Posts: 184
Joined: Wed May 29, 2013 2:10 pm
Location: Bracknell

Re: Is it possible to put a variable in quotes?

by MikeAtkinson » Tue Jul 21, 2015 2:51 pm

In SQL scripts within Console the best that can be achieved with a released build is:

Code: Select all

set var tablename  my_schema.my_table;

select '''';
set var quote  $Col1;

select $quote $tablename $quote as input_table,
         min(x) as min_x,
         max(x) as max_x,
         count(*) as n
from $tablename;
This is not ideal because a separate query is performed as the way of setting the $quote variable. The input_table is set to be ' my_schema.my_table ' (note the extra spaces).
Reply with quote Top
Contributor
Offline
User avatar
Posts: 27
Joined: Thu Jun 26, 2014 3:23 pm

Re: Is it possible to put a variable in quotes?

by William » Tue Jul 21, 2015 3:53 pm

Very creative solutions. Thanks a lot.

I've used this and concatenated the strings using ${quote}${demandtable}${quote}.

Thanks,

Will
Reply with quote Top
Contributor
Offline
Posts: 184
Joined: Wed May 29, 2013 2:10 pm
Location: Bracknell

Re: Is it possible to put a variable in quotes?

by MikeAtkinson » Tue Jul 21, 2015 4:13 pm

The next version of Console will treat '''' as special so that the following may be used:

Code: Select all

set var tablename  my_schema.my_table;
set var quote  '''';

select ${quote}${tablename}${quote} as input_table,
         min(x) as min_x,
         max(x) as max_x,
         count(*) as n
from $tablename;
Reply with quote Top
Single Poster
Offline
User avatar
Posts: 1
Joined: Fri Aug 14, 2015 8:16 am

Re: Is it possible to put a variable in quotes?

by gondar5678 » Fri Aug 14, 2015 8:17 am

Thank for great question and great answer
Reply with quote Top

Who is online

Users browsing this forum: No registered users and 1 guest

cron