X
X
xXNullXx2020-04-21 23:30:03
SQL
xXNullXx, 2020-04-21 23:30:03

How to transfer to procedure parameter result of request which in procedure?

Good day!

I have a procedure that adds "Organization"...

CREATE PROCEDURE AddOrganization
  @NameOrg NVARCHAR(50),
  @StatusId INT,
  @Email NVARCHAR(50) = NULL,
  @PhonesNumbers nvarchar(12) = NULL,
  @IdOrganizations INT = null
AS
  INSERT INTO Organizations([Name], StatusId)
  VALUES(@NameOrg, @StatusId)

...and in the future this procedure will add the emails and phone numbers of this organization. But for this you need to know the "Id" of this organization. This can be done with queries like this:
WITH res AS
(
  SELECT TOP (1) Id
  FROM Organizations
  ORDER BY Id DESC
)
SELECT * FROM res

But I don't know how to pass this value to the "IdOrganizations" parameter (that is, so that it happens inside the procedure) (that is, so that it happens inside the procedure) that is in the "AddOrganization" procedure. How to implement?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Tsvetkov, 2020-04-22
@xXNullXx

CREATE PROCEDURE AddOrganization
  @NameOrg NVARCHAR(50),
  @StatusId INT,
  @Email NVARCHAR(50) = NULL,
  @PhonesNumbers nvarchar(12) = NULL,
  @IdOrganizations INT = NULL  OUTPUT
AS
  INSERT INTO Organizations ( [Name], StatusId )  VALUES ( @NameOrg, @StatusId )
  SELECT @IdOrganizations = MAX( ID ) FROM Organizations -- Это вместо TOP(1)
  SET @IdOrganizations = IDENT_CURRENT ('Organizations') -- Это если Organizations.ID у вас IDENTITY

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question