【发布时间】:2014-11-12 13:50:31
【问题描述】:
我在创建存储过程时遇到了一些问题,该过程将记录添加到临时表中。
我收到上述错误消息,不是在我尝试执行该过程时,而是在第一次尝试创建它时。我检查了语句中的每个子查询,它看起来不错,因为它们设置为顶部 1 并且只返回一个值。我还注释掉了子选择并用硬编码值替换了它们,但这仍然给出了同样的错误。
怎么会在没有子查询的时候返回这个错误呢?
我已经查看了此处的其他答案,但它们似乎与子查询返回的多个值直接相关,在这种情况下并没有发生。
-- Drop stored procedure if it already exists
IF EXISTS (
SELECT *
FROM INFORMATION_SCHEMA.ROUTINES
WHERE SPECIFIC_SCHEMA = N'dbo'
AND SPECIFIC_NAME = N'PLO_CreateExchanges'
)
DROP PROCEDURE dbo.PLO_CreateExchanges
GO
CREATE PROCEDURE dbo.PLO_CreateExchanges
@FirstExchange DATETIME,
@EndDate DATETIME,
@StartDate DATETIME,
@Frequency INT,
@AgreementID INT
AS
BEGIN
DECLARE @ExchangeDate DATETIME
DECLARE @NumberOfExchanges INT
DECLARE @ExchangeQty INT
--Data
WHILE @ExchangeDate <= @EndDate
BEGIN
IF @ExchangeDate >= @StartDate
BEGIN
--Do something
IF OBJECT_ID('tempdb..#Orders') IS NOT NULL
BEGIN
DROP TABLE #Orders
END
ELSE
BEGIN
--Create Table
SELECT *
INTO #Orders
FROM dbo.Orders
WHERE id < 0
ALTER TABLE dbo.#Orders
DROP COLUMN id
END
SET @AgreementID = 1
INSERT INTO #Orders
(AgreementID ,
TransType ,
StartDate ,
EndDate ,
JobNumber ,
AddressID ,
DeliveryAddressID ,
AreaID ,
ProductTypeID ,
InternalNoteID ,
CollectionTime ,
ContainerTypeID ,
DeliveryContainerID ,
CollectionContainerID ,
NetCharge ,
SpecialPrice ,
VehicleID ,
DisposalSiteID ,
DisposalCharge ,
DisposalTicket ,
OnRoad ,
Authorisation1 ,
Authorisation2 ,
Weighed ,
Printed ,
InvoiceNumber ,
InvoiceDate ,
WeighbridgeTransactionID ,
RouteID ,
RouteSequence ,
Quantity ,
Activity ,
ContactPerson ,
ContactTime ,
Notes1 ,
Notes2 ,
Notes3 ,
InvoiceMethodID ,
ScheduledDate ,
Extracted ,
AdditionalCharge ,
AdditionalChargeDesc ,
ScheduledSiteID ,
ChargedBy ,
Weight ,
HaulierID ,
AllocationCode ,
ConsignmentNumber ,
PreNoteNumber ,
Drums ,
HaulageCharge ,
Rate ,
Manual ,
Hours ,
CurrencyID ,
Confirmed ,
Paid ,
VehicleTypeID ,
QuotationID ,
ContractID ,
ScheduledTypeID ,
TempField1 ,
TempField2 ,
PaymentID ,
EWCID ,
DisposalCost ,
Hold ,
SalesID ,
Cost ,
CostSpecial ,
HaulageCost ,
MobileStatus ,
Scheduled ,
Complete ,
SiteMobileStatus ,
ContainerPriceID ,
WeighbridgePriceID ,
OriginalQuantity ,
MissedEmail ,
DateTimeLastUpdated ,
LastUpdatedBy ,
IsDeleted)
SELECT @AgreementID , -- AgreementID - int
'D' , -- TransType - char(2)
A.StartDate , -- StartDate - datetime
A.StartDate , -- EndDate - datetime
NULL , -- JobNumber - int
A.AddressID , -- AddressID - int
A.DeliveryAddressID , -- DeliveryAddressID - int
A.AreaCodeID , -- AreaID - int
A.ProductTypeID , -- ProductTypeID - int
A.InternalNoteID , -- InternalNoteID - int
A.DropTime , -- CollectionTime - datetime
A.ContainerTypeID , -- ContainerTypeID - int
NULL , -- DeliveryContainerID - int
NULL , -- CollectionContainerID - int
A.DeliveryCharge , -- NetCharge - decimal
A.SpecialPrice , -- SpecialPrice - bit
A.DefaultDeliveryVehicleID , -- VehicleID - int
A.DisposalSiteID , -- DisposalSiteID - int
NULL , -- DisposalCharge - decimal
NULL , -- DisposalTicket - nvarchar(30)
A.OnRoad , -- OnRoad - bit
A.Authorisation1 , -- Authorisation1 - nvarchar(50)
A.Authorisation2 , -- Authorisation2 - nvarchar(50)
0 , -- Weighed - bit
0 , -- Printed - bit
0 , -- InvoiceNumber - int
NULL , -- InvoiceDate - datetime
NULL , -- WeighbridgeTransactionID - int
A.DeliveryRoute , -- RouteID - int
NULL , -- RouteSequence - int
1 , -- Quantity - int
NULL , -- Activity - nvarchar(10)
A.ContactPerson , -- ContactPerson - nvarchar(250)
A.ContactTime , -- ContactTime - datetime
A.Notes1 , -- Notes1 - nvarchar(350)
A.Notes2 , -- Notes2 - nvarchar(350)
A.Notes3 , -- Notes3 - nvarchar(350)
A.InvoiceMethodID , -- InvoiceMethodID - int
A.StartDate , -- ScheduledDate - datetime
0 , -- Extracted - bit
(SELECT CASE WHEN A.ApplyToDelivery = 1 THEN A.AdditionalCharge
ELSE 0
END) , -- AdditionalCharge - decimal
(SELECT CASE WHEN A.ApplyToDelivery = 1 THEN A.AdditionalFor
ELSE NULL
END) , -- AdditionalChargeDesc - nvarchar(350)
A.SiteID , -- ScheduledSiteID - int
'V' , -- ChargedBy - char(1)
NULL , -- Weight - decimal
A.HaulierID , -- HaulierID - int
NULL , -- AllocationCode - nvarchar(50)
NULL , -- ConsignmentNumber - nvarchar(50)
NULL , -- PreNoteNumber - nvarchar(50)
NULL , -- Drums - int
NULL , -- HaulageCharge - decimal
NULL , -- Rate - decimal
0 , -- Manual - bit
NULL , -- Hours - decimal
NULL , -- CurrencyID - int
0 , -- Confirmed - bit
0 , -- Paid - bit
(SELECT TOP 1
VehicleTypeID
FROM dbo.Vehicles V
WHERE id = A.DefaultDeliveryVehicleID AND V.IsDeleted = 0) , -- VehicleTypeID - int
0 , -- QuotationID - int
0 , -- ContractID - int
(SELECT TOP 1
ScheduleTypeID
FROM dbo.ContainerTypes CT
WHERE id = A.ContainerTypeID AND CT.IsDeleted = 0) , -- ScheduledTypeID - int
A.TempField1 , -- TempField1 - nvarchar(250)
A.TempField2 , -- TempField2 - nvarchar(250)
NULL , -- PaymentID - int
(SELECT TOP 1
EWCID
FROM dbo.ProductCodes PC
WHERE id = A.ProductTypeID AND PC.IsDeleted = 0) , -- EWCID - int
NULL , -- DisposalCost - decimal
0 , -- Hold - bit
A.SalesPersonID , -- SalesID - int
NULL , -- Cost - decimal
0 , -- CostSpecial - bit
NULL , -- HaulageCost - decimal
0 , -- MobileStatus - int
1 , -- Scheduled - bit
0 , -- Complete - bit
NULL , -- SiteMobileStatus - int
0 , -- ContainerPriceID - int
0 , -- WeighbridgePriceID - int
A.Quantity , -- OriginalQuantity - int
NULL , -- MissedEmail - nvarchar(50)
GETDATE() , -- DateTimeLastUpdated - datetime
A.LastUpdatedBy , -- LastUpdatedBy - int
0 -- IsDeleted - bit
FROM dbo.Agreements A
WHERE id = @AgreementID AND A.IsDeleted = 0
END
SET @ExchangeDate = DATEADD(d,7*@Frequency,@ExchangeDate)
END
END
编辑:下面的代码只有在去掉注释行时才有效,否则还是报同样的错误,说明和子选择无关:
CREATE PROCEDURE [dbo].[test]
AS
BEGIN
INSERT INTO dbo.[Order]
(
AgreementID
-- TransType ,
-- StartDate ,
-- EndDate ,
-- JobNumber ,
-- AddressID ,
-- DeliveryAddressID ,
-- AreaID ,
-- ProductTypeID ,
-- InternalNoteID ,
-- CollectionTime ,
-- ContainerTypeID ,
-- DeliveryContainerID ,
-- CollectionContainerID ,
-- NetCharge ,
-- SpecialPrice ,
-- VehicleID ,
-- DisposalSiteID ,
-- DisposalCharge ,
-- DisposalTicket ,
-- OnRoad ,
-- Authorisation1 ,
-- Authorisation2 ,
-- Weighed ,
-- Printed ,
-- InvoiceNumber ,
-- InvoiceDate ,
-- WeighbridgeTransactionID ,
-- RouteID ,
-- RouteSequence ,
-- Quantity ,
-- Activity ,
-- ContactPerson ,
-- ContactTime ,
-- Notes1 ,
-- Notes2 ,
-- Notes3 ,
-- InvoiceMethodID ,
-- ScheduledDate ,
-- Extracted ,
-- AdditionalCharge ,
-- AdditionalChargeDesc ,
-- ScheduledSiteID ,
-- ChargedBy ,
-- [Weight] ,
-- HaulierID ,
-- AllocationCode ,
-- ConsignmentNumber ,
-- PreNoteNumber ,
-- Drums ,
-- HaulageCharge ,
-- Rate ,
-- [Manual] ,
-- [Hours] ,
-- CurrencyID ,
-- Confirmed ,
-- Paid ,
-- VehicleTypeID ,
-- QuotationID ,
-- ContractID ,
-- ScheduledTypeID ,
-- TempField1 ,
-- TempField2 ,
-- PaymentID ,
-- EWCID ,
-- DisposalCost ,
-- Hold ,
-- SalesID ,
-- Cost ,
-- CostSpecial ,
-- HaulageCost ,
-- MobileStatus ,
-- Scheduled ,
-- Complete ,
-- SiteMobileStatus ,
-- ContainerPriceID ,
-- WeighbridgePriceID ,
-- OriginalQuantity ,
-- MissedEmail ,
-- DateTimeLastUpdated ,
-- LastUpdatedBy ,
-- IsDeleted
)
VALUES (
1
-- AgreementID - int
-- '' , -- TransType - char(2)
-- GETDATE() , -- StartDate - datetime
-- GETDATE() , -- EndDate - datetime
-- 0 , -- JobNumber - int
-- 0 , -- AddressID - int
-- 0 , -- DeliveryAddressID - int
-- 0 , -- AreaID - int
-- 0 , -- ProductTypeID - int
-- 0 , -- InternalNoteID - int
-- GETDATE() , -- CollectionTime - datetime
-- 0 , -- ContainerTypeID - int
-- 0 , -- DeliveryContainerID - int
-- 0 , -- CollectionContainerID - int
-- 1.1 , -- NetCharge - decimal
-- 0 , -- SpecialPrice - bit
-- 0 , -- VehicleID - int
-- 0 , -- DisposalSiteID - int
-- 1.1 , -- DisposalCharge - decimal
-- N'' , -- DisposalTicket - nvarchar(30)
-- 0 , -- OnRoad - bit
-- N'' , -- Authorisation1 - nvarchar(50)
-- N'' , -- Authorisation2 - nvarchar(50)
-- 0 , -- Weighed - bit
-- 0 , -- Printed - bit
-- 0 , -- InvoiceNumber - int
-- GETDATE() , -- InvoiceDate - datetime
-- 0 , -- WeighbridgeTransactionID - int
-- 0 , -- RouteID - int
-- 0 , -- RouteSequence - int
-- 0 , -- Quantity - int
-- N'' , -- Activity - nvarchar(10)
-- N'' , -- ContactPerson - nvarchar(250)
-- GETDATE() , -- ContactTime - datetime
-- N'' , -- Notes1 - nvarchar(350)
-- N'' , -- Notes2 - nvarchar(350)
-- N'' , -- Notes3 - nvarchar(350)
-- 0 , -- InvoiceMethodID - int
-- GETDATE() , -- ScheduledDate - datetime
-- 0 , -- Extracted - bit
-- 1.1 , -- AdditionalCharge - decimal
-- N'' , -- AdditionalChargeDesc - nvarchar(350)
-- 0 , -- ScheduledSiteID - int
-- '' , -- ChargedBy - char(1)
-- 1.1 , -- Weight - decimal
-- 0 , -- HaulierID - int
-- N'' , -- AllocationCode - nvarchar(50)
-- N'' , -- ConsignmentNumber - nvarchar(50)
-- N'' , -- PreNoteNumber - nvarchar(50)
-- 0 , -- Drums - int
-- 1.1 , -- HaulageCharge - decimal
-- 1.1 , -- Rate - decimal
-- 0 , -- Manual - bit
-- 1.1 , -- Hours - decimal
-- 0 , -- CurrencyID - int
-- 0 , -- Confirmed - bit
-- 0 , -- Paid - bit
-- 0 , -- VehicleTypeID - int
-- 0 , -- QuotationID - int
-- 0 , -- ContractID - int
-- 0 , -- ScheduledTypeID - int
-- N'' , -- TempField1 - nvarchar(250)
-- N'' , -- TempField2 - nvarchar(250)
-- 0 , -- PaymentID - int
-- 0 , -- EWCID - int
-- 1.1 , -- DisposalCost - decimal
-- 0 , -- Hold - bit
-- 0 , -- SalesID - int
-- 1.1 , -- Cost - decimal
-- 0 , -- CostSpecial - bit
-- 1.1 , -- HaulageCost - decimal
-- 0 , -- MobileStatus - int
-- 0 , -- Scheduled - bit
-- 0 , -- Complete - bit
-- 0 , -- SiteMobileStatus - int
-- 0 , -- ContainerPriceID - int
-- 0 , -- WeighbridgePriceID - int
-- 0 , -- OriginalQuantity - int
-- N'' , -- MissedEmail - nvarchar(50)
-- GETDATE() , -- DateTimeLastUpdated - datetime
-- 0 , -- LastUpdatedBy - int
-- 0 -- IsDeleted - bit
)
END
【问题讨论】:
-
我复制了你的代码,添加了
USE MyDatabase语句,存储过程创建成功。 -
没有抱怨没有协议表吗?
-
作为旁注,我将选择中的所有代码都注释掉了,除了协议ID。在我删除注释代码之前,它仍然给出错误。一旦我在评论部分添加,我又遇到了错误。我让另一位开发人员尝试了这个,结果相同。
-
@connersz - 通常,视图和存储过程在实际运行之前不会以这种方式进行验证,因为在某些用例中表直到(不久之前)才存在。比如说,一个 sp,它创建一个包含一些数据的临时表,然后从中选择......你不需要
SELECT CASE ...中的SELECT。TOP 1没有ORDER BY是没有意义的——要么你有多行,需要先订购它们,要么你只有一行,并且可以进行常规连接。声明在 SQL Fiddle 中按原样工作(减去前导空格)。 -
@Clockwork-Muse 不管意见如何。无论如何,如果没有循环,它就无法工作。