English 中文(简体)
改变动态 JOINS to statements
原标题:Converting dynamic JOINS to SQL statements
  • 时间:2011-06-10 07:33:24
  •  标签:
  • sql

我需要一种想法,将以下“如果是”条件转变为原始声明。 (无动态)

Dim strVar1 as String, strVar2 as String, strVar3 as String, intSet Int32

.Append("SELECT * FROM TABLE_A as A ")
.Append("INNER JOIN TABLE_B as B ON A.Code=B.Code_A ")

If strVar1.Length > 0 Then
  .Append("LEFT JOIN TABLE_C as C ON C.Code=A.Code_C ")
  .Append("LEFT OUTER JOIN TABLE_D as D on C.Code=D.Code_C AND D.Col1 IN (1,NULL) ")
End If

If strVar2.Length > 0 Or strVar3.Length > 0 Then
  .Append("LEFT OUTER JOIN TABLE_E as E ON A.Code=E.Code_A ")
  .Append("LEFT OUTER JOIN TABLE_F as F ON E.Col1=F.Col1 ")
  .Append("INNER JOIN TABLE_G ON as G ON F.Code=G.Code_F AND G.Col1=1 ")
End If

Select Case intType
    Case 1 , 2    
       If intSet <> -1 Then
         .Append("LEFT OUTER JOIN TABLE_H H on A.code=H.code_A AND H.code_H=1 AND H.Pos=1 ")
       Else
         .Append("LEFT OUTER JOIN TABLE_H H on A.code=H.code_A AND H.Pos=1 ")
       End If
    Case 3 , 4
End Select
问题回答

我不敢肯定,将你的生动 code法变成一个具有参数的单一q。 我感觉到,唯一的解决办法是,为你在检测中使用的每一种价值观组合制造一个疑问。 有许多问题:如果我正确计算,有16个问题。

如果你的目标是加快你的问答,我很相信这将有助于:执行计划每次都要计算。 但是,这是以更为复杂的法典为代价的。

You may try WHEN THEN SQL constructs! Logic: All joins will be there. However if you want to join based on some condition, put cross table column in join clause else the same table column Something like:

 SELECT DISTINCT TABLE_A.* FROM TABLE_A as A
INNER JOIN TABLE_B as B ON A.Code=B.Code_A
LEFT JOIN TABLE_C as C ON C.Code=
    CASE WHEN LEN(@strVar1)>0
    THEN
    A.Code_C -- join with your table
    ELSE
    C.Code -- join with itself to do nothing
    END
LEFT OUTER JOIN TABLE_D as D on C.Code=
    CASE WHEN LEN(@strVar1)>0 THEN
    D.Code_C AND D.Col1 IN (1,null)
    ELSE
    C.Code
    END

and so on





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签