Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Rewriting Stored Procedures as Functions

Posted on 2010-08-13
2
Medium Priority
?
307 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 252 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 248 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

In this article I will describe the Detach & Attach 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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

705 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