在db2 mysql中声明变量 [英] Declaring Variable in db2 mysql

查看:99
本文介绍了在db2 mysql中声明变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  BEGIN 
声明@STDATE DATE DEFAULT‘2015-08-19’;
声明@ENDATE DEFAULT'2016-08-19';
选择*从MYTABLE
中@STDATE和@ENDATE之间的日期
END

我正在尝试使用sql在db2上执行查询,以允许在一段时间内检索数据并计算结果,该时间由Start Date和End Date定义。



当我尝试执行此语句时,出现意外的令牌错误

 声明@stdate也被抛出,也尝试使用
创建变量sdate char(10)DEFAULT'2015-08-19';
创建变量edate char(10)DEFAULT'2016-08-19';
选择*从MYTABLE
中@SDATE和@EDATE之间的日期
END

我根据戈登的建议进行了更改,我执行了查询



BEGIN
DECLARE v_stdate日期;
DECLARE v_endate日期;
SET v_date = date('2016-01-01');
SET v_endate =日期('2016-04-03');
SELECT * FROM MYTABLE
在v_stdate和v_endate
END
之间的日期,意外令牌的错误已删除
,但随后我又遇到了另一个错误
[在此处输入图片描述] [它说出了意外的 as],然后我注释了
行,它抛出了另一个sql错误,表示在表中的select *中出现了意外的错误'from'



请帮助

解决方案

如果您使用的是小岛(旧版AS400),则可能性为

 -创建变量
创建或替换变量yourlib.sdate date默认'2015-08-19';
创建或替换变量yourlib.edate日期默认值'2016-08-19';

-您使用带变量的查询
SELECT * FROM yourlib.yourtable
yourdate在yourlib.sdate和yourlib.edate之间;

-自由变量
降落变量yourlib.sdate;
降落变量yourlib.edate;

最好在qtemp库中创建变量,除非要将此变量保留为全局变量其他脚本。


BEGIN
DECLARE @STDATE DATE DEFAULT '2015-08-19';
DECLARE @ENDATE DATE DEFAULT '2016-08-19';
SELECT * FROM MYTABLE
WHERE DATE BETWEEN @STDATE AND @ENDATE
END

I'm trying to execute a query on db2 using sql to allow retrieval of data and calculation of results for a period of time, defined by Start Date and End Date. The Start Date and the End Date should be made into variables, or parameters.

When I'm trying to execute this statements an error of unexpected token

declare @stdate is thrown also tried using
create variable sdate char(10) DEFAULT '2015-08-19';
create variable edate char(10)DEFAULT '2016-08-19';
SELECT * FROM MYTABLE
WHERE DATE BETWEEN @SDATE AND @EDATE
END

i made changes according to the suggestion of gordon i executed the query

BEGIN DECLARE v_stdate date; DECLARE v_endate date; SET v_date=date('2016-01-01'); SET v_endate= date ('2016-04-03'); SELECT * FROM MYTABLE WHERE DATE BETWEEN v_stdate AND v_endate END the error of unexpected token is removed but then i am getting another error [enter image description here][it says unexpected 'as'] then i commented the line its throwing another sql error saying unexpected error 'from' at the select * from my table

please help

解决方案

Possibility if you are on iseries (old AS400) is

--creation des variables
CREATE OR REPLACE VARIABLE yourlib.sdate date DEFAULT '2015-08-19';
CREATE OR REPLACE VARIABLE yourlib.edate date DEFAULT '2016-08-19';

-- your query with variables used
SELECT * FROM yourlib.yourtable
WHERE yourdate  BETWEEN yourlib.sdate AND  yourlib.edate;

-- free variable
drop VARIABLE yourlib.sdate ;
drop VARIABLE yourlib.edate ;

It is best to create your variables in the qtemp Library, except if you want keep this variables as global for other scripts.

这篇关于在db2 mysql中声明变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