Solved

Rewriting Stored Procedures as Functions

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

14 Experts available now in Live!

Get 1:1 Help Now