Netezza/SQL 列别名将文本与参数值连接起来
Netezza/SQL Column Alias concatenate text with parameter value
在下面的存储过程中,我只想创建一个基于参数值的列别名。看起来很简单,但我找不到答案。
CREATE OR REPLACE PROCEDURE "SP"(INTEGER, INTEGER)
RETURNS INTEGER
EXECUTE AS OWNER
LANGUAGE NZPLSQL AS
BEGIN_PROC
DECLARE
whichyear ALIAS FOR ;
BEGIN
Select x as "Some Text" + whichyear from some table...
;
END;
END_PROC;
在 SQL 服务器中,您可以像这样使用动态 sql:
DECLARE @sql varchar(max) = 'Select x as SomeText' + @whichyear + ' from sometable';
EXEC(@sql);
Tab 是正确的,这需要 Netezza 中的动态 SQL。这是一个例子。
CREATE OR REPLACE PROCEDURE "SP"(INTEGER)
RETURNS INTEGER
EXECUTE AS OWNER
LANGUAGE NZPLSQL AS
BEGIN_PROC
DECLARE
whichyear ALIAS FOR ;
vSQL varchar(30000);
BEGIN
drop table the_results if exists;
vSQL := 'CREATE TABLE THE_RESULTS AS SELECT COL1 "SOME TEXT ' || whichyear || '" FROM TABLE_A;';
execute immediate vSQL;
END;
END_PROC;
这是输出。
TESTDB.ADMIN(ADMIN)=> exec SP(5);
SP
----
(1 row)
TESTDB.ADMIN(ADMIN)=> select * from the_results;
SOME TEXT 5
-------------
2
3
1
(3 rows)
在下面的存储过程中,我只想创建一个基于参数值的列别名。看起来很简单,但我找不到答案。
CREATE OR REPLACE PROCEDURE "SP"(INTEGER, INTEGER)
RETURNS INTEGER
EXECUTE AS OWNER
LANGUAGE NZPLSQL AS
BEGIN_PROC
DECLARE
whichyear ALIAS FOR ;
BEGIN
Select x as "Some Text" + whichyear from some table...
;
END;
END_PROC;
在 SQL 服务器中,您可以像这样使用动态 sql:
DECLARE @sql varchar(max) = 'Select x as SomeText' + @whichyear + ' from sometable';
EXEC(@sql);
Tab 是正确的,这需要 Netezza 中的动态 SQL。这是一个例子。
CREATE OR REPLACE PROCEDURE "SP"(INTEGER)
RETURNS INTEGER
EXECUTE AS OWNER
LANGUAGE NZPLSQL AS
BEGIN_PROC
DECLARE
whichyear ALIAS FOR ;
vSQL varchar(30000);
BEGIN
drop table the_results if exists;
vSQL := 'CREATE TABLE THE_RESULTS AS SELECT COL1 "SOME TEXT ' || whichyear || '" FROM TABLE_A;';
execute immediate vSQL;
END;
END_PROC;
这是输出。
TESTDB.ADMIN(ADMIN)=> exec SP(5);
SP
----
(1 row)
TESTDB.ADMIN(ADMIN)=> select * from the_results;
SOME TEXT 5
-------------
2
3
1
(3 rows)