sql - SQL 获取用户输入并进一步使用它
问题描述
我有将新记录插入 table 的任务EMPLOYEES
。我知道如何通过要求用户输入一个值来做到这一点,例如:
INSERT INTO EMPLOYEES (first_name, last_name, email, hire_date, job_id)
VALUES ('&first_name', '&last_name', '&email' ,'&hire_date', 'SA_REP' );
但是,我不想询问用户,email
而是通过将输入的第一个字母与将其数据添加到表中的人的first_name
连接来自动插入它。last_name
为了做到这一点,我想我必须暂时存储插入的值,或者至少获得一些对first_name
and的引用last_name
。我尝试在网上搜索,但真的一无所获。你能为我提供这个任务的最简单的解决方案吗?我正在使用 Oracle SQL Developer。
解决方案
您可以将其包装在 PL/SQL 块中,以使用具有正确数据类型的适当变量。这也将确保以date
正确的格式正确输入变量的值。
DECLARE
v_first_name employees.first_name%type := '&first_name';
v_last_name employees.last_name%type := '&last_name';
v_hire_date employees.hire_date%type := TO_DATE('&hire_date_YYYYMMDD','YYYYMMDD');
BEGIN
INSERT INTO EMPLOYEES (first_name, last_name, email, hire_date, job_id)
VALUES (v_first_name, v_last_name,
substr(v_first_name,1,1)||'.'||v_last_name , v_hire_date, 'SA_REP' );
--first letter of the first_name with last name
END;
/
结果
Enter value for first_name: John
Enter value for last_name: Doe
Enter value for hire_date_YYYYMMDD: 20190521
..
..
PL/SQL procedure successfully completed.
推荐阅读
- powershell - Powershell 从多台远程计算机、所有驱动器中查找多个文件
- ajax - 从源“http://localhost:8080”访问“http://localhost:8092/URL”的 XMLHttpRequest 已被 CORS 策略阻止
- ruby-on-rails - 如何从 shell 获取passenger_app_env 值?
- android - 在 Flutter 中使用本地用户选择的 TTF 文件
- javascript - 如何将我的代码转换为正确的 jsx 格式?
- matlab - MATLAB-注水算法-无限循环
- javascript - 什么更节省资源:lodash deepEqual 或每次都创建一个新对象?
- spring-boot - TestRestTemplate vs WebTestClient vs RestAssured:什么是 Spring Boot Rest API 集成测试的最佳方法
- java - 比较2队实力,找出获胜概率
- python - 从 Pandas DataFrame 中的 groupby 解包列表 - Python