?
Solved

writing update store procedure

Posted on 2005-05-02
4
Medium Priority
?
281 Views
Last Modified: 2010-03-19
Is this the proper format for writing an update store procedure?

USE [mydatabasetable]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[sp_VUpdate]') and OBJECTPROPERTY(id, N'IsProcedure') =
1)
drop procedure [sp_VUpdate]
GO


SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

Create Procedure sp_VUpdate
    (
      @strSupplierID int
      @strSupplierName  varchar(50),
      @strContactName   varchar(50),
      @strPhone  varchar(50),
      @strFax  varchar(100),
      @strCell  varchar(100),
      @strAddress  varchar(50),
      @strCity  varchar(50),
      @strSoP  varchar(50),
      @strZip  varchar(50),
      @strActive real,
      @strRateing real
    )
As
If Exists (Select * From Vendors WHERE SupplierID = @strSupplierID)
      Update Vendors Set SupplierName = @strSupplierName
      ContactName = @strContactName
      Address = @strAddress
      City = @strCity
      StateOrProvince = @strSoP
      PostalCode = @strZip
      PhoneNumber= @strPhone
      FaxNumber = @strFax
      CellPhone = @strCell
      Active = @strActive
      Rateing = @strRateing
      FROM  Vendors
      WHERE Vendors.Supplierid = @strSupplierID
            SELECT @@identity AS SupplierID
            SET NOCOUNT OFF
            Go
ELSE
      INSERT INTO Vendors(SupplierName, ContactName, Address, City, StateOrProvince,
      PostalCode, PhoneNumber, FaxNumber, CellPhone)
      values(@strSupplierName, @strContactName, @strAddress, @strCity, @strSoP,
      @strZip, @strPhone, @strFax, @strCell)
            SELECT @@identity AS SupplierID
            SET NOCOUNT OFF
            Go
End If

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

GRANT  EXECUTE  ON [sp_VUpdate]  TO [myuserid]
GO
0
Comment
Question by:matthersjr
  • 3
4 Comments
 
LVL 28

Expert Comment

by:rafrancisco
ID: 13912231
Your sp looks good.  Here are just my comments:

1.  In the Update portion, this part SELECT @@identity AS SupplierID will not yield anything because you did not do an insert but an update.
2.  Remove the GO after the SELECT @@identity AS SupplierID on both the INSERT and the Update.
0
 
LVL 28

Expert Comment

by:rafrancisco
ID: 13912252
If you want to return the SupplierID, for the update part use this:

SELECT @strSupplierID  AS SupplierID

For the INSERT part, use this:

SELECT SCOPE_IDENTITY() AS SupplierID
0
 

Author Comment

by:matthersjr
ID: 13912571
Here is what I did and what I got in return:

USE [mydatabasetable]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[sp_VUpdate]') and OBJECTPROPERTY(id, N'IsProcedure') =
1)
drop procedure [sp_VUpdate]
GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

Create Procedure sp_VUpdate
    (
     @strSupplierID int
     @strSupplierName  varchar(50),
     @strContactName   varchar(50),
     @strPhone  varchar(50),
     @strFax  varchar(100),
     @strCell  varchar(100),
     @strAddress  varchar(50),
     @strCity  varchar(50),
     @strSoP  varchar(50),
     @strZip  varchar(50),
     @strActive real,
     @strRateing real
    )
As
If Exists (Select * From Vendors WHERE SupplierID = @strSupplierID)
     Update Vendors Set SupplierName = @strSupplierName
     ContactName = @strContactName
     Address = @strAddress
     City = @strCity
     StateOrProvince = @strSoP
     PostalCode = @strZip
     PhoneNumber= @strPhone
     FaxNumber = @strFax
     CellPhone = @strCell
     Active = @strActive
     Rateing = @strRateing
     FROM  Vendors
     WHERE Vendors.Supplierid = @strSupplierID
      SELECT @strSupplierID  AS SupplierID
ELSE
     INSERT INTO Vendors(SupplierName, ContactName, Address, City, StateOrProvince,
     PostalCode, PhoneNumber, FaxNumber, CellPhone)
     values(@strSupplierName, @strContactName, @strAddress, @strCity, @strSoP,
     @strZip, @strPhone, @strFax, @strCell)
      SELECT SCOPE_IDENTITY() AS SupplierID

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

GRANT  EXECUTE  ON [sp_VUpdate]  TO [myuserid]
GO

-------------------

Errors:

Server: Msg 156, Level 15, State 1, Procedure sp_VUpdate, Line 26
Incorrect syntax near the keyword 'ELSE'.
Server: Msg 208, Level 16, State 11, Line 3
Invalid object name 'sp_VUpdate'.
0
 
LVL 28

Accepted Solution

by:
rafrancisco earned 2000 total points
ID: 13912602
You forgot to put the code in BEGIN ... END block, (which I was not able to notice earlier):

USE [mydatabasetable]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[sp_VUpdate]') and OBJECTPROPERTY(id, N'IsProcedure') =
1)
drop procedure [sp_VUpdate]
GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

Create Procedure sp_VUpdate
    (
     @strSupplierID int
     @strSupplierName  varchar(50),
     @strContactName   varchar(50),
     @strPhone  varchar(50),
     @strFax  varchar(100),
     @strCell  varchar(100),
     @strAddress  varchar(50),
     @strCity  varchar(50),
     @strSoP  varchar(50),
     @strZip  varchar(50),
     @strActive real,
     @strRateing real
    )
As
If Exists (Select * From Vendors WHERE SupplierID = @strSupplierID)
BEGIN
     Update Vendors Set SupplierName = @strSupplierName
     ContactName = @strContactName
     Address = @strAddress
     City = @strCity
     StateOrProvince = @strSoP
     PostalCode = @strZip
     PhoneNumber= @strPhone
     FaxNumber = @strFax
     CellPhone = @strCell
     Active = @strActive
     Rateing = @strRateing
     FROM  Vendors
     WHERE Vendors.Supplierid = @strSupplierID
     SELECT @strSupplierID  AS SupplierID
END
ELSE
BEGIN
     INSERT INTO Vendors(SupplierName, ContactName, Address, City, StateOrProvince,
     PostalCode, PhoneNumber, FaxNumber, CellPhone)
     values(@strSupplierName, @strContactName, @strAddress, @strCity, @strSoP,
     @strZip, @strPhone, @strFax, @strCell)
     SELECT SCOPE_IDENTITY() AS SupplierID
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

GRANT  EXECUTE  ON [sp_VUpdate]  TO [myuserid]
GO
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

864 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