Click here to Skip to main content
15,888,008 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there,

I appreciate if someone can assist me with my SQL queries.

I require assistance in creating a generic query which will work depending on the parameters given.

Here is my following table structure I have to work with:
Table1
MachId
MachCode
MachLinkType(Can be of Type 'ROBOT', 'MINI')

Table2
StatId
Stat_GDU_Code(linked to the Table 3)
Stat_MachCode(linked to Table1)

Table3
GDU_Id
GDU_prefix
GDU_code
GDU_description
Note The GDU_CODE is not unique. The GDU_prefix and GDU_id have to be unique.

Table4
PlacId
Plac_Mach_Code(links to MachCode)
PlacType(e.g FROD, PLACK)
PlacDescription

If the MachLinkType is 'ROBOT' then I look in Table 1 and Table 4 to get the description etc:
SQL
SELECT MachId, MachCode, MachLinkType, PlacType, Plac_Description 
from Table1 INNER JOIN Table4
ON Table1.MachCode = Table4.Plac_Mach_Code
RTRIM(MachLinkType)= 'ROBOT' AND RTRIM(PlacType)= 'FROD'


Now if the value is not ROBOT then I have to interrogate 3 tables (Table 1, Table 2 and Table 3) but I have 2 issues with this:

Firstly if I include the GDU_Description fields I get duplicate records.

I have tried using 'distinct' on the Table1.MachId but I think that is incorrect way of using it for my query.

I need the description of the record from Table 3 in regards to the output I am trying to generate.

Below is my query if the value provided by the user isn't PLAC:
SQL
SELECT distinct(MachId), MachCode, MachLinkType, Stat_GDU_Code, GDU_Description 
from Table1 INNER JOIN Table2
ON Table1.MachCode = Table2.Stat_MachCode
INNER JOIN Table2 ON
Table3.GDU_Code = Table2.Stat_GDU_Code
WHERE RTRIM(MachLinkType) = 'MINIBOT' AND RTRIM(Stat_GDU_Code) = '31A'


Secondly, if user provides ROBOT and MINIBOT types how can I combine the two queries above in an appropriate manner so I can retrieve results for both ROBOT and MINIBOT types.

I appreciate if someone can assist me please?

This is the sort of output i want to generate in my user control:

MachineReference:123
MachineCode: EPIS
MachineLinkType: AUTONOMOUS
MachinePartCode: 31A
MachineDescription: 'Test Description'.

Regardless of which table the respective data lives in i would like to use assign the data to those 5 labels.

Thanks,
Posted
Updated 31-May-15 4:37am
v2

1 solution

Try build Query based on the input in your Code behind or in SQL Server and execte. Those are the only way to achieve this.

For Dynamic SQL Query[^]

Select Query Builder C#[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900