IIB:将局部变量传递给 ESQL select 语句
IIB: Passing local variable to ESQL select statement
我需要根据 IIB/WMB 中输入 xml 中给出的作业代码从两个表中检索数据。
SQL 查询
SELECT D.DEPTNO,D.DNAME,D.LOC,E.EMPNO,E.ENAME,E.JOB,E.SAL FROM DEPT D JOIN EMP E on E.JOB ='MANAGER' OR E.JOB = 'CLERK'
ESQL 查询
从输入中获取 JOB 详细信息 xml 并将其分配给局部变量。
SET type1 = InputRoot.XMLNSC.Employee.Type[1];
SET type2 = InputRoot.XMLNSC.Employee.Type[2];
现在我需要将上述变量传递给我的 ESQL 查询。
SET Outputroot.XMLNSC.List[] = PASSTHRU('SELECT D.DEPTNO,D.DNAME,D.LOC,E.EMPNO,E.ENAME,E.JOB,E.SAL FROM Database.EMP AS E,Database.DEPT AS D where E.JOB =?' VALUES('type1','type2')) ;
但是上面的查询不起作用。
这个查询有两个问题:
首先,您传递了两个参数,但查询中只有一个问号 (?)。
其次,您传递的是 'type1' 之类的字符串值,正确的是传递变量:
SET Outputroot.XMLNSC.List[] = PASSTHRU('SELECT D.DEPTNO,D.DNAME,D.LOC,E.EMPNO,E.ENAME,E.JOB,E.SAL FROM Database.EMP AS E,Database.DEPT AS D where E.JOB =?' VALUES(type1)) ;
在您的 PASSTHRU 语句后添加 To Database.DBName
,其中 DBName 是您的数据库名称
像这样:
SET Outputroot.XMLNSC.List[] = PASSTHRU('SELECT D.DEPTNO,D.DNAME,D.LOC,E.EMPNO,E.ENAME,E.JOB,E.SAL FROM Database.EMP AS E,Database.DEPT AS D where E.JOB =?' VALUES(type1)) To Database.DBName ;
解决方法如下:
DECLARE designation char;
DECLARE designation1 char;
DECLARE Query char;
FOR source AS InputRoot.XMLNSC.Employees.Designation[] DO
IF(source = '') THEN
SET designation = 'Empty';
ELSE
SET designation = designation1 ||''''||UPPER(source)||'''';
SET designation1 = ''''||UPPER(source)||''''||','||designation1 ;
END IF;
SET Query = '(SELECT D.DEPTNO,D.DNAME,D.LOC,E.EMPNO,E.ENAME,E.JOB,E.SAL FROM EMP AS E JOIN DEPT AS D on D.DEPTNO = E.DEPTNO where E.JOB in ('||designation||'))';
SET Outputroot.XMLNSC.List[] = PASSTHRU(Query);
直通呼叫应该是
SET Outputroot.XMLNSC.List[] = PASSTHRU('SELECT D.DEPTNO,D.DNAME,D.LOC,E.EMPNO,E.ENAME,E.JOB,E.SAL FROM Database.EMP AS E,Database.DEPT AS D where E.JOB in (? , ?)', type1,type2) ;
或
SET Outputroot.XMLNSC.List[] = PASSTHRU('SELECT D.DEPTNO,D.DNAME,D.LOC,E.EMPNO,E.ENAME,E.JOB,E.SAL FROM Database.EMP AS E,Database.DEPT AS D where E.JOB=? or E.JOB=?)', type1,type2) ;
我需要根据 IIB/WMB 中输入 xml 中给出的作业代码从两个表中检索数据。
SQL 查询
SELECT D.DEPTNO,D.DNAME,D.LOC,E.EMPNO,E.ENAME,E.JOB,E.SAL FROM DEPT D JOIN EMP E on E.JOB ='MANAGER' OR E.JOB = 'CLERK'
ESQL 查询
从输入中获取 JOB 详细信息 xml 并将其分配给局部变量。
SET type1 = InputRoot.XMLNSC.Employee.Type[1];
SET type2 = InputRoot.XMLNSC.Employee.Type[2];
现在我需要将上述变量传递给我的 ESQL 查询。
SET Outputroot.XMLNSC.List[] = PASSTHRU('SELECT D.DEPTNO,D.DNAME,D.LOC,E.EMPNO,E.ENAME,E.JOB,E.SAL FROM Database.EMP AS E,Database.DEPT AS D where E.JOB =?' VALUES('type1','type2')) ;
但是上面的查询不起作用。
这个查询有两个问题:
首先,您传递了两个参数,但查询中只有一个问号 (?)。
其次,您传递的是 'type1' 之类的字符串值,正确的是传递变量:
SET Outputroot.XMLNSC.List[] = PASSTHRU('SELECT D.DEPTNO,D.DNAME,D.LOC,E.EMPNO,E.ENAME,E.JOB,E.SAL FROM Database.EMP AS E,Database.DEPT AS D where E.JOB =?' VALUES(type1)) ;
在您的 PASSTHRU 语句后添加 To Database.DBName
,其中 DBName 是您的数据库名称
像这样:
SET Outputroot.XMLNSC.List[] = PASSTHRU('SELECT D.DEPTNO,D.DNAME,D.LOC,E.EMPNO,E.ENAME,E.JOB,E.SAL FROM Database.EMP AS E,Database.DEPT AS D where E.JOB =?' VALUES(type1)) To Database.DBName ;
解决方法如下:
DECLARE designation char;
DECLARE designation1 char;
DECLARE Query char;
FOR source AS InputRoot.XMLNSC.Employees.Designation[] DO
IF(source = '') THEN
SET designation = 'Empty';
ELSE
SET designation = designation1 ||''''||UPPER(source)||'''';
SET designation1 = ''''||UPPER(source)||''''||','||designation1 ;
END IF;
SET Query = '(SELECT D.DEPTNO,D.DNAME,D.LOC,E.EMPNO,E.ENAME,E.JOB,E.SAL FROM EMP AS E JOIN DEPT AS D on D.DEPTNO = E.DEPTNO where E.JOB in ('||designation||'))';
SET Outputroot.XMLNSC.List[] = PASSTHRU(Query);
直通呼叫应该是
SET Outputroot.XMLNSC.List[] = PASSTHRU('SELECT D.DEPTNO,D.DNAME,D.LOC,E.EMPNO,E.ENAME,E.JOB,E.SAL FROM Database.EMP AS E,Database.DEPT AS D where E.JOB in (? , ?)', type1,type2) ;
或
SET Outputroot.XMLNSC.List[] = PASSTHRU('SELECT D.DEPTNO,D.DNAME,D.LOC,E.EMPNO,E.ENAME,E.JOB,E.SAL FROM Database.EMP AS E,Database.DEPT AS D where E.JOB=? or E.JOB=?)', type1,type2) ;