I am setting up a database that will hold a transaction ticket. The transaction ticket is stored via a stored procedure.
The transaction item table has the following foreign keys:
Driver Id
Producer Id
Carrier Id
I would like to store the following based by using "SELECT" and "INSERT INTO" by referencing the corresponding foreign keys, ie. dbo.Driver DriverId
"dbo.Driver DriverFisrtName"
"dbo.Driver DriverLastName"
"dbo.Producer ProducerName"
"dbo.Carrier CarrierName"
The problem I am having is understanding the best method to perform this inside of a stored procedure.
ALTER PROCEDURE [dbo].[TransactionTicket]
@EstBarrels INT,
@DrvierId INT,
@CarrierId INT,
@ProducerId INT,
@LaneId INT,
@StartTime VARCHAR(50),
@StartDate VARCHAR(50),
@EndTime VARCHAR(50),
@EndDate VARCHAR(50),
@MeterGsvStart REAL,
@MeterGsvStop REAL,
@MeterNsvStart REAL,
@MeterNsvStop REAL,
@TransactionNetTotal REAL,
@TransactionGsvTotal REAL,
@AvgTemp REAL,
@UserTicket INT,
@Density REAL,
@MeterFactor REAL,
@AvgBsw REAL,
@Id INT OUTPUT,
@FirstName VARCHAR(50),
@LastName VARCHAR(50),
@ProducerName VARCHAR(50),
@CarrierName VARCHAR(50)
AS
BEGIN
SET NOCOUNT ON;
--Declare @DriverId INT
INSERT INTO @FirstName
SELECT FirstName
FROM dbo.driver
WHERE DriverId = @DrvierId ;
INSERT INTO @LastName
SELECT LastName
FROM dbo.driver
WHERE DriverId = @DrvierId ;
INSERT INTO @ProducerName
SELECT ProducerName
FROM dbo.Producer
WHERE ProducerId = @ProducerId ;
INSERT INTO @CarrierName
SELECT CarrierName
FROM dbo.Carrier
WHERE CarrierId = @CarrierId ;
-- Insert statements for procedure here
INSERT INTO TransactionItem (DriverId, CarrierId, ProducerId, LaneId,
AverageTemp, MeterStartGSV,
MeterStopGSV, MeterStartNSV, MeterStopNSV,
TotalBarrelsGSV, TotalBarrelsNSV, EstimatedBarrels,
Density, MeterFactor, StartTime, StartDate,
EndTime, EndDate, AvgBsw, FirstName, LastName, ProducerName, CarrierName)
VALUES (@DrvierId, @CarrierId, @ProducerId, @LaneId, @AvgTemp,
(@MeterGsvStart/100), (@MeterGsvStop/100), (@MeterNsvStart/100),
(@MeterNsvStop/100), (@TransactionGsvTotal/100),
(@TransactionNetTotal/100), @EstBarrels, @Density, @MeterFactor,
@StartTime, @StartDate, @EndTime, @EndDate, @AvgBsw,
@FirstName, @LastName, @ProducerName, @CarrierName);
SET @Id = SCOPE_IDENTITY()
RETURN @Id
END
Here are the errors I get:
Msg 1087, Level 16, State 1, Procedure TransactionTicket, Line 54
Must declare the table variable "@FirstName".Msg 1087, Level 16, State 1, Procedure TransactionTicket, Line 56
Must declare the table variable "@LastName".Msg 1087, Level 16, State 1, Procedure TransactionTicket, Line 58
Must declare the table variable "@ProducerName".Msg 1087, Level 16, State 1, Procedure TransactionTicket, Line 60
Must declare the table variable "@CarrierName".
insert into @blahshould beselect @blah=blah from dbo.blahblah