首先,我已经看到了这个问题的答案,但它们并不适用于我。我的SELECT和INSERT语句中的列数相同。请帮忙,我已经花了半天时间。当我运行它时,它完美地工作但是在最后一次迭代后它给了我一个错误。
消息121,级别15,状态1,行12 INSERT的选择列表 语句包含的项目多于插入列表。的数量 SELECT值必须与INSERT列的数量匹配。
proc [dbo].[uspPOP_WBC]
--params
(@ptblname varchar(MAX)
,@pID_Sub varchar
,@pSubCodeShort varchar(2)
,@pdbname varchar(10) )
AS
BEGIN
SET nocount ON;
DECLARE @strSQL AS nvarchar(MAX)
SET @strSQL = 'INSERT INTO dbo.WBC
( ID
, ID_SUB
, wellcompl
, id_wellcompl
, id_type_wellcompl
, type_wellcompl
, id_wellstring
, wellstring
, type_wellstring
, start_date
, end_date
, comment)'
SET @strSQL = @strSQL +
' SELECT WC.id
,'+convert(varchar(1),@pID_Sub)+'
, WC.name
, WC.id
, WC.id_type_wellcompl
, TWC.name
, WC.id_wellstring
, WS.name
, TWS.name
, WC.start_date
, WC.end_date
, WC.comment
FROM (
['+ @pdbname+ @pSubCodeShort +'].[dbo].[wellbore_compl] as WC
INNER JOIN ['+@pdbname+ @pSubCodeShort+'].[dbo].[wellstring] as WS
ON WC.id_wellstring = WS.id
INNER JOIN ['+@pdbname+ @pSubCodeShort+'].[dbo].[type_wellcompl] as TWC
ON WC.id_type_wellcompl = TWC.id
INNER JOIN ['+@pdbname+ @pSubCodeShort+'].[dbo].[type_wellstring] as TWS
ON WS.id_type_wellstring = TWS.id)
where WC.deletedAt is null'
;
EXEC sp_executesql @strSQL
SET nocount OFF;
END
打印@strSQL
INSERT INTO dbo.WBC ( ID, ID_SUB, wellcompl, id_wellcompl,
id_type_wellcompl, type_wellcompl, id_wellstring, wellstring,
type_wellstring, start_date, end_date, comment)
SELECT
WC.id
, 1
, WC.name
, WC.id
, WC.id_type_wellcompl
, TWC.name
, WC.id_wellstring
, WS.name
, TWS.name
, WC.start_date
, WC.end_date
, WC.comment
FROM (
[PDS_SYNC_CM].[dbo].[wellbore_compl] as WC INNER JOIN [PDS_SYNC_CM].[dbo].[wellstring] as WS ON WC.id_wellstring = WS.id
INNER JOIN [PDS_SYNC_CM].[dbo].[type_wellcompl] as TWC ON WC.id_type_wellcompl = TWC.id
INNER JOIN [PDS_SYNC_CM].[dbo].[type_wellstring] as TWS ON WS.id_type_wellstring = TWS.id)
where WC.deletedAt is null
答案 0 :(得分:0)
如果我的计数是正确的,那么代码在INSERT子句中有12列,在SELECT子句中只有11列。缺少一个,所以请检查您的列以找出哪个缺失并添加它。