Feb 2, 2012 4:40 AM
declaration of local variables
-
Like (0)
Hi, can you please help me with the syntax for declaring the variable to be used in the queries. I have looked at the examples that are given in 'Stored procs' but it is not working I am looking for something like below: declare @name varchar(10) set select select @name=emp_name from EMPLOYEE where emp_id=12345 select @name Thanks Venkat
You can write like this,
CREATE OR REPLACE PROCEDURE TEST1()
RETURNS VARCHAR(ANY)
EXECUTE AS OWNER
LANGUAGE NZPLSQL AS
BEGIN_PROC
DECLARE
V_NAME RECORD;
BEGIN
SELECT EMP_NAME INTO V_NAME FROM EMP
WHERE EMP_ID=1;
--you can take the entire record as well
RAISE NOTICE '%',V_NAME.EMP_NAME;
END;
END_PROC;
this is again writing in stored proc right?
cant we write in simple query?
It doesn't seem like you can.
The question becomes completely different if you want to use a parametrized proc with dynamic SQL. Then you'd have to use a loop to execute the dynamically built SELECT statement and return the results back to you.
You can try using following method:
1) set a variable (I am assuming you are using Linux client)
export abc=500
2) nzsql -c "select count(*) from test where col1 = $abc;"
You can also create a shell script and do add the queries as follows:
export abc=500
nzsql -c "select count(*) from test where col1 = $abc;"
nzsql -c "select count(*) from test where col1 = $abc;"
Execute the script and queries will use the variable set in script.
Hi Kapil
I am using Aginity Workbench.
can you please help me in writing a simple query which declares a variable and sets the value from a table( no strored procs please
)
something like these:
declare @name varchar(10)
select @name=emp_name from EMPLOYEE where emp_id=12345
select @name
Thanks
Venkat
As I mentioned above, it doesn't seem like you can do it in NZSQL alone. Here's an unofficial workaround that I'm using in some of my procs:
CREATE OR REPLACE PROCEDURE VENKAT()
RETURNS INTEGER
LANGUAGE NZPLSQL AS
BEGIN_PROC
DECLARE
-- table_name ALIAS FOR $1;
sql_statement varchar = '';
r record;
max_key int;
BEGIN
-- Here's a bit of NZPLSQL nonsense... you can't dynamically execute a SELECT statement.
-- We've to use a loop to retrieve the value of the SELECT statement below
sql_statement = 'SELECT COUNT(*) as records FROM DB_NAME..SOME_TABLE' ;
FOR r in EXECUTE sql_statement
LOOP
max_key := r.records;
END LOOP;
INSERT INTO CURRENT_DB_CONTEXT..SOME_OTHER_TABLE values (max_key, NOW());
RETURN max_key;
END;
END_PROC;
I think what others are trying to imply here is that you cannot declare and use variables in NZSQL. You can only do it in NZPLSQL (i.e. in a stored procedure). SQL (i.e. NZSQL) is not a procedural language and does not support the use of variables because declaring, assigning and use of variables are procedural in nature. That is why databases support procedural forms of SQL through stored procedures and functions. In all cases, these procedural languages are proprietary in nature in that each DB vendor has their own dialect and functionality.
Now, it is easy to get confused when looking at some other implementations of SQL front ends. Some vendors allow you to write in-line procedures within their editor and execute it on the fly. However, under the covers it is actually creating, executing and then discarding a stored procedure. Neither Workbench or native NZSQL support this. It is not clear if this will be readily available any time soon because of semantic differences in the BEGIN statement between NZSQL and NZPLSQL. In NZSQL, BEGIN initiates a transaction, while in NZPLSQL, BEGIN initiates a code block. Trying to incorporate both dialects within the same interface can be confusing and vague.
Jordan Chernev wrote:
-- Here's a bit of NZPLSQL nonsense... you can't dynamically execute a SELECT statement.
-- We've to use a loop to retrieve the value of the SELECT statement below
sql_statement = 'SELECT COUNT(*) as records FROM DB_NAME..SOME_TABLE' ;
Not quite sure what you are referring to here. You can always do this:
declare mycount bigint;
begin
select count(*) into mycount from tablex;
You should also be able to paramitize it and use execute immediate:
declare mycount bigint;
mysql varchar(2000);
begin
mysql := 'select count(*) into mycount from ' || $1;
execute immediate mysql;
Nick, I believe my example abive is wrong.
However, I recall that I ran into this limitation at some point and had to resolve it via a FOR LOOP as suggested by the documentation. I'll try to dig up what was preventing me from doing an EXECUTE IMMEDIATE and update my post.

