[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Stored Procedure with two insert statements

Posted on 2012-03-27
4
Medium Priority
?
375 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
micropc1 earned 800 total points
ID: 37774999
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
ID: 37775007
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
ID: 37778139
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
ID: 37792786
Thanks both works...but i like yours better:-)
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

649 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