我有以下选择查询,它按预期运行正常(其目的是从另一个表中的字符串构建单词列表):
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
查询有效,为什么将数据插入新表的这种简单修改会失败?
答案 0 :(得分: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;