This question follows from this one.
The following SQL works:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Update_Repair_Details]
@RepairID BIGINT,
@NewDetails NewDetails READONLY
AS
BEGIN
SET NOCOUNT ON;
DELETE FROM Repair_Details
WHERE RepairID = @RepairID
INSERT INTO Repair_Details
SELECT *, GETDATE()
FROM @NewDetails
END
But since RepairID
is the first column in the user-defined table type, there is no reason to pass it as an additional parameter.
Thus I wrote:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Update_Repair_Details]
@NewDetails NewDetails READONLY
AS
BEGIN
SET NOCOUNT ON;
DELETE FROM Repair_Details
WHERE RepairID = @NewDetails.RepairID
INSERT INTO Repair_Details
SELECT *, GETDATE()
FROM @NewDetails
END
which causes an error:
Must declare the scalar variable "@NewDetails"
Why does this have the error while the previous version does not?
In this case,
@NewDetails
is a table; as such, you can't just doWHERE RepairID = @NewDetails.RepairID
. You can useIN
,EXISTS
or aJOIN
: