Netezza / SQL列别名将文本与参数值连接起来

时间:2015-08-31 18:26:42

标签: sql-server netezza

在下面的存储过程中,我只想根据参数值创建列别名。它看起来很简单,但我找不到答案。

CREATE OR REPLACE PROCEDURE "SP"(INTEGER, INTEGER)
RETURNS INTEGER
EXECUTE AS OWNER
LANGUAGE NZPLSQL AS
BEGIN_PROC
DECLARE
    whichyear ALIAS FOR $1;

BEGIN
    Select  x as "Some Text" + whichyear  from some table...

 ;
END;
END_PROC;

2 个答案:

答案 0 :(得分:0)

在SQL Server中,您将使用如下动态sql:

DECLARE @sql varchar(max) = 'Select  x as SomeText' + @whichyear + ' from sometable';

EXEC(@sql);

答案 1 :(得分:0)

Tab是正确的,这需要Netezza中的动态SQL。这是一个例子。

CREATE OR REPLACE PROCEDURE "SP"(INTEGER)
RETURNS INTEGER
EXECUTE AS OWNER
LANGUAGE NZPLSQL AS
BEGIN_PROC
DECLARE
    whichyear ALIAS FOR $1;
    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)