Solved

SQL Server Named Parameters

Posted on 2010-11-10
9
592 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

810 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