2017-04-02 6 views
1

はpsql(9.6.1、9.5.5サーバー)変数の使用

従業員

 Column  |   Type    |       Modifiers       | Storage | Stats target | Description 
----------------+-----------------------------+-----------------------------------------------------------------+----------+--------------+---- --------- 
employee_id | integer      | not null default nextval('employees_employee_id_seq'::regclass) | plain |    | 
first_name  | character varying(20)  |                 | extended |    | 
last_name  | character varying(25)  | not null              | extended |    | 
email   | character varying(25)  | not null              | extended |    | 
phone_number | character varying(20)  |                 | extended |    | 
hire_date  | timestamp without time zone | not null              | plain |    | 
job_id   | character varying(10)  | not null              | extended |    | 
salary   | numeric(8,2)    |                 | main  |    | 
commission_pct | numeric(2,2)    |                 | main  |    | 
manager_id  | integer      |                 | plain |    | 
department_id | integer 

は、自己の教育のために私は、変数を使用したいと思います。

このリクエストの結果は私に合うでしょう:

hr=> select last_name, char_length(last_name) as Length from employees where substring(last_name from 1 for 1) = 'H' order by last_name; 
last_name | length 
-----------+-------- 
Hartstein |  9 
Higgins |  7 
Hunold |  6 
(3 rows) 

しかし、自己教育のために私は、変数を使用したい:

\set chosen_letter 'H' 
hr=> select last_name, char_length(last_name) as Length from employees where substring(last_name from 1 for 1) = :chosen_letter order by last_name; 
ERROR: column "h" does not exist 
LINE 1: ...ployees where substring(last_name from 1 for 1) = H order by... 
                  ^

これらのアポストロフィは、すべてを台無しにするようです。そして、私はこの問題に対処できません。

変数を使用して上記のように結果を取得する方法を理解してもらえますか?

答えて

2

使用してみてください:

\set chosen_letter '''H'''