Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi every one,

I have stored procedure created
SQL
USE [WAAS]
GO
/****** Object:  StoredProcedure [WAAS].[ADMIN_ADD_TEMPLATES]    Script Date: 11/30/2012 12:00:44 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [ADD_RECORDS]
(	
    
	@GrpId bigint,
	@DispName varchar(50),
	@FullName varchar(50),
	@LastValue varchar(50),
	@ActiveDirName varchar(50),
        @ClassVal int,
	@UID varchar(15)
		
)
As
BEGIN
        
                INSERT INTO table(GrpId,
				DispName,
				FullName,
				LastValue,
				ActiveDirName,
				ClassVal,
				CreatedBy,
				CreatedDt)
	VALUES(@LobGrpId,
			@DispName,
			@FullName,
			@LastValue,
		        @ActiveDirName,
			@ClassVal,
			@UID,			
			GETDATE())
END

This is my stored procedure created to add records into database.

Now I would like to update the details by writing the update query to update the values in the same stored procedure.

values needs to be updated:
LobGrpId
FullName
ActiveDirName
ClassificationVal
UID
updatedby
updatedtime

Can anyone help me?

Thanks in advance
Posted
Updated 29-Nov-12 23:41pm
v2

Hi..

Before INSERT query you need to check whether there is a record with same @LobGroupId in your table. Try the following procedure in your Stored proc..

SQL
IF EXISTS(SELECT * FROM Table Where GrpId = @LobGroupId)
BEGIN

   -- write your Update query

END

ELSE
BEGIN

   -- write your Insert query

END


Note :- Rename the parameter @GrpId bigint to @LobGrpId bigint in the parameter section of your Stored procedure..

Thank you
 
Share this answer
 
Hope
Quote:
GrpId
is unique in this table. if so, check for existence and can update or insert accordingly

Ex:
 
Share this answer
 
You can do one thing u can pass 1 as insert 2 as update 3 as delete and so on pass the value with value 1 if u want to insert 2 to update
and check in if(@check=1) then in begin and end block write insert query
and check if (@check=2)then in begin and end block write update query and so on...
 
Share this answer
 
Here is an article you might find helpful
Combining Insert/Update to one Procedure[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900