SQL 语句插入
SQL Statement Insert Into
无论我做什么,我都会得到以下信息,如果有任何帮助,那就太棒了。
Msg 116, Level 16, State 1, Line 15
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
Msg 109, Level 15, State 1, Line 1
There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.
我的查询
[tableA].[PROJECTID],
[tableA].[STUDYID],
[tableA].[SUBJNO],
[tableA].[CASENUMBER],
[tableA].[CASESTATUS],
[tableA].[MODIFIEDBY]
)VALUES((
SELECT b.PROJECTID,
((SELECT TOP 1 a.STUDYID FROM [PRODVIEW] a WHERE a.DYNAME = b.DYNAME and
a.ProjID = b.PROJID)) as STUDYID,
b.SUBJNO,
(b.SUBJNO + '_' + b.SEQUENCE) as CaseNumber,
'READY' as CASESTATUS,
b.UPLOADEDBY
FROM [dbo].[TableB] b WHERE VIEWED = 0
AND b.UPLOADEDDATE >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)))
如果要使用 SELECT
作为 INSERT
的数据源,则不要使用 VALUES
,它用于插入文字数据:
INSERT INTO yourTable ([PROJECTID], [STUDYID], [SUBJNO], [CASENUMBER], [CASESTATUS],
[MODIFIEDBY])
SELECT
b.PROJECTID,
(SELECT TOP 1 a.STUDYID FROM [PRODVIEW] a
WHERE a.DYNAME = b.DYNAME and a.ProjID = b.PROJID),
b.SUBJNO,
(b.SUBJNO + '_' + b.SEQUENCE),
'READY',
b.UPLOADEDBY
FROM [dbo].[TableB] b
WHERE
VIEWED = 0 AND
b.UPLOADEDDATE >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0);
可能有一种方法可以在不使用 select 子句中的相关子查询的情况下编写查询,例如通过加入。实际上,带有 TOP
的子查询没有任何意义,因为没有 ORDER BY
子句。
另请注意,您不需要在 SELECT
语句中使用别名。事实上,它们将被忽略,因为 INSERT
确定目标列。
无论我做什么,我都会得到以下信息,如果有任何帮助,那就太棒了。
Msg 116, Level 16, State 1, Line 15
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.Msg 109, Level 15, State 1, Line 1
There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.
我的查询
[tableA].[PROJECTID],
[tableA].[STUDYID],
[tableA].[SUBJNO],
[tableA].[CASENUMBER],
[tableA].[CASESTATUS],
[tableA].[MODIFIEDBY]
)VALUES((
SELECT b.PROJECTID,
((SELECT TOP 1 a.STUDYID FROM [PRODVIEW] a WHERE a.DYNAME = b.DYNAME and
a.ProjID = b.PROJID)) as STUDYID,
b.SUBJNO,
(b.SUBJNO + '_' + b.SEQUENCE) as CaseNumber,
'READY' as CASESTATUS,
b.UPLOADEDBY
FROM [dbo].[TableB] b WHERE VIEWED = 0
AND b.UPLOADEDDATE >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)))
如果要使用 SELECT
作为 INSERT
的数据源,则不要使用 VALUES
,它用于插入文字数据:
INSERT INTO yourTable ([PROJECTID], [STUDYID], [SUBJNO], [CASENUMBER], [CASESTATUS],
[MODIFIEDBY])
SELECT
b.PROJECTID,
(SELECT TOP 1 a.STUDYID FROM [PRODVIEW] a
WHERE a.DYNAME = b.DYNAME and a.ProjID = b.PROJID),
b.SUBJNO,
(b.SUBJNO + '_' + b.SEQUENCE),
'READY',
b.UPLOADEDBY
FROM [dbo].[TableB] b
WHERE
VIEWED = 0 AND
b.UPLOADEDDATE >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0);
可能有一种方法可以在不使用 select 子句中的相关子查询的情况下编写查询,例如通过加入。实际上,带有 TOP
的子查询没有任何意义,因为没有 ORDER BY
子句。
另请注意,您不需要在 SELECT
语句中使用别名。事实上,它们将被忽略,因为 INSERT
确定目标列。