dcsimg
Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: Enterprise
Expertise: Beginner
Oct 17, 2001

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Using SQL Server 2000 User-Defined Functions to Retrieve Table-Like Data


In the SQL Server 7 days, when we wanted to calculate data and return it, we usually used stored procedures. User-defined functions are a new feature of SQL Server and they are much better as their results can be used in the 'From' clause of a 'Select' statement. Imagine that you wanted to retrieve data from the Shoes or Shirts tables in order to determine their stock in a flexible way.
 
Create Function SomeTable(@Name char(6))
Returns @recordsExtracted Table (quantityInStock)
As
Begin
If @Name='Shoes' Then
Insert @recordsExtracted Select quantityInStock From Shoes
If @Name='Shirts' Then
Insert @recordsExtracted Select quantityInStock From Shirts
Return
End

As you can see, logic and returned recordset in one place. To use this function, you would do the following:
 
Select Sum(quantityInStock) From SomeTable('Shoes') , or,
Select Sum(quantityInStock) From SomeTable('Shirts')
Jorge Silva
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date