Friday, February 8, 2008

UPSERT Example (SQL Server Central)

Importance of UPSERT (SQL Server Central)


Set statistics io on
GO
declare @parm1 int,
@parm2 char(1),
@parm3 int
set @parm1 = 1
set @parm2 = 'Y'
set @parm3 = 3
UPDATE dbo.tbTicketing - Always Schema Qualify
SET DiscrepancyFlag = @Parm2
, Status = @Parm1
WHERE TicketNumber = @Parm3
IF @@rowcount = 0 - Make sure you do this before executing any other statement
INSERT dbo.tbTicketing
( TicketNumber
,Customer
,TicketDate
,TicketFrom
,Comments
,DiscrepancyFlag
,Status
)
VALUES
( @parm3
,'Test1'
,getDate()
,'Marketing'
,'Testing'
, @parm2
, @parm1)
Set statistics io off
GO

No comments: