在db2中进行游标声明之前无法从表中分配值

时间:2018-09-10 08:43:02

标签: sql db2

我无法从select语句中将值分配给局部变量。请帮助我。

我想在游标声明之前将值赋给局部变量。我想在游标选择语句中使用该局部变量值。

尝试了以下代码

CREATE OR REPLACE PROCEDURE TESTPROC(IN STARTKEY BIGINT, IN ENDKEY BIGINT)
BEGIN
DECLARE v_var1 VARCHAR(100);
SELECT status into v_var1 from emp where emp_id=1000; -- this will return only one value
DECLARE C_CURSOR CURSOR WITH HOLD FOR SELECT emp_code from company where status=v_var1;
OPEN C_CURSOR;
    FETCH PV INTO C_CURSOR;
    --
    --
    -- My logic 
CLOSE C_CURSOR;
END
@

3 个答案:

答案 0 :(得分:0)

尝试一下

class Person(models.Model):
    name = models.CharField(max_length=128)
    address = models.CharField(max_length=128)
    age = models.DateField()

    class Meta:
        abstract = True

class Member(Person):
    def save(self):
        super().save()
        self.dependents.exclude(address=self.address).update(address=self.address)

class Dependent(Person):
    parent = models.ForeignKey(Member, related_name='dependents')
    extra_field = ...

答案 1 :(得分:0)

只需修改您的查询

SELECT f1.emp_code from company f1 
inner join emp f2 on f1.status=f2.status and f2.emp_id=1000;

答案 2 :(得分:0)

在游标中使用变量之前,您可以尝试使用嵌套的 BEGIN/END 块将值分配给变量。 (这适用于其他 RBDMS)。

CREATE OR REPLACE PROCEDURE TESTPROC(IN STARTKEY BIGINT, IN ENDKEY BIGINT)
BEGIN -- Parent block
 DECLARE v_var1 VARCHAR(100);

 BEGIN -- Child block 1
  SELECT status into v_var1 from emp where emp_id=1000;
 END;

 BEGIN -- Child block 2
  DECLARE C_CURSOR CURSOR WITH HOLD FOR SELECT emp_code FROM company WHERE status = v_var1;
  OPEN C_CURSOR;
    FETCH PV INTO C_CURSOR;
    -- Your logic ...
  CLOSE C_CURSOR;
 END;

END
@