为什么这个SELECT语句没有FROM?

时间:2012-07-17 19:12:34

标签: sql tsql coldfusion

<CFIF ListLen(SESSION.WHSurveyStruct.reasonString, ";") gt 0>
    <CFQUERY name="insertReasons" datasource="#REQUEST.dsn#">
        INSERT INTO TWelcomeHome_Reason
        (ReasonID, SubReasonID, SurveyID)
        SELECT #sanitize(ListFirst(SESSION.WHSurveyStruct.reasonString, ";"))#, #sanitize(getLatestSurveyID.SurveyID)# 
        <CFLOOP list="#sanitize(ListRest(SESSION.WHSurveyStruct.reasonString, ';'))#" index="thisReason" delimiters=";">
            UNION ALL
            SELECT #sanitize(thisReason)#, #sanitize(getLatestSurveyID.SurveyID)#
        </CFLOOP>

    </CFQUERY>

我正在努力了解这是做什么的。我对循环感到困惑,为什么select语句没有FROM?好吧,他们只是标量。

如何在循环外部有一个select语句,在内部有一个?我有点不明白union all。为什么有3列被指定(ReasonID,SubReasonID,SurveyID)但是在每个select中给出了2个值?

转储:

  

结构

     

CACHED:false EXECUTIONTIME:0 RECORDCOUNT:8 SQL:INSERT INTO   TWelcomeHome_Reason(ReasonID,SubReasonID,SurveyID)SELECT 6,   18,245                 UNION ALL
                SELECT 6,21,245
                UNION ALL
                SELECT 6,24,245
                UNION ALL
                SELECT 3,5,245
                UNION ALL
                SELECT 3,6,245
                UNION ALL
                SELECT 3,8,245
                UNION ALL
                SELECT 3,11,245
                UNION ALL
                SELECT 3,7,245

     

2 个答案:

答案 0 :(得分:16)

它看起来只是SELECT标量值,而不是来自任何表的记录。所以

INSERT INTO myTable
SELECT 'foo'
UNION ALL
SELECT 'bar'

会将两条记录插入myTable,foobar

答案 1 :(得分:7)

简短的回答是,它不是从表中选择的。所以FROM没有表格。

如果您执行:

INSERT INTO TableSomething (ColumnA)
SELECT 'A'
UNION ALL
SELECT 'B'

它会将A和B插入ColumnA。

ColdFusion正在创建要插入而不是从表中拉出的数据。