Solved

SQL Server Named Parameters

Posted on 2010-11-10
9
603 Views
Last Modified: 2012-06-27
Can named parameters be used in a function call as they can be in a stored procedure call?

Kevin
0
Comment
Question by:zorvek (Kevin Jones)
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 11

Expert Comment

by:David Kroll
ID: 34105920
sure.

ex:

CREATE FUNCTION [dbo].[function1]   ( @pvar1 varchar(30),  @pvar2 varchar(30), @pvar3 int )
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 34105927
Assuming you mean user defined functions, yes.
http://www.sqlteam.com/article/user-defined-functions
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34105986
NO

create function dbo.namedfun (@i int , @j int) returns int
as begin
return @i+@j
end

select dbo.namedfun (1, 2)
select dbo.namedfun (@i=1, @j=2)
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34105998
Welcome to SQL, Kevin.
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34106022
A more complete sample
One line will fail as indicated
use tempdb
GO
create function dbo.namedfun (@i int , @j int) returns int
as begin
return @i+@j
end
GO
select dbo.namedfun (1, 2) -- ok
GO
select dbo.namedfun (@i=1, @j=2) -- fail
GO
drop function namedfun
GO
create proc dbo.namedproc @i int , @j int
as
select @i * 10 + @j
GO
exec dbo.namedproc 1, 2 -- ok, result is 12
GO
exec dbo.namedproc @j=1, @i=2 -- ok, result is 21, not 12
GO
drop proc namedproc
GO

Open in new window

0
 
LVL 81

Author Comment

by:zorvek (Kevin Jones)
ID: 34106297
So we can't call a function with named arguments like we can with a stored procedure. And, from what I have learned since posting the question, ALL the parameters have to ALWAYS be passed.

Correct?

Kevin
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 34106373
Yes. Defaults are not available to functions.
Best bet to implement "defaults" is to have functions that call other functions.
(here add2 is a shorter form of add3 with param3 defaulted to 0)

create function add2(@a int, @b int) returns int as
begin
return dbo.add3(@a, @b, 0)
end
GO
0
 
LVL 81

Author Comment

by:zorvek (Kevin Jones)
ID: 34106416
cyberkiwi,

Kewl!

Sort of like overloading. I like.

Thanks!

Kevin
0

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question