1
Is there any way I can make a SP that receives an object? This is my SP
ALTER PROCEDURE [dbo].[sp_alt_funcionarios]
-- Add the parameters for the stored procedure here
(
@id int,
@nome varchar(60)
,@dataNascimento DateTime
,@cpf bigint
,@cidade int
)
AS
BEGIN
---- SET NOCOUNT ON added to prevent extra result sets from
SET NOCOUNT ON;
-- Insert statements for procedure here
update funcionarios set
nome = @nome
,dataNascimento = @dataNascimento
,cpf = @cpf
,cidade = @cidade
where
id = @id
END
and this is my service that uses this SP
public class PutFuncionario
{
BancoContext banco = new BancoContext();
public HttpResponseMessage updateFuncionario(Funcionario funcionario)
{
banco.Database.ExecuteSqlCommand("exec sp_alt_cidade @id, " +
"@nome," +
"@dataNascimento, " +
"@cpf, " +
"@cidade",
new SqlParameter("@id", funcionario.id),
new SqlParameter("@nome", funcionario.nome),
new SqlParameter("@dataNascimento", funcionario.dataNascimento),
new SqlParameter("@cpf", funcionario.cpf),
new SqlParameter("@cidade", funcionario.cidade));
return new HttpResponseMessage(HttpStatusCode.OK);
}
instead of me going like this: employee id, functionary.name and etc Pass the whole function object? It is more elegant and etc
Not.
– Jéf Bueno
There is a way: you can serialize the object in XML and pass as argument to an SP. See article: https://www.itworld.com/article/2960645/development/tsql-how-to-use-xml-parameters-in-stored-procedures.html
– William John Adam Trindade
It is also possible through the use of table-Valued Parameter (TVP). Suggested reading: "Using Table-Valued Parameters in SQL Server" -> http://sommarskog.se/arrays-in-sql-2008.html
– José Diz
Note that, an object like you say, is something that exists in a language, for the bank does not make much sense, unless it is a standard format, like
xml
orjson
, but this does not make it "more elegant". The Procedure was created to receive parameters and thus works very well. Passing objects in programming languages has other concepts involved, objects can be passed by reference, they can have behaviors, etc., things that don’t make any sense to a precedent. Just pointing out that, although it is possible, with this reduced number of parameters does not make much sense– Ricardo Pontual