Solved

if exists

Posted on 2013-06-11
2
169 Views
Last Modified: 2013-06-13
I want to check if SID exists in table sms_messages first, if it does not, I want to insert my data into sms_messages. I also want a status to say, if 1, it is inserted to a table, if 0 then no. I only want the result to return 0 or 1.


create procedure Insert_msg

@sid              varbinary(50),
@date_received      smalldatetime,
@direction      char(1),
@send_from      varchar(14),
@status              varchar(1),
@body            varchar(164)

as




if not exists
(select [sid] from  sms_messages where sid = @sid)

    insert into sms_messages (sid, date_received, direction, sent_from, [status], body) values
    (@sid, date_received, direction, sent_from, @status, @body)

    status = 1

else

   status = 0
0
Comment
Question by:VBdotnet2005
[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 Comments
 
LVL 23

Accepted Solution

by:
nemws1 earned 250 total points
ID: 39239394
Here's the new proc.  I added a new parameter, @inserted, that is an OUTPUT variable.  Pass in another variable (of TYPE int) when you call this stored procedure and that var will have a zero or 1 in it after the procedure returns.

To do compound statements with IF..ELSE, use BEGIN..END blocks.

CREATE PROCEDURE Insert_msg @sid VARBINARY(50)
	, @date_received SMALLDATETIME
	, @direction CHAR(1)
	, @send_from VARCHAR(14)
	, @status VARCHAR(1)
	, @body VARCHAR(164)
	, @inserted INT OUTPUT
AS
BEGIN
	SET @inserted = 0;
	
	IF NOT EXISTS (
			SELECT TOP 1 [sid]
			FROM sms_messages
			WHERE sid = @sid
			)
	BEGIN
		INSERT INTO sms_messages (
			sid
			, date_received
			, direction
			, sent_from
			, [status]
			, body
			)
		VALUES (
			@sid
			, date_received
			, direction
			, sent_from
			, @status
			, @body
			);
			
		SET @inserted = 1
	END
	ELSE
	BEGIN
		SET @inserted = 0
	END
END
GO

Open in new window

0
 
LVL 5

Assisted Solution

by:DOSLover
DOSLover earned 250 total points
ID: 39239879
Here is another way of doing it using system variabbles for error and inserted-record-count:
It sets return code of 'E' in case of any error in insert statement.
CREATE PROCEDURE Insert_msg 
	( @sid VARBINARY(50)
	, @date_received SMALLDATETIME
	, @direction CHAR(1)
	, @send_from VARCHAR(14)
	, @status VARCHAR(1)
	, @body VARCHAR(164) 
	, @returnCode varchar(1) OUTPUT
	)
AS
BEGIN

DECLARE @InsertCountVar INT;
DECLARE @ErrorVar INT;

    insert into sms_messages (sid, date_received, direction, sent_from, status, body) 
	       values (@sid, @date_received, @direction, @sent_from, @status, @body)
	where not exists (select sid from  sms_messages where sid = @sid);

	SELECT @ErrorVar = @@ERROR, @InsertCountVar = @@ROWCOUNT;

	IF @ErrorVar <> 0
		BEGIN
			SET @returnCode = 'E'
		END
	else
		IF @InsertCountVar <> 0
			BEGIN
				SET @returnCode = '1'
			END
		ELSE
			BEGIN
				SET @returnCode = '0'
			END
...
...
...

END

Open in new window

0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Convert rows to columns 5 34
exec SQL Server Change Tracking CurrentVersion()  across dbs 6 28
What is needed to become a DBA? 7 56
HIghlights of SSIS? 3 45
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

751 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