If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Can I reference special register USER in the FROM clause?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-12-07, 14:02
DBA-Jr DBA-Jr is offline
Registered User
 
Join Date: Mar 2005
Posts: 108
Can I reference special register USER in the FROM clause?

Greeting All!
I have a quick question. I want to code something like below in dynamic SQL:
Code:
select * from USER.table-name;
I assume that "USER" is the special register, but DB2 does not interpret it as the special register in this context. Can you advise how can I achieve my need?

Thanks you in advance.

Gary
Reply With Quote
  #2 (permalink)  
Old 09-12-07, 14:17
nidm nidm is offline
Registered User
 
Join Date: May 2003
Posts: 113
no. db2(or sql standard don't allow this).
but for dynamic sql, you can always construct your sql. for example in java, a normal query will be prepared and execute like below:


String sqlstmt;
ResultSet rs;
sqlstmt = new String(" SELECT ts1 FROM T1 " )+

rs = stmt.executeQuery(sqlstmt);

System.out.println("query: " + sqlstmt);
while (rs.next()) {
System.out.println("ts1: " + rs.getTimestamp(1));
}

all you need to do is to construct this string " SELECT ts1 FROM T1 " dynamically.
Reply With Quote
  #3 (permalink)  
Old 09-12-07, 15:30
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Quote:
Originally Posted by DBA-Jr
Greeting All!
I have a quick question. I want to code something like below in dynamic SQL:
Code:
select * from USER.table-name;
I assume that "USER" is the special register, but DB2 does not interpret it as the special register in this context. Can you advise how can I achieve my need?

Thanks you in advance.

Gary
You could also do:

set current schema current user
select * from table-name

Andy
Reply With Quote
  #4 (permalink)  
Old 09-12-07, 16:50
DBA-Jr DBA-Jr is offline
Registered User
 
Join Date: Mar 2005
Posts: 108
Thanks. But I don't want to overwrite CURRENT SCHEMA. Any other suggestions?

Quote:
Originally Posted by ARWinner
You could also do:

set current schema current user
select * from table-name

Andy
Reply With Quote
  #5 (permalink)  
Old 09-13-07, 01:47
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
DBA-Jr,
what is your version of DB2 and fixpack level? OS name and level?

Where will result SQL be run? For example in Java application, CMD, etc.

You could get user from special register:
db2 values (current user)
then write a program that concats two SQL strings into one string and then execute.

Hope this helps,
Grofaty
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On