我正在从这个选择语句中获取数据
SELECT 'NONSTK' AS [FormType]
,S.[NonStockCode] AS [KeyField]
,'EBQ001' AS [FieldName]
,NULL AS [AlphaValue]
,1 AS [NumericValue]
,NULL AS [DateValue]
FROM [SysproCompanyA].[dbo].[QotNonStock] S
LEFT OUTER JOIN [SysproCompanyA].[dbo].[AdmFormData] N
ON S.[NonStockCode] = N.[KeyField]
WHERE ( N.[FormType] = 'NONSTK'
OR N.[FormType] IS NULL
)
AND N.[KeyField] IS NULL
并尝试将其插入以下
INSERT INTO [SysproCompanyT].[dbo].[AdmFormData]
( [FormType]
,[KeyField]
,[FieldName]
,[AlphaValue]
,[NumericValue]
,[DateValue]
)
SELECT 'NONSTK' AS [FormType]
,S.[NonStockCode] AS [KeyField]
,'EBQ001' AS [FieldName]
,NULL AS [AlphaValue]
,1 AS [NumericValue]
,NULL AS [DateValue]
FROM [SysproCompanyA].[dbo].[QotNonStock] S
LEFT OUTER JOIN [SysproCompanyA].[dbo].[AdmFormData] N
ON S.[NonStockCode] = N.[KeyField]
WHERE ( N.[FormType] = 'NONSTK'
OR N.[FormType] IS NULL
)
AND N.[KeyField] IS NULL
返回以下内容
子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。
我以为我以前做过这件事,但我无法靠近它。
那么可以吗?在此期间我将启动我的usp光标一次执行一次插入
答案 0 :(得分:0)
全部
请参阅上面的评论。但是为了解决我的问题,我使用了像这样的前进光标
--This cusor reads from one select statement result set
--loads data from result set in variable on a loop basis
-- and inserts into the existing sql table
-- Declare variable to hold insert data
--REMEMBER THE SEMICOLONS!!!
DECLARE @NonStockCode varchar(80);
--Declare cusor
DECLARE InsertCursor CURSOR FAST_FORWARD
For
SELECT S.[NonStockCode]
FROM [SysproCompanyA].[dbo].[QotNonStock] S
left outer join [SysproCompanyA].[dbo].[AdmFormData] N On S.[NonStockCode]=N.[KeyField]
where (N.[FormType]='NONSTK' or N.[FormType] is null)
and N.[FieldName] <>'MOU001';
--Open Cusor
OPEN InsertCursor;
--Read first row of data and store in variables
FETCH NEXT FROM InsertCursor INTO @NonStockCode;
Print @NonStockCode; --Test print
--Now use loop to process result set of cusor
WHILE @@FETCH_STATUS = 0
BEGIN
Print 'NONSTK'+' ' +@NonStockCode+'MOU001'; --Test print
INSERT INTO [SysproCompanyA].[dbo].[AdmFormData]
([FormType] ,[KeyField],[FieldName],[AlphaValue],[NumericValue] ,[DateValue])
VALUES ('NONSTK',@NonStockCode,'MOU001' ,'99',null ,null );
FETCH NEXT FROM InsertCursor INTO @NonStockCode;
END
--Now Close and deallocate the cursor MUST HAPPEN
CLOSE InsertCursor;
DEALLOCATE InsertCursor;
Go