Solved

Rewriting Stored Procedures as Functions

Posted on 2010-08-13
2
303 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
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

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.

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 …
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 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