![](/img/trans.png)
[英]TERADATA: how to create a SQL statement with an autoincrement number?
[英]How to decare a variable in Teradata sql and call them in a select statement?
我想聲明兩個變量並在 teradata 中 select 語句的 where 條件下使用 thoes 變量,但我收到了一個錯誤。
Create Procedure Demo()
Begin
Declare REnd Date;
Declare RStart Date;
Set REnd = (select max(CalendarDate) as REnd
from Table1
where CalendarDate = MonthEndDate
and Monthofyear in ('June', 'December')
and CalendarDate < Current_date()
);
Set RStart = (select max(CalendarDate) as RStart
from Table1
where CalendarDate = (CalendarDate - Extract(Day From CalendarDate)+1)
and Monthofyear in ('January', 'July')
and CalendarDate < REnd
);
Call dbc.sysexecsql(('select * from table 2 where reviewdate between' ||REnd|| 'and' ||RStart|| ');');
End;
不需要動態 SQL。
REPLACE PROCEDURE Demo()
DYNAMIC RESULT SETS 1 -- SP will return a result set
BEGIN
Declare REnd Date;
Declare RStart Date;
Set REnd = (select max(CalendarDate) as REnd
from Table1
where CalendarDate = MonthEndDate
and Monthofyear in ('June', 'December')
and CalendarDate < Current_date()
);
Set RStart = (select max(CalendarDate) as RStart
from Table1
where CalendarDate = (CalendarDate - Extract(Day From CalendarDate)+1)
and Monthofyear in ('January', 'July')
and CalendarDate < REnd
);
-- Return result set
BEGIN
DECLARE cur2 CURSOR WITH RETURN ONLY FOR
-- this is your query using parameters
select * from table2
where reviewdate between :RStart and :REnd;
OPEN cur2; -- don't close, otherwise no result set
END;
END;
游標對於處理數據來說是邪惡的,但這不是真正的 cursor,它只是用於返回結果集的冗長的標准 SQL 語法:-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.