Solved

SQL Server Named Parameters

Posted on 2010-11-10
9
607 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

688 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