HANA + Is it possible to create a local temp table with dynamic name (name including current time)? - hana

As we do in SQL, is it possible to pass variable on create statement as a table name in HANA?
I'm trying the below code but its throwing an error
CREATE PROCEDURE temp_table()
AS
table_name nvarchar(255);
BEGIN
table_name := '#TMP_TABLE'+ CURRENT_TIMESTAMP;
CREATE LOCAL TEMPORARY COLUMN TABLE :table_name(
SCENARIO varchar(64) NULL,
SCENARIO_CY varchar(64) NULL,
MONTH_NO numeric(4, 0) NULL
);
DROP TABLE :table_name;
END;
Please help me to find out a solution.

What you are looking for is Dynamic SQL. You will need to pass your SQL statement as a string to function EXEC or EXECUTE IMMEDIATE. The timestamp dependent name can be assembled by string concatenation.
In your code above your are using '+' to do string concatenation:
table_name := '#TMP_TABLE'+ CURRENT_TIMESTAMP;
IMHO this does not work on HANA. You should use a double pipe '||' instead:
table_name := '#TMP_TABLE' || CURRENT_TIMESTAMP;

Related

Oracle stored procedure with parameter

I am fairly new to Oracle SQL. I am trying to create a stored procedure which takes 2 parameters. I am getting errors when I save below. Any idea ?
CREATE OR REPLACE PROCEDURE SWAP_VIEWS
(
SchemaName NVARCHAR,
TableName NVARCHAR
) AS
BEGIN
DECLARE SQLstring NVARCHAR :=
'ALTER VIEW AS POL.V_' + TableName + ' as SELECT * FROM ' + SchemaName + '.' + TableName
EXEC SQLstring;
END SWAP_VIEWS ;
Here the version corrected
CREATE OR REPLACE PROCEDURE SWAP_VIEWS
(
SchemaName NVARCHAR,
TableName NVARCHAR
) AS
SQLstring varchar2(4000);
BEGIN
SQLstring := 'CREATE OR REPLACE FORCE VIEW POL.V_' || TableName || ' as SELECT * FROM ' || SchemaName || '.' || TableName ' ;
EXECUTE IMMEDIATE SQLstring;
END SWAP_VIEWS ;
/
The concatenation character is |
The variable SQLString must be declared
The way to execute a variable is EXECUTE IMMEDIATE
You can use CREATE OR REPLACE FORCE VIEW instead of ALTER VIEW
You seem to be mixing SQL Server and Oracle syntax here:
Oracle uses standard operator || for string concatenation, not +
Oracle wants EXECUTE IMMEDIATE instead of EXEC
you need CREATE OR REPLACE VIEW
NVARCHAR2 should be preferred to NVARCHAR
Note that you don't need an intermediate variable assignment, you can concatenate the query string and execute it at once.
Consider:
CREATE OR REPLACE PROCEDURE SWAP_VIEWS (
pSchemaName NVARCHAR2,
pTableName NVARCHAR2
) AS
BEGIN
EXECUTE IMMEDIATE 'CREATE OR REPLACE VIEW AS POL.V_'
|| pTableName + ' as SELECT * FROM '
|| pSchemaName || '.' || pTableName;
END;
/
I have taken your procedure and modified like below, see if it works. Additionally i want to say, you can explicitly do exception handling in the procedure to capture any error during the execution and also creating view with '*' has some disadvantages. Please chek
CREATE OR REPLACE PROCEDURE SWAP_VIEWS
(
p_schema_name IN NVARCHAR2,
p_table_name IN NVARCHAR2
)
IS
lo_sql_string NVARCHAR2(4000);
lo_view_name NVARCHAR2(30);
lo_table_name NVARCHAR2(30);
BEGIN
-- I just prefer local variables for convenience to avoid more complex string in actual one and also for debugging purpose
lo_view_name := 'POL.V_'||p_table_name;
lo_table_name := p_schema_name||'.'||p_table_name;
lo_sql_string:= 'create or replace view '||lo_view_name||
' as '||
'select * from '||lo_table_name;
EXECUTE IMMEDIATE lo_sql_string;
END SWAP_VIEWS;
/

Using NEWUID() function in HANA database

