2014-01-17 45 views
1

我有以下選擇查詢,它如預期運行良好(其目的是從字符串在另一個表建立一個單詞列表):爲什麼這個SELECT查詢能夠工作,但是當我嘗試SELECT INTO時失敗?

SELECT UPPER(LTRIM(RTRIM(allWords.words))) 
FROM 
(
    SELECT section.Cols.value('.', 'varchar(250)') words 
    FROM @xml.nodes('/c') section(Cols) 
) AS allWords 
WHERE 
    LTRIM(RTRIM(words)) <> '' 
    AND dbo.RegExIsMatch('.*[\W\d].*',LTRIM(RTRIM(words)),1) <> 1 
    AND LEN(LTRIM(RTRIM(words))) > 3 
GROUP BY words 

但是,當我加入INTO聲明失敗:

IF OBJECT_ID('usr.nameList') IS NOT NULL DROP TABLE usr.nameList; 

SELECT UPPER(LTRIM(RTRIM(allWords.words))) 
INTO usr.nameList 
FROM 
(
    SELECT section.Cols.value('.', 'varchar(250)') words 
    FROM @xml.nodes('/c') section(Cols) 
) AS allWords 
WHERE 
    LTRIM(RTRIM(words)) <> '' 
    AND dbo.RegExIsMatch('.*[\W\d].*',LTRIM(RTRIM(words)),1) <> 1 
    AND LEN(LTRIM(RTRIM(words))) > 3 
GROUP BY words 

輸出是:

對象或列名稱缺失或爲空。對於SELECT INTO 語句,請確認每列都有一個名稱。對於其他語句,請查看 以獲取空白別名。別名定義爲「」或[]是不允許的。 將別名更改爲有效的名稱。

如果SELECT查詢有效,爲什麼要將數據插入到新表中的這種簡單修改會失敗?

回答

4

你在錯誤信息中缺少什麼?您需要提供該列的名稱:

SELECT UPPER(LTRIM(RTRIM(allWords.words))) as words 
INTO usr.nameList 
FROM 
(
    SELECT section.Cols.value('.', 'varchar(250)') words 
    FROM @xml.nodes('/c') section(Cols) 
) AS allWords 
WHERE 
    LTRIM(RTRIM(words)) <> '' 
    AND dbo.RegExIsMatch('.*[\W\d].*',LTRIM(RTRIM(words)),1) <> 1 
    AND LEN(LTRIM(RTRIM(words))) > 3 
GROUP BY words;