You can translate the question and the replies:

VQL Stored Procedure read variable in execute command

Hi, I need connect a database in vql stored procedure with variable. I try this: > (vdb_name IN VARCHAR, prt OUT VARCHAR) > AS ( > -- IN > vdb VARCHAR; > -- OUT > prt VARCHAR; > > ) BEGIN EXECUTE 'CONNECT DATABASE '':vdb'''; RETURN ROW (prt) VALUES (vdb); END But receive the error: > QUERY [SELECTION] [ERROR] > SP_REPLACE_INTERFACEVIEW [STORED_PROCEDURE] [ERROR] > SP_REPLACE_INTERFACEVIEW [STORED PROCEDURE] [ERROR] Received exception with message 'Syntax error: Exception parsing query near '''' Any idea how i call the variable in VQL Stored Procedure in command execute 'connect database'? Thanks Best regrads
user
01-07-2024 10:02:02 -0400
code

5 Answers

Hello, The error occurs because of an issue with the SQL syntax in the EXECUTE statement within the stored procedure. Specifically, the statement `CONNECT DATABASE '':vdb''`; tries to concatenate a database name (`:vdb`) with single quotes, but it results in a syntax error due to the extra single quotes around `:vdb`. This causes the database to interpret the statement incorrectly. In the VQL stored procedure you provided, the `:vdb` part is used to parameterize a SQL statement that is executed dynamically. When creating a Denodo VQL stored procedure, PARAMETERS are used to specify `:vdb` as a variable, and VALUES are used to assign it a value, as shown in the example below: Procedure `connect_and_return`: ``` (vdb_name IN VARCHAR, prt OUT VARCHAR) AS ( -- Procedure variables -- IN vdb VARCHAR; -- OUT prt VARCHAR; ) BEGIN -- Construct and execute the dynamic SQL statement to connect to the database EXECUTE 'CONNECT DATABASE :vdb PARAMETERS (vdb) VALUES(vdb_name); -- Return the output parameter value RETURN ROW (prt) VALUES (vdb_name); END; ``` To call the procedure `connect_and_return` with an example value for `teacher_database`: ```CALL connect_and_return('teacher_database');``` In this example, PARAMETERS are used to specify that `:vdb` represents a variable, while VALUES are used to assign it a value. Here, (vdb_name) represents the parameter passed as an input for the :vdb variable in the dynamic SQL statement used to connect to the database. For more details on the stored procedures, you can refer to the [Developing VQL Stored Procedures](https://community.denodo.com/docs/html/browse/8.0/en/vdp/developer/developing_extensions/developing_stored_procedures/developing_vql_stored_procedures) user manuals. If you still need assistance and you are a valid support user, you can raise a support case for help from our Support team. Hope this helps!
Denodo Team
01-07-2024 18:09:15 -0400
code
Hi, Thanks for responce. The proposed syntax no longer returns any errors, but it does not connect to another Virtual Database. I have interface_view called '**i_apofrp_d_version**' in Virtual Data Base called '**apofrp**'. My VQL Stored Procedure in the Virtual Data Base called '**dnd_tst**', when I run the code below, I receive the error 'View '**i_apofrp_d_versao**' not found', as the view does not exist in the Virtual Data Base '**dnd_tst**' but in the Virtual Data Base '**apofrp**' where it is should be have connected. ``` (vdb_name IN VARCHAR, prt OUT VARCHAR) AS ( -- Variaveis de Input vdb VARCHAR; -- Variavel de Output o_count INTEGER; prt VARCHAR; cursor c1 is 'select count(*) from i_apofrp_d_versao'; ) BEGIN vdb:=vdb_name; EXECUTE 'CONNECT DATABASE :vdb;' PARAMETERS (vdb) VALUES(vdb_name); RETURN ROW (prt) VALUES (vdb); OPEN C1; FETCH C1 INTO o_count; CLOSE C1; RETURN ROW (prt) VALUES (o_count); END ``` Any idea how to force the connection to a Virtual Data Base different from the Virtual Data Base where I created the Stored Procedure? Thanks
user
03-07-2024 10:18:23 -0400
Hi, To achieve this, you don’t need to connect to the Virtual database to retrieve data from a specific view within it. In Denodo you can simply pass the database name in the FROM clause when executing the SELECT statement within the cursor. For example, you can modify the provided procedure as follows to retrieve data from the desired interface: ``` (vdb_name IN VARCHAR, prt OUT VARCHAR) AS ( -- Variaveis de Input vdb VARCHAR; -- Variavel de Output o_count INTEGER; prt VARCHAR; CURSOR c1 IS 'SELECT COUNT(*) FROM :vdb.i_apofrp_d_versao; ) BEGIN vdb := vdb_name; OPEN C1 PARAMETERS (vdb) VALUES(vdb); FETCH C1 INTO o_count; CLOSE C1; RETURN ROW (prt) VALUES (o_count); END ``` If you still need assistance and you are a valid support user, you can raise a support case for help from our Support team. Hope this helps!
Denodo Team
03-07-2024 18:03:00 -0400
code
Hi, Thanks for your response. But the problem not is how i SELECT any view from other Virtual Data base. The problem it´s the command CONNECT DATABASE not work in VQL Stored Procedures. I tried put the VDB name directly in command like this: > (prt OUT VARCHAR) > AS ( > o_count INTEGER; > prt VARCHAR; > cursor c1 is 'select count(*) from i_apofrp_d_versao'; > ) > BEGIN > EXECUTE 'CONNECT DATABASE vdb_apofrp'; > OPEN C1; > FETCH C1 INTO o_count; > CLOSE C1; > RETURN ROW (prt) VALUES (o_count); > END > and not works. The VQL SP not connect to the Virtual Data Base vdb_apofrp. The error it´s: 'Received exception with message 'View 'i_apofrp_d_versao' not found' It's probably a BUG in Stored Procedures VQL that doesn't recognize the CONNECT DATABASE command? Thanks.
user
04-07-2024 05:11:41 -0400
Hi, The behavior you are seeing with the **CONNECT** statement is expected. According to the [Developing VQL Stored Procedures](https://community.denodo.com/docs/html/browse/8.0/en/vdp/developer/developing_extensions/developing_stored_procedures/developing_vql_stored_procedures) page of the developer guide, the **EXECUTE** keyword is meant for DDL statements only. **CONNECT** is not typically considered a DDL (Data Definition Language) statement in the context of SQL databases. **CONNECT** is generally used to establish a connection to a database or another system, rather than to define, modify, or remove the structure of database objects. In SQL, DDL statements are primarily concerned with schema definition and management, such as creating, altering, and dropping database objects like tables, views, indexes, etc. These operations involve changing the structure or schema of the database rather than establishing a connection. However, in similar cases, the parser would return a ‘**Query Not Supported**’ error instead of the given success message. As a workaround to access data residing in other databases in Denodo, you can pass the database name in the FROM clause when executing the SELECT statement. ``` (vdb_name IN VARCHAR, prt OUT VARCHAR) AS ( -- Variaveis de Input vdb VARCHAR; -- Variavel de Output o_count INTEGER; prt VARCHAR; CURSOR c1 IS 'SELECT COUNT(*) FROM :vdb.i_apofrp_d_versao'; ) BEGIN vdb := vdb_name; OPEN C1 PARAMETERS (vdb) VALUES(vdb); FETCH C1 INTO o_count; CLOSE C1; RETURN ROW (prt) VALUES (o_count); END ``` If you need to use the **CONNECT DATABASE** command or would like an error message to be thrown, you can raise an enhancement support case for help from our support team. Hope it helps!
Denodo Team
08-07-2024 13:28:23 -0400
code
You must sign in to add an answer. If you do not have an account, you can register here