I'm trying to use NEWUID() function to generate unique digits, but can't execute code because of probable errors in syntax.
How can I correct this one?
Thanks.
CREATE PROCEDURE GENERATINGN (
IN
p_docentryt NVARCHAR(50)
)
LANGUAGE SQLSCRIPT
AS
BEGIN
SELECT cast("ItemCode" || '-' || NEWUID() as nvarchar(50)) AS "UNIQUEIDENTIFIER"
FROM "OITM"
WHERE "T0"."DocEntry" = p_docentryt;
END;
It should be generate digits like this:
IT1225-513613161514161
The code looks ok to me. Wat error message do you have? Can you tell a bit more about the HANA version you are using?
I executed the following similar SQL code and it worked:
CREATE PROCEDURE GENERATINGN (IN p_docentryt NVARCHAR(50))
LANGUAGE SQLSCRIPT
AS
BEGIN
SELECT cast("ItemCode" || '-' || NEWUID() as nvarchar(50)) AS "UNIQUEIDENTIFIER"
FROM (
SELECT 'my-ItemCode' as "ItemCode" FROM DUMMY
);
END;

Create table with concatenating current year and month

I want to create a table in Postgresql if it is not already there and insert some data in it.
The table has to have the following name convention which is test_yyyymm where yyyymm is current year and month.
I tried various ways and stopped on dynamic sql.
DO
$$
BEGIN
EXECUTE 'CREATE TABLE IF NOT EXISTS ' || 'select concat(''test_'', (select to_char(now(), ''yyyymm'')))::text'|| ' (temp_id INT NOT NULL,
temp_name varchar(150),
PRIMARY KEY (temp_id))';
END;
$$
if I make a select with only this part
select concat('test_', (select to_char(now(), 'yyyymm')))::text
I get the name that I want, but it does not work inside dynamic sql.
I've tried to remove that line and replace it with a static name and it works like a charm, so I am sure that, the problem is in the table name.
DO $$
BEGIN
EXECUTE 'CREATE TABLE IF NOT EXISTS ' || 'test_table' || ' (temp_id INT NOT NULL,
temp_name varchar(150),
PRIMARY KEY (temp_id))';
END;
$$
How can I achieve this in a humane way?
Thanks in advance!
You were close. select isn't needed, just use the conversion as-is.
DO $$
BEGIN
EXECUTE 'CREATE TABLE IF NOT EXISTS ' || concat('Log_',to_char(now(), 'yyyymm')) || ' (temp_id INT NOT NULL,
temp_name varchar(150),
PRIMARY KEY (temp_id))';
END;
$$

SQL Use a Variable as tablename

i would like to set the Tablename as Variable
Why: i go from a oracle db to a mysql db via db link and in the mysql db i have some table-names which includes the date- example: data_20121126
here's my statement :
DECLARE
tbname VARCHAR2 (200);
BEGIN
SELECT ab.teste
INTO tbname
FROM (SELECT '"data_'
|| REPLACE (TO_CHAR (SYSDATE - 1, 'yyyy.mm.dd'),
'.',
'')
|| '"#myDB'
AS teste
FROM DUAL) ab;
SELECT * FROM tbname;
END;
could anybody help me please?
thanks in advance
It's not possible with static SQL (and I doubt that creating a new table for every day in your MySQL database is a sensible design approach).
If you really need to do this, you can
use dynamic SQL (EXECUTE IMMEDIATE / OPEN CURSOR FOR ...)
create a synonym once per day, pointing to the current table, and use that synonym in your query
You can't do this without resorting to dynamic SQL:
DECLARE
tbname VARCHAR2 (200) := '"data_'
|| TO_CHAR (SYSDATE - 1, 'yyyymmdd')
|| '"#myDB';
vMyVariable varchar2(10);
BEGIN
execute immediate "SELECT MyColumn
FROM " || tbname into vMyVariable ;
END;
Of course, this version assumes you'll be returning a single column from a single field into a variable. In all likelihood, you'll really want to return a ref cursor to your calling application.

Select from dynamic table names

Consider this query.
SELECT app_label || '_' || model as name from django_content_type where id = 12;
name
-------------------
merc_benz
DJango people might have guessed, 'merc_benz' is a table name in same db. I want to write some SQL migrations and I need to select results from such dynamic table names.
How can i use variable name as a table name???
Something like this...(see RETURN QUERY EXECUTE in the plpgsql portion of the manual)
CREATE function dynamic_table_select(v_id int) returns setof text as $$
DECLARE
v_table_name text;
BEGIN
SELECT app_label || '_' || model into
v_table_name from django_content_type where id = v_id;
RETURN QUERY EXECUTE 'SELECT a_text_column from '||quote_ident(v_table_name);
RETURN;
END
$$ LANGUAGE plpgsql;
It becomes a little more complex if you want to return more than a single column of one type - either create a TYPE that is representative, or if you're using all the columns of a table, there's already a TYPE of that table name. You could also specify multiple OUT parameters.
http://www.postgresql.org/docs/8.1/static/ecpg-dynamic.html
The basic answer I think is EXECUTE IMMEDIATE