I'm trying to update/insert a SQL table using a stored procedure. Its inputs are a DataTable and other individual parameters.
EmployeeDetails
table:
ID | Name | Address | Operation | Salary
---+-------+------------+-------------+------------
1 | Jeff | Boston, MA | Marketing | 95000.00
2 | Cody | Denver, CO | Sales | 91000.00
Syntax for user-defined table type (DataTable):
CREATE TYPE EmpType AS TABLE
(
ID INT,
Name VARCHAR(3000),
Address VARCHAR(8000),
Operation SMALLINT
)
Procedure for the operation:
ALTER PROCEDURE spEmpDetails
@Salary Decimal(10,2),
@Details EmpType READONLY
AS
BEGIN
UPDATE e
SET e.Name = d.Name,
e.Address = d.Address
FROM EmployeeDetails e, @Details d
WHERE d.ID = e.ID
--For inserting the new records in the table
INSERT INTO EmployeeDetails(ID, Name, Address)
SELECT ID, Name, Address
FROM @Details;
END
This procedure spEmpDetails
gets its inputs as individual parameter @Salary
and a DataTable @Details
. Using these inputs, I'm trying to update/unsert the EmployeeDetails
table. But, I failed to join these inputs together in the update/insert statement. In the above code, I'm only using the @Details
DataTable data to update the EmployeeDetails
table and I'm missing the @Salary
to update in the table.
I'm looking for some suggestions on how to do it. Any suggestion will be greatly appreciated.