db2设置多个varchar变量

时间:2014-04-28 20:58:14

标签: sql db2

如何在多个选择标记中将多个varchar变量设置为后者使用?

documentation有一个整数示例

SET (NEW_VAR.SALARY, NEW_VAR.COMM) = (50000, 8000);  

我无法弄清楚如何为varchar做同样的事情

SET (NEW_VAR.var, NEW_VAR.var1) = ('%hello%','%world%');
SELECT 
  *
FROM 
 fooSchema.barTable AS b
WHERE 
  b.first_name like new_var.var and
  b.last_name like new_var.var1;

1 个答案:

答案 0 :(得分:1)

正如Mustaccio所说,你可以在复合语句,触发器或例程之间使用变量。对于您的示例(触发器):

CREATE TRIGGER trigger1
  AFTER insert ON mytable
  REFERENCING NEW AS NEW_VAR
  FOR EACH ROW
BEGIN

DECLARE val INT;
SET (NEW_VAR.var, NEW_VAR.var1) = ('%hello%','%world%');
SELECT col INTO val
  FROM fooSchema.barTable AS b
  WHERE b.first_name like new_var.var
  AND b.last_name like new_var.var1;

END @

或在复合声明中

BEGIN
 DECLARE var varchar(32);
 DECLARE var1 varchar(32);
 DECLARE val INT;
  SET (NEW_VAR.var, NEW_VAR.var1) = ('%hello%','%world%');
 SELECT col INTO val
   FROM fooSchema.barTable AS b
   WHERE b.first_name like new_var.var
   AND b.last_name like new_var.var1;
END @

请注意,我为select的输出使用了一个额外的变量。像这样的查询应该只返回一行。否则,您应该使用光标。

请记住,您应该使用以下选项执行这些脚本:

db2 -td@ -f yourFilename.sql