CAn you use a vsql variable in a where statement which expects a varchar

Moderator: NorbertKrupa

Post Reply
VaughanR
Newbie
Newbie
Posts: 3
Joined: Wed Mar 18, 2015 4:46 am

CAn you use a vsql variable in a where statement which expects a varchar

Post by VaughanR » Wed Mar 18, 2015 4:53 am

Hi,

I have defined a vsql variable:
\set MPcode 'LIPI3';

now I would like to use it like so:
select MasterProductID from sandbox.DimProductMaster where MasterProductCode = :MPcode;
where MasterProductCode is a varchar but I get the error
ERROR 2624: Column "LIPI3" does not exist

How do I specify the variable to get this match to work?

VaughanR
Newbie
Newbie
Posts: 3
Joined: Wed Mar 18, 2015 4:46 am

Re: CAn you use a vsql variable in a where statement which expects a varchar

Post by VaughanR » Wed Mar 18, 2015 5:19 am

Well a little more playing around and I got this to work:
sandbox=> \set MPcode to_char('LIPI3');
sandbox=> \echo :MPcode
to_char('LIPI3');
sandbox=> select MasterProductID from sandbox.DimProductMaster where MasterProductCode = :MPcode;
MasterProductID
-----------------
4867
(1 row)

User avatar
JimKnicely
Site Admin
Site Admin
Posts: 1825
Joined: Sat Jan 21, 2012 4:58 am
Contact:

Re: CAn you use a vsql variable in a where statement which expects a varchar

Post by JimKnicely » Wed Mar 18, 2015 8:10 pm

You can also do it like this:

Code: Select all

dbadmin=> select * from t1;
 c1 |  c2
----+------
  1 | Jim
  2 | Jane
(2 rows)

dbadmin=> \set v '''Jane'''

dbadmin=> select * from t1 where c2 = :v;
 c1 |  c2
----+------
  2 | Jane
(1 row)
Last edited by NorbertKrupa on Wed Mar 18, 2015 9:48 pm, edited 1 time in total.
Reason: fixed typo (missing the word "like")
Jim Knicely

Image

Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.

Post Reply

Return to “Vertica SQL”