How do i declare and increment local variables in db2?

风格不统一 提交于 2019-12-12 03:45:52

问题


I want to show row number for each of result set row, I have this query in mySQL

SELECT @rownum := @rownum + 1 row, e.* FROM Employee e, (SELECT @rownum := 0) r

Here @rownum is local variable and would increment its value for each result row. How do i write this query in db2 ( ibm's dashdb ) ?


回答1:


If you're just looking to number the output rows, you can use the row_number() function:

select 
    row_number() over() as row, 
    e.* 
from 
    Employee e



回答2:


If you are looking to set a variable and set a value:

db2 -td@ "begin declare test integer; set test = 1; end @"

Or

begin
  declare test integer;
  set test = 1;
  set test = test + 1;
end @


来源:https://stackoverflow.com/questions/42665529/how-do-i-declare-and-increment-local-variables-in-db2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!