473,467 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help - Dynamic Table

Gb
Hi,
just a quick (maybe stupid) question ..
Can be possible to link a table dynamically using this logic?:

--@@@ SQL START

SELECT Value as myvar, m.field1,m.field2,m.field3 FROM
-- I want to dynamically attach a table here based on the value of
myvar (which is changed by the sql expression builder)
CASE
when myvar='123' then myFile2 m
else myfile3 m
end
where m.field1 ='Blah'

--@@@ SQL END
I cannot use procedure and I want see if something like this can be
done directly in SQL...... just plain sql!!

Thank you!
Nov 12 '05 #1
9 1804
Gb wrote:
Hi,
just a quick (maybe stupid) question ..
Can be possible to link a table dynamically using this logic?:

--@@@ SQL START

SELECT Value as myvar, m.field1,m.field2,m.field3 FROM
-- I want to dynamically attach a table here based on the value of
myvar (which is changed by the sql expression builder)
CASE
when myvar='123' then myFile2 m
else myfile3 m
end
where m.field1 ='Blah'

--@@@ SQL END
I cannot use procedure and I want see if something like this can be
done directly in SQL...... just plain sql!!


Yes, if you use dynamic SQL, then you can construct any statement you like,
including varying table names. But dynamic SQL is usually something done
from an application.

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #2
Gb
It cannot be done directly in SQL?

Knut Stolze <st****@de.ibm.com> wrote in message news:<cc**********@fsuj29.rz.uni-jena.de>...
Gb wrote:
Hi,
just a quick (maybe stupid) question ..
Can be possible to link a table dynamically using this logic?:

--@@@ SQL START

SELECT Value as myvar, m.field1,m.field2,m.field3 FROM
-- I want to dynamically attach a table here based on the value of
myvar (which is changed by the sql expression builder)
CASE
when myvar='123' then myFile2 m
else myfile3 m
end
where m.field1 ='Blah'

--@@@ SQL END
I cannot use procedure and I want see if something like this can be
done directly in SQL...... just plain sql!!


Yes, if you use dynamic SQL, then you can construct any statement you like,
including varying table names. But dynamic SQL is usually something done
from an application.

Nov 12 '05 #3
Uh, no you can't use CASE in the where clause.

Dynamically building the entire correct statement would be your best
choice.

Perhaps you could make use of the ALIAS keyword?

Could you replace the query with a stored procedure?

Lastly,

select *
from ( select
case
when value = '123' then value
else NULL
end as myvar,
case
when value = '123' then field1
else NULL
end as fld1,
case
when value = '123' then field2
else NULL
end as fld2,
case
when value = '123' then field3
else NULL
end as fld3
from myfile2
union
select
case
when value <> '123' then value
else NULL
end as myvar,
case
when value <> '123' then field1
else NULL
end as fld1,
case
when value <> '123' then field2
else NULL
end as fld2,
case
when value <> '123' then field3
else NULL
end as fld3
from myfile3
) as tbl
where myvar is not NULL

HTH,
Charles
In article <85**************************@posting.google.com >,
do*****************@yahoo.com says...
Hi,
just a quick (maybe stupid) question ..
Can be possible to link a table dynamically using this logic?:

--@@@ SQL START

SELECT Value as myvar, m.field1,m.field2,m.field3 FROM
-- I want to dynamically attach a table here based on the value of
myvar (which is changed by the sql expression builder)
CASE
when myvar='123' then myFile2 m
else myfile3 m
end
where m.field1 ='Blah'

--@@@ SQL END
I cannot use procedure and I want see if something like this can be
done directly in SQL...... just plain sql!!

Thank you!

Nov 12 '05 #4
Try this:

SELECT Value as myvar, m.field1,m.field2,m.field3 FROM
myFile2
WHERE myvar='123' as m
AND m.field1 = 'Blah'
UNION ALL
SELECT Value as myvar, m.field1,m.field2,m.field3 FROM
myFile3 as m
WHERE myvar<>'123'
and m.field1 ='Blah'

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #5
Gb wrote:
It cannot be done directly in SQL?


