473,395 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Some basic questions

I've not touched SQL server programming since 1999. I have very little
memory of it and need some clarifications on some basic questions that
I could even use a book for. Until I get myself a good book, someone
please help me with the answers:
1) What are SQL functions and how are they different from stored
procedures? Do both of the programming objects not achieve the same
thing? What was the need of having one in addition to the other?

2) How do we use an "if construct"/if clause within a SQL statement?
Can we use conditional checking with the if construct within a stored
procedure? Can you please post a trivial example of a stored procedure
with an if clause?

3) Stored procedures can have input parameters as well as output
parameters. Can they also have in/out parameters that are like "by
reference" parameters? What's the syntax on Microsoft's T-SQL version?

4) How does one check the return value of a stored procedure?
Thanks for helping out.

Apr 11 '06 #1
2 1497
Water Cooler v2 (wt*****@yahoo.com) writes:
1) What are SQL functions and how are they different from stored
procedures? Do both of the programming objects not achieve the same
thing? What was the need of having one in addition to the other?
A function is a much restricted module. A function may not change database
state, and therefore you cannot perform INSERT, UPDATE or DELETE statements
except on table variables (which are local to the function). Nor can you
call stored procedures, nor use dynamic SQL.

There are two main types of functions: scalar and table-valued. The latter
in their falls into inline and multi-statment. Inline functions consists
of a single query and are really parameterised views.

Functions comes in handy at times, but stored procedure remains the main
programming object. Scalar functions that accesses data is something to
be restrictive with, since if they appear in a SELECT, they can cause
serious performance degradataion.
2) How do we use an "if construct"/if clause within a SQL statement?
Can we use conditional checking with the if construct within a stored
procedure? Can you please post a trivial example of a stored procedure
with an if clause?
Within an SQL statement you use the CASE expression for conditional
checking. For control-of-flow in a stored procedure you use IF. For examples
on CASE, see Books Online.
3) Stored procedures can have input parameters as well as output
parameters. Can they also have in/out parameters that are like "by
reference" parameters? What's the syntax on Microsoft's T-SQL version?
Stored procedures has input parameters and input/output parameters. They
don't have output-only parameters like Ada. You cannot control whether a
value is passed by value of by reference. (And it would not be meaningful
in a client-server interface for that matter.)

Beware that for an OUTPUT parameter, the OUTPUT keyword must be specified
both for the formal parameter and the actual parameter.
4) How does one check the return value of a stored procedure?


EXEC @err = some_sp
SELECT @err = coalesce(nullif(@err, 0), @error)
IF @err <> 0
-- error handling.

Return values for stored procedures is normally only used to indicate
success/failure, with 0 meaning success, and everything else failure.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Apr 11 '06 #2
Thank you very much, Erland and SriSamp. Some extra bit of thanks to
Erland for taking out the time for custom-answers to my questions.

Apr 11 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steven O. | last post by:
First, this may not be the correct newsgroup. I have some relatively basic questions on SQL. I tried to find a newsgroup that was specifically just about SQL, and was surprised to find that all...
7
by: Steven O. | last post by:
I am basically a hobbyist programmer, at the moment doing a little work experimenting with some AI stuff. I learned C++, and then tried to teach myself MFC using MS Visual C++ 6.0. I swore off of...
12
by: Vibhajha | last post by:
Hi friends, My sister is in great problem , she has this exam of C++ and she is stuck up with some questions, if friends like this group provides some help to her, she will be very grateful....
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
1
by: danilo of TUP | last post by:
Greetings, im Danilo, a student from TUP Manila and taking up COMPUTER SCIENCE. we are going to have a defense or we could say a project in turbo c. i just wanna know how to use the SWITCH...
3
by: aziz001DETESTSPAM | last post by:
I've been following some tutorials and reading books and have a basic grasp of the fundamental ADO.NET commands. But all the books I've read use a database with only 1 table. My database has many,...
4
by: Peter Morris [Droopy eyes software] | last post by:
Hi all, I have some basic WebService questions #1 Let's say I have a business class named "Customer". The customer would be able to log in and update their personal information like so void...
0
by: software2006 | last post by:
ASP And Visual Basic Interview questions and answers I have listed over 100 ASP and Visual Basic interview questions and answers in my website...
3
by: alcapoontje | last post by:
hi there, i'm already searching the internet for a couple of days to find an answer on this (simple) questions but i couln't find anything usefull. - how to quit and open a programm like "word" with...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.