简体   繁体   中英

How to set an output parameter that is the combination of two fields, e.g., fullname=firstname+lastname?

I'm trying to create a stored procedure that would return the full name of a customer based on their ID as the sole input parameter. The full name would be the first name + the last name contained in the table. I'm pretty sure there's something (a lot) wrong with my code, though... Can you help me spot the problem, please? Thanks!

alter procedure udemy
@businessID int,
@firstname varchar(max) output,
@lastname varchar(max) output,
@fullname varchar(max) output
as
begin
select firstname,lastname
from person.Person
where @businessID=BusinessEntityID and
@firstname=FirstName and
@lastname=LastName
set @fullname=@firstname+@lastname
end

a stored procedure that would return the full name of a customer based on their ID

Simply assign it to the variable

alter procedure udemy
@businessID int,
@fullname varchar(max) output
as
begin
  select @fullname=firstname + lastname -- Retuen Full Name
  from person.Person
  where @businessID=BusinessEntityID --Based on ID
end

Then call your SP as

DECLARE @FullName VARCHAR(MAX); --Declare the OUTPUT parameter

EXEC dbo.udemy 1, @FullName OUTPUT; --Pass an ID and the OUT parameter

SELECT @FullName; --See the results

So, there is no need to the other two parameters, unless you want to return the FirstName and the LastName too.

Also, why you use them in the WHERE clause since you have the ID ? in the same time you specify OUTPUT for both.


If you don't want to use the OUTPUT parameter then

alter procedure udemy
@businessID int
as
begin
  select firstname + lastname -- Retuen Full Name
  from person.Person
  where @businessID=BusinessEntityID --Based on ID
end

You can do all the assignment you need in the select :

alter procedure udemy (
    @businessID int,
    @firstname varchar(max) output,
    @lastname varchar(max) output,
    @fullname varchar(max) output
) as
begin
    select @firstname = firstname,
           @lastname = lastname,
           @fullname = @firstname+@lastname
    from person.Person p
    where BusinessEntityID = @businessID;
end;

You can try the following query and you need to write output parameter in select as shown below.

Create Table Person (BusinessEntityID int, firstname varchar(10), lastname varchar(10))
insert into Person Values (1, 'Suraj', 'Kumar'),(2, 'Deepak', 'Kumar')
go
Create procedure udemy
@businessID int,
@fullname varchar(max) output
as
begin
  select @fullname=firstname + lastname
  from Person
  where BusinessEntityID = @businessID
end
go
DECLARE @get VARCHAR(20);
EXEC udemy 1,@get output

SELECT @get

The output is as SurajKumar You can find the live demo Live Demo Here

you can use CONCAT why go through all complications.

CONCAT(First_Name, '*space*', Midle_Name,'space', Last_Name) AS Full_Name

This is your query corrected

alter procedure udemy
@businessID int,
@firstname varchar(max) output,
@lastname varchar(max) output,
@fullname varchar(max) output
as
begin
select CONCAT(firstname,' ',lastname) AS Fll_Name --Here you go
from person.Person
where @businessID=BusinessEntityID and
@firstname=FirstName and
@lastname=LastName
set @fullname=@firstname+@lastname
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM