Solved

SQL Server Named Parameters

Posted on 2010-11-10
9
585 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)
9 Comments
 
LVL 11

Expert Comment

by:David Kroll
Comment Utility
sure.

ex:

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

Expert Comment

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

Expert Comment

by:cyberkiwi
Comment Utility
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
 
LVL 58

Expert Comment

by:cyberkiwi
Comment Utility
Welcome to SQL, Kevin.
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 58

Expert Comment

by:cyberkiwi
Comment Utility
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)
Comment Utility
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
Comment Utility
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)
Comment Utility
cyberkiwi,

Kewl!

Sort of like overloading. I like.

Thanks!

Kevin
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now