Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stored Procedure Question 1

Status
Not open for further replies.

darude

Programmer
Jun 23, 2003
138
US
Hi All,
I am new stored procedures and want to write one that:

First, check to see if a Contact already exists in our data

If not, then I want to do an append query

If it does, then I want to send back a message that "contact already exists"

Thank you in advance.

 
Is the stored procedure going to be used in an application, or is it something that is going to be executed from a Query Analyzer/Management Studio query window?
 
It will be called from a MS Access application.
 
Well, something like the following would work:
Code:
CREATE PROCEDURE AddContract
@ContractNumber INT
AS

IF EXISTS (SELECT 1 FROM Contracts WHERE ContractNumber = @ContractNumber)
  BEGIN
    SELECT 0 AS RowsInserted
  END
ELSE
  BEGIN
    INSERT INTO Contracts (ContractNumber) SELECT @ContractNumber
    SELECT 1 AS RowsInserted
  END

Then, if you're calling this sproc from VBA within Access, the pseudocode would look something like:
Code:
Dim RowsInserted As Integer
RowsInserted = <scalar result from your stored procedure>
If RowsInserted = 0 Then
  MsgBox("This contract already exists")
End If
 
Just a dummy value to tell you how many records were inserted. If this was an update stored procedure, you could use the @@RowCount statement to get the number of rows updated. However, since your stored procedure is probably going to be inserting just one record at a time, selecting the constant 1 will suffice.
 
Okay. So I hate to bother you but. Now I want to get the staffID of the current user to add to the INSERT. So, here's my stored proc.



ALTER PROCEDURE procAppendToStudMR
@StudentID varchar(10)
AS
IF EXISTS
(SELECT StudMRStudentID FROM StudMR WHERE StudMRStudentID = @StudentID)
BEGIN
SELECT 0 as RowInserted
END
ELSE
BEGIN

INSERT INTO StudMR (StudMRStudentID, StudMRFirstname, StudMRLastname, StudMRStaffID)
SELECT StudentID, Firstname, Lastname, ArchiveSchoolID,
FROM vw_ArchiveStudents
WHERE StudentID = @StudentID
SELECT 1 as RowInserted
END
 
Where is the StaffID stored? If it is in a table other than vw_ArchiveStudents, you could do something like
Code:
DECLARE @StaffID INT
SELECT @StaffID = StaffID FROM SomeTable WHERE User = @CurrentUser

Then, use it in your INSERT statement
Code:
     INSERT INTO StudMR (StudMRStudentID, StudMRFirstname,  StudMRLastname, StudMRStaffID)                                                                                                                                         
    SELECT StudentID, Firstname,     Lastname, ArchiveSchoolID,  @StaffID       
     FROM vw_ArchiveStudents
     WHERE StudentID = @StudentID
 
Yes. It is stored in the Staff table. So I would
get the currentuser based on Staff.Username to get
back the Staff.Staffid.
 
The accepted syntax for EXISTS() is to use SELECT *, not SELECT 1.
 
ESquared said:
The accepted syntax for EXISTS() is to use SELECT *, not SELECT 1

Accepted by who? I don't remember getting a memo on the subject.
 
RiverGuy,

I didn't mean to step on your toes.

In any case, thanks to Borislav and some further research, I stand corrected!

It is better to use SELECT 1 than SELECT * because SELECT * makes the query compilation just a little more expensive by making it expand * into all the columns in the table, then it throws the column names away.

EXISTS Subqueries: SELECT 1 vs. SELECT *
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top