473,748 Members | 7,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1516
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****@sommarsk og.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
5188
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 the SQL-related newsgroups seem to be product related. But if I missed something, and someone can steer me to a correct newsgroup, please do so. My specific questions: 1. I want to put comments in an SQL script. For example, I want
7
2585
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 MFC, which was a nightmare, and have been playing with Borlands C++ Builder. In C++ Builder, creating forms and other GUI elements is much simpler than MFC, very similar to Visual Basic. In the store yesterday, I was looking at the C++ .Net...
12
2329
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. These are some questions:- 7. design and implement a class binsearch for a binary search tree.it includes search,remove and add options.make suitable assumption. 8.explai how pointers to functions can be declared in c++.under what conditions can...
193
9612
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 speak only of features in the spirit of C; something like object-orientation, though a nice feature, does not belong in C. Something like being able to #define a #define would be very handy, though, e.g: #define DECLARE_FOO(bar) #define...
1
1628
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 FUNCTION cause we really need it badly. about the array in the interger type variables, what if i put this: int num; then print some statements so that the user can put some values
3
1405
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, so: 1. I know there is 1 OleDbConnectino object variable per database file, but is there one dataAdapter per table/query? (e.g. my tables: tblCustomer, tblProducts, tblPrices etc etc, so should I have dataAdapters such as these?...
4
1368
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 UpdateCustomerInformation(Customer customer) changing information such as their telephone number etc. Now the customer class would have information in it such as "Decimal CreditLimit". This
0
541
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 http://www.geocities.com/myinterviewquestions/ASPAndVisualBasic.htm So please have a look and make use of it.
3
1409
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 a trigger in visual basic? - can you run your programms without opening excell first(because i'm working with vb in excell). My brother said something of .exe files but he didn't knew how to do that in visual basic because he works with java. ...
0
8989
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8828
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9537
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9367
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8241
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6795
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.