How to make parameter value optional in stored procedure

I would like this sp to return all rows where the surname = @name and allow the user to enter specific first name(@Gname or not. if user enters a first name then return all rows that match firstname surname else return all rows where surname matches name.


create proc spHPI

@Gname varchar(30),
@Name varchar(30)
as
      select
      n.[given name],
      n.surname,
      n.[professional ID],
      p.[Common Person Number],
      a.street_address,
      a.city,
      q.qualification
      from name n
      left    outer join address a  on n.[professional id]= a.[professional id]      
            join Professional p   on n.[Professional ID] = p.[Professional ID]
            join qualification q   on q.[Professional ID]= p.[Professional ID]
      where n.surname = @Name
      and n.[given name] = @Gname
      order by n.[given name]
deNZityAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DimitrisSenior Solution ArchitectCommented:
here u r
create proc spHPI
@Name varchar(30),
@Gname varchar(30) = null,
as
      select
      n.[given name],
      n.surname,
      n.[professional ID],
      p.[Common Person Number],
      a.street_address,
      a.city,
      q.qualification
      from name n
      left    outer join address a  on n.[professional id]= a.[professional id]      
            join Professional p   on n.[Professional ID] = p.[Professional ID]
            join qualification q   on q.[Professional ID]= p.[Professional ID]
      where n.surname = @Name
      and n.[given name] = isnull(@Gname, n.[given name])
      order by n.[given name]
 
 
 
/* You can call it like 
exec spHPI 'dimitris' or exec spHPI @Name ='dimitris' 
or
exec spHPI 'dimitris','FromGreece' or exec spHPI @Name ='dimitris', @gname ='FromGreece'
 
The second parameter (@gname has a default value of null 
so ig you don't pass  it (first exec method) it uses the null 
 
*/

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
deNZityAuthor Commented:
Thanks I changed it a bit but just what I wanted.

create proc spHPI
@Name varchar(30),
@Gname varchar(30) = null
as
      select
      n.[given name],
      n.surname,
      n.[professional ID],
      p.[Common Person Number],
      a.street_address,
      a.city,
      q.qualification
      from name n
      left    outer join address a  on n.[professional id]= a.[professional id]      
            join Professional p   on n.[Professional ID] = p.[Professional ID]
            join qualification q   on q.[Professional ID]= p.[Professional ID]
      where n.surname like @Name + '%'
      and n.[given name] like isnull(@Gname + '%', n.[given name])
      order by n.[given name]
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.