Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server ASP
|
SQL Server ASP Discussions about ASP programming with Microsoft's SQL Server. For more ASP forums, see the ASP forum category.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server ASP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 1st, 2004, 06:37 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 249
Thanks: 0
Thanked 0 Times in 0 Posts
Default Store Procedure IF Else Newbie Question

Can somebody please help me out here. I am trying to write some code that will determine whether to INSERT or UPDATE depending on if the @StoryID has a value or not. I have tried
@StoryID = NULL , IS NULL = "" just about everything I can think of. What is happening is that it always INSERTS, it will not update. Any help is greatly appreciated.


CREATE PROCEDURE sp_ModifyStories
(
@StoryID int,
@StoryDate datetime,
@StoryNo varchar(100),
@StoryTitle varchar(200),
@ModifiedID int OUTPUT
)
AS
IF @StoryID < 1
    BEGIN
    INSERT INTO Stories
    (Story_Date, Story_No, Story_Title)
    VALUES
    (@StoryDate, @StoryNo, @StoryTitle)

    SELECT @ModifiedID = @@IDENTITY
    END
ELSE
    BEGIN
    UPDATE Stories
    SET
        Story_Date = @StoryDate,
        Story_No = @StoryNo,
        Story_Title = @StoryTitle,
    WHERE Story_ID = @StoryID
    END
GO

Thanks
MIke
__________________
Peace
Mike
http://www.eclecticpixel.com
 
Old November 1st, 2004, 08:31 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Try this...
Code:
......
AS
BEGIN TRAN
IF (select count(*) from Stories where Story_ID = @StoryID)=0
    BEGIN
    INSERT INTO Stories
    (Story_Date, Story_No, Story_Title) 
    VALUES
    (@StoryDate, @StoryNo, @StoryTitle)
    SELECT @ModifiedID = @@IDENTITY
    END
ELSE
    BEGIN
    UPDATE Stories
    SET
        Story_Date = @StoryDate,
        Story_No = @StoryNo,
        Story_Title = @StoryTitle,
    WHERE Story_ID = @StoryID
    END
IF @@ERROR <> 0
    ROLLBACK TRAN
ELSE
    COMMIT TRAN
Select Result=@@ERROR
Go
Hope that helps.
Cheers!

_________________________
- Vijay G
Strive for Perfection





Similar Threads
Thread Thread Starter Forum Replies Last Post
Store Procedure For Attendance prasanta2expert SQL Server 2000 4 July 5th, 2012 03:10 AM
Store procedure help... RinoDM SQL Server 2000 7 August 11th, 2008 07:09 PM
Store procedure help ??? RinoDM SQL Server 2000 8 May 1st, 2008 03:03 PM
Store Procedure sureshyuga SQL Server 2000 0 May 18th, 2007 01:49 AM
How do I Call store procedure kau_shuk VS.NET 2002/2003 1 September 7th, 2006 08:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.