reggieboyYEAH
reggieboyYEAH

Reputation: 910

How to execute an SQL string in DB2

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

Upvotes: 4

Views: 26136

Answers (4)

zerone
zerone

Reputation: 115

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.

Upvotes: 4

zawhtut
zawhtut

Reputation: 8551

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

enter image description here

Upvotes: 0

MrG
MrG

Reputation: 1623

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

Upvotes: 5

Leo
Leo

Reputation: 1514

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.

Upvotes: 0

Related Questions