I defined the next Data Type:
CREATE TYPE dbo.FieldsTable AS TABLE
(
[Name] nvarchar(255),
[Value] nvarchar(255)
)
I want to declare a stored procedure with this sign:
CREATE PROCEDURE dbo.uspSaveEntity
(
@Id int,
@Fields [FieldsTable] readonly
)
AS
...
This procedure should receive a FieldsTable type with the list of fields and values that I need to set in the UPDATE of the entity.
For example, to call the procedure I want to do somnething like:
DECLARE @entityFields FieldsTable;
INSERT INTO @entityFields VALUES ('FirstName', 'John');
INSERT INTO @entityFields VALUES ('LastName', 'Doe');
EXEC uspSaveEntity 1500, @entityFields
The question is: How I can extract the parameters from the table I receive?
Until now I was thinking in something like:
SET @FirstName = (SELECT Value FROM @Fields WHERE Name = 'FirstName');
SET @LastName = (SELECT Value FROM @Fields WHERE Name = 'LastName');
...
UPDATE tblEntity SET FirstName = @FirstName ...
I think using SET and SELECT for each field is too much code maybe... exist some approche more clear to do this?
The UPDATE query and all the stored procedure dont need to be dynamic, but needs to support have this particular way to receive parameters.