In a stored procedure, you can do that. You would use the procedure to
build a string containing the SQL statement and then execute that statement
dynamically.

But I'd say that Serge's approach would be the easiest for your example.

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #6
Charles Wilt wrote:
Uh, no you can't use CASE in the where clause.


I guess you meant the FROM clause, right? (And even there you could have it
in the join predicate - but that's another topic.)

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #7
Gb
Great guys!

Thank you

Serge Rielau <sr*****@ca.eye-be-em.com> wrote in message news:<cc**********@hanover.torolab.ibm.com>...
Try this:

SELECT Value as myvar, m.field1,m.field2,m.field3 FROM
myFile2
WHERE myvar='123' as m
AND m.field1 = 'Blah'
UNION ALL
SELECT Value as myvar, m.field1,m.field2,m.field3 FROM
myFile3 as m
WHERE myvar<>'123'
and m.field1 ='Blah'

Cheers
Serge

Nov 12 '05 #8
Serge,

I'm hesitent to ask since you're from the IBM SQL compiler development
team, but....

Is that going to work? How?
"WHERE myvar='123' as m
AND m.field1 = 'Blah'"

Seems to be wrong to me given the "m" used as a table/correlation name
and a column name.
Using a basic UNION like this was my original thought. But I thought it
wouldn't work. I could have sworn I tested it and it didn't work.
Which lead to the complicated mess I posted <grin>. But trying again it
works fine.
Charles
In article <cc**********@hanover.torolab.ibm.com>, sr*****@ca.eye-be-
em.com says...
Try this:

SELECT Value as myvar, m.field1,m.field2,m.field3 FROM
myFile2
WHERE myvar='123' as m
AND m.field1 = 'Blah'
UNION ALL
SELECT Value as myvar, m.field1,m.field2,m.field3 FROM
myFile3 as m
WHERE myvar<>'123'
and m.field1 ='Blah'

Cheers
Serge

Nov 12 '05 #9
Never trust anything just because of a signature....
Of course the correlation name m belongs to the FROM clause :-)

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Pat Patterson | last post by:
I'm having serious issues with a page I'm developing. I just need some simple help, and was hoping someone might be able to help me out in here. I have a form, that consists of 3 pages of...
1
by: Rohit Raghuwanshi | last post by:
Hello all, we are running a delphi application with DB2 V8.01 which is causing deadlocks when rows are being inserted into a table. Attaching the Event Monitor Log (DEADLOCKS WITH DETAILS) here....
18
by: Bill Smith | last post by:
The initial row is inserted with the colPartNum column containing a valid LIKE pattern, such as (without the single quotes) 'AB%DE'. I want to update the column value with the results of a query...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
4
by: Laura | last post by:
Here's the situation: I'm trying to use an update query to copy data from one row to another. Here is the situation: I have 5 companies that are linked to each other. I need to show all 5...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
6
by: hb | last post by:
Hi, I have a page bill.aspx and its code-behind bill.aspx.cs. On bill.aspx I have: === Select a month: <asp:dropdownlist runat="server" id="lstDate" autopostback="True" /> <br> <asp:table...
3
by: mcmahonb | last post by:
Hey people... I've been searching this forum for a few hours and even though this topic has been went over from many different angles; I cannot seem to figure out how to make things work on my...
0
by: SOI_0152 | last post by:
Hi all! Happy New Year 2008. Il hope it will bring you love and happyness I'm new on this forum. I wrote a stored procedure on mainframe using DB2 7.1.1 and IBM language c. Everything works...
0
by: richard12345 | last post by:
Hi Guys I have problem with site I am building. The sidebar with menu and other thinks is overlapping footer. The footer move with the content and but it dos it dos not move with the sidebar. ...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.