Solved

Stored Procedure with two insert statements

Posted on 2012-03-27
4
372 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 200 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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 …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 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