If exists SP needs work

Can somebody show me the correct way to write this SP?

IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID(N'[dbo].[usp_LiveCoinAddAllOrderBookReturnID]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[usp_LiveCoinAddAllOrderBookReturnID];
GO
CREATE PROCEDURE [dbo].[usp_LiveCoinAddAllOrderBookReturnID]
(
		   @ExchangeID INT,
		   @Symbol NVARCHAR(128),           
		   @LiveCoinAllOrderBookID INT OUTPUT
)
AS
BEGIN
IF NOT EXISTS (SELECT * FROM [dbo].[LiveCoinAllOrderBook] WHERE [symbol] = @Symbol)
INSERT INTO [dbo].[LiveCoinAllOrderBook]
(
		
		   [ExchangeID],
		   [Symbol]
)
VALUES
(
		   @ExchangeID,
           @Symbol
)

SELECT @LiveCoinAllOrderBookID = @@IDENTITY

ELSE

SELECT LiveCoinAllOrderBookID FROM [dbo].[LiveCoinAllOrderBook] WHERE [symbol] = @Symbol

END
GO

Open in new window

CAMPzxzxDeathzxzxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Data DudeCommented:
Not sure about 'correct', but since it looks like this SP was created by some kind of querying UI tool, which is optimized to get the query right and not for cosmetic appearance, here's the cleaned up query
IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID(N'dbo.usp_LiveCoinAddAllOrderBookReturnID') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
	DROP PROCEDURE dbo.usp_LiveCoinAddAllOrderBookReturnID;
GO

CREATE PROCEDURE dbo.usp_LiveCoinAddAllOrderBookReturnID( @ExchangeID INT, @Symbol NVARCHAR(128), @LiveCoinAllOrderBookID INT OUTPUT)
AS

BEGIN

IF NOT EXISTS (SELECT * FROM dbo.LiveCoinAllOrderBook WHERE symbol = @Symbol)
	BEGIN

	INSERT INTO dbo.LiveCoinAllOrderBook(ExchangeID,Symbol)
	VALUES(@ExchangeID, @Symbol)

	SELECT @LiveCoinAllOrderBookID = @@IDENTITY

	end
ELSE
	SELECT LiveCoinAllOrderBookID FROM dbo.LiveCoinAllOrderBook WHERE symbol = @Symbol

END
GO

Open in new window


The only programmatic change I made is the begin..end block after the IF NOT EXISTS statement, as an IF statement will execute only the first line if the IF condition is met, and I see two lines here, so the way it was written it would have always executed the second line.

That and I changed the last SELECT to do @LiveCoinAllOrderBookID = LiveCoinAllOrderBookID, which I'm guessing is the indended behavior.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CAMPzxzxDeathzxzxAuthor Commented:
I knew I was overlooking something dumb!  Thanks
Jim HornMicrosoft SQL Server Data DudeCommented:
>That and I changed the last SELECT to do @LiveCoinAllOrderBookID = LiveCoinAllOrderBookID, which I'm guessing is the indended behavior.

Didn't make it to my answer up top.  Should be..
SELECT @LiveCoinAllOrderBookID = LiveCoinAllOrderBookID FROM dbo.LiveCoinAllOrderBook WHERE symbol = @Symbol

Open in new window

Thanks for the grade.
Jim
CAMPzxzxDeathzxzxAuthor Commented:
I figured that one out.  here is the completed sp..

IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID(N'dbo.usp_LiveCoinAddAllOrderBookReturnID') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
	DROP PROCEDURE dbo.usp_LiveCoinAddAllOrderBookReturnID;
GO
CREATE PROCEDURE dbo.usp_LiveCoinAddAllOrderBookReturnID( @ExchangeID INT, @Symbol NVARCHAR(128), @LiveCoinAllOrderBookID INT OUTPUT)
AS
BEGIN

IF NOT EXISTS (SELECT * FROM dbo.LiveCoinAllOrderBook WHERE symbol = @Symbol)
	BEGIN

	INSERT INTO dbo.LiveCoinAllOrderBook(ExchangeID,Symbol)
	VALUES(@ExchangeID, @Symbol)

	SELECT @LiveCoinAllOrderBookID = @@IDENTITY

	END
ELSE
	BEGIN

	SELECT @LiveCoinAllOrderBookID = (SELECT LiveCoinAllOrderBookID FROM dbo.LiveCoinAllOrderBook WHERE symbol = @Symbol)

	END

END
GO

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.