Solved

Rewriting Stored Procedures as Functions

Posted on 2010-08-13
2
305 Views
Last Modified: 2012-05-10
expersts,  am reading about function. I don understand this
     Its primary purpose is to build intermediate results
plase, can you give me transact sql
0
Comment
Question by:enrique_aeo
[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
2 Comments
 
LVL 7

Accepted Solution

by:
bouscal earned 63 total points
ID: 33432002
A function will accept only an input parameter and has only one return value.

A stored procedure will accept input and output parameters and can have multiple output values.


The code below is from MSDN http://msdn.microsoft.com/en-us/library/ms187926.aspx

CREATE { PROC | PROCEDURE } [schema_name.] procedure_name [ ;number ] 
    [ { @parameter [ type_schema_name. ] data_type } 
        [ VARYING ] [ = default ] [ OUT | OUTPUT ] [READONLY]
    ] [ ,...n ] 
[ WITH <procedure_option> [ ,...n ] ]
[ FOR REPLICATION ] 
AS { [ BEGIN ] sql_statement [;] [ ...n ] [ END ] }
[;]

<procedure_option> ::= 
    [ ENCRYPTION ]
    [ RECOMPILE ]
    [ EXECUTE AS Clause ]

Open in new window

0
 
LVL 42

Assisted Solution

by:dqmq
dqmq earned 62 total points
ID: 33432165
>It's primary function is to build intermediate results

No, I don't think so...
A function is just some stored SQL that returns a single value.  For example, GETDATE() returns the current date and SYSTEM_USER() return the current login. Many functions accept arguments, for example UPPER(string) accepts a string argument and returns the same string converted to upper case.  

SQL server comes with many built-in functions, but also supports user defined functions (UDF's), so you can write your own.  You write those using TSQL  (or any CLR language).  For example, here's a simple function that uses the CAST AND DATEADD builtin functions to return tomorrows date.

Create Function dbo.GetTomorrow ()
Returns date
as
begin
return (dateadd(d,1,cast(cast(getdate() as int) as datetime)))
end
go

select getdate() as today, dbo.getTomorrow() as tomorrow

Generally speaking, stored procedures cannot be converted to functions because functions have many limitations and they return results a different way.  












0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
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.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

631 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