4

How do I execute an SQL string statement in DB2? I'm using IBM Data Studio.

4 Answers 4

5

Do you mean executing a dynamic SQL string? Something like:

DECLARE stmt VARCHAR(1000);
DECLARE my_table VARCHAR(50);
SET my_table = 'DEPT_'||deptNumber;
SET stmt = 'SELECT * FROM '||my_table;
PREPARE s1 FROM stmt;
EXECUTE s1;

You can only do that in a stored proc though. One defined as CREATE PROCEDURE GetDeptInfo (deptNumber VARCHAR(5)) for this example. Read about EXECUTE and PREPARE in the db2 docs http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/index.jsp

Sign up to request clarification or add additional context in comments.

Comments

5

After days of researching I found how to write and run dynamic SQL on DB2:

create or replace procedure Search ()

    BEGIN

       DECLARE v_dynamicSql varchar(2000);
       SET v_dynamicSql = 'INSERT INTO dictonary(name) values(' || 'dynamicSQL in db2' ||')';

       EXECUTE IMMEDIATE v_dynamicSql;

    END;

Hope to help someone.

Comments

0

What difficulty are you encountering?

There are likely lots of ways to do it. Here's one:

File -> New -> Other -> SQL or XQuery script

You may need to create a project or define a database connection.

Enter the SQL code.

Script -> Run script.

Results will show up at the bottom of your screen.

Comments

0

In Control Center, right click the database, you will see "Query". Click on it and you are good to go.

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.