SQL经典的asp - 条件语句

时间:2012-06-01 20:48:27

标签: sql asp-classic

我不是SQL的专家,并且已经识别出我的一些现有经典asp代码中的错误,因此它在所有情况下都不起作用。 我希望有一些优秀的SQL经验的人可以帮助我编写两种情况发生时更复杂的情况

If intItemType = SV_ITEM_TYPE_MATRIX然后使用SQL的这部分

LEFT OUTER JOIN usd_matrixSets MS " &_
    "ON MS.itemID = SI.itemID " &_

但是当intItemType <> SV_ITEM_TYPE_MATRIX时,它会被忽略。

正如你所看到的,我已经为这两种情况编写了代码,但这实际上并没有涵盖我可能发生的情况。

以下是代码:

If intItemType = SV_ITEM_TYPE_MATRIX Then   
    strSQL = "SELECT SI.itemID, itemType, itemText, minimumValue, maximumValue, dataType, MS.matrixSetType, MS.matrixSetID " &_
             "FROM usd_SurveyItem SI " &_
             "LEFT OUTER JOIN usd_matrixSets MS " &_
             "ON MS.itemID = SI.itemID " &_
             "WHERE surveyID = " & intSurveyID &_
             " AND itemType " &_
             "In(" & SV_ITEM_TYPE_TEXTAREA & "," &_
                    SV_ITEM_TYPE_SINGLE_LINE & "," &_
                    SV_ITEM_TYPE_DATE & "," &_
                    SV_ITEM_TYPE_CHECKBOXES & "," &_
                    SV_ITEM_TYPE_RADIO & "," &_
                    SV_ITEM_TYPE_DROPDOWN & "," &_
                    SV_ITEM_TYPE_MATRIX & "," &_
                    SV_ITEM_TYPE_UPLOAD_FILE & ")" &_
             " AND pageID = " & intPageNumber &_
             " ORDER BY pageID, SI.orderByID "

        else            
    strSQL = "SELECT SI.itemID, itemType, itemText, minimumValue, maximumValue, dataType" &_
             " FROM usd_SurveyItem SI " &_
             "WHERE surveyID = " & intSurveyID &_
             " AND itemType " &_
             "In(" & SV_ITEM_TYPE_TEXTAREA & "," &_
                    SV_ITEM_TYPE_SINGLE_LINE & "," &_
                    SV_ITEM_TYPE_DATE & "," &_
                    SV_ITEM_TYPE_CHECKBOXES & "," &_
                    SV_ITEM_TYPE_RADIO & "," &_
                    SV_ITEM_TYPE_DROPDOWN & "," &_
                    SV_ITEM_TYPE_MATRIX & "," &_
                    SV_ITEM_TYPE_UPLOAD_FILE & ")" &_
             " AND pageID = " & intPageNumber &_
             " ORDER BY pageID, SI.orderByID "      

        end if      

我的问题是,有时我在我的数据库中同时拥有SV_ITEM_TYPE_MATRIX和其他所有内容。我该如何重写这段代码?

1 个答案:

答案 0 :(得分:2)

您应该始终执行LEFT JOIN,并且当MS.matrixSetType不是MS.matrixSetID时,只允许其他值(NULLSV_ITEM_TYPE_MATRIX)为SELECT SI.itemID, itemType, itemText, minimumValue, maximumValue, dataType, MS.matrixSetType, MS.matrixSetID FROM usd_SurveyItem SI LEFT OUTER JOIN usd_matrixSets MS ON MS.itemID = SI.itemID AND @intItemType = SI.itemType WHERE surveyID = @intSurveyID AND itemType IN ( ... ) AND pageID = @intPageNumber ORDER BY pageID, SI.orderByID

类似的东西(我将使用参数来避免SQL注入漏洞):

{{1}}