Solved

Stored Procedure with two insert statements

Posted on 2012-03-27
4
361 Views
Last Modified: 2012-08-14
Hello Experts,
Was trying to create a stored procedure which will insert records on two tables (table 1 & table 2) if recordID does not exists else just update one table (table 1). Unfortunately i'm experiencing an incorrect syntax near the keyword 'ELSE' when i create the SP.



CREATE PROCEDURE [dbo].[SP_01]
@RecordID INT,
@Desc VARCHAR(MAX)
AS
BEGIN
IF
(SELECT COUNT(*) FROM [dbo].[Table_1] WHERE [RecordID] = @RecordID) = 0

INSERT INTO [dbo].[Table_1] (Col1, Col2) VALUES (@RecordID, @Desc)

INSERT INTO  [dbo].[Table_2] (Col1, Col2, Col3) VALUES (@RecordID, 1, GETDATE())

ELSE
UPDATE  [dbo].[Table_1] SET Col2 = @Desc WHERE Col1 = @RecordID

END

GO
0
Comment
Question by:jsuanque
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
micropc1 earned 200 total points
Comment Utility
Try...

CREATE PROCEDURE [dbo].[SP_01]
@RecordID INT,
@Desc VARCHAR(MAX)
AS 
BEGIN
   IF  (SELECT COUNT(*) FROM [dbo].[Table_1] WHERE [RecordID] = @RecordID) = 0
      BEGIN
         INSERT INTO [dbo].[Table_1] (Col1, Col2) VALUES (@RecordID, @Desc)
         INSERT INTO  [dbo].[Table_2] (Col1, Col2, Col3) VALUES (@RecordID, 1, GETDATE())
      END
   ELSE
      UPDATE  [dbo].[Table_1] SET Col2 = @Desc WHERE Col1 = @RecordID
END

GO

Open in new window

0
 

Assisted Solution

by:jsuanque
jsuanque earned 0 total points
Comment Utility
Sorry disregard...found the solution...

CREATE PROCEDURE [dbo].[SP_01]
@RecordID INT,
@Desc VARCHAR(MAX)
AS
IF
(SELECT COUNT(*) FROM [dbo].[Table_1] WHERE [RecordID] = @RecordID) = 0
BEGIN
INSERT INTO [dbo].[Table_1] (Col1, Col2) VALUES (@RecordID, @Desc)

INSERT INTO  [dbo].[Table_2] (Col1, Col2, Col3) VALUES (@RecordID, 1, GETDATE())
END

IF
(SELECT COUNT(*) FROM [dbo].[Table_1] WHERE [RecordID] = @RecordID) <> 0
BEGIN
UPDATE  [dbo].[Table_1] SET Col2 = @Desc WHERE Col1 = @RecordID
END

GO
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Or simply (just fix the obvious typo in xUPDATE):
xUPDATE  [dbo].[Table_1] SET Col2 = @Desc WHERE Col1 = @RecordID
IF @@ROWCOUNT = 0
	BEGIN
		INSERT INTO [dbo].[Table_1] (Col1, Col2) VALUES (@RecordID, @Desc)
		INSERT INTO  [dbo].[Table_2] (Col1, Col2, Col3) VALUES (@RecordID, 1, GETDATE())
	END

Open in new window

0
 

Author Closing Comment

by:jsuanque
Comment Utility
Thanks both works...but i like yours better:-)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now