日期:2014-05-17 浏览次数:20512 次
CREATE TABLE [dbo].[T_FillingRec]( [FillingRec] [uniqueidentifier] NOT NULL, [Organize] [varchar](16) NULL, [SerialPort] [int] NULL, [Sequence] [bigint] NULL, [BUTime] [datetime] NULL, [BUUserName] [varchar](32) NULL, CONSTRAINT [T_Gas_FillingRec_PK] PRIMARY KEY CLUSTERED ( [FillingRec] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
Create PROCEDURE [dbo].[pro_T_FillingRec1_Insert] @FillingRec uniqueidentifier, @Organize varchar(16), @SerialPort int, @Sequence bigint, @BUTime datetime, @BUUserName varchar(32) AS SET NOCOUNT ON IF EXISTS(SELECT * FROM [dbo].[T_FillingRec1] WHERE SerialPort = @SerialPort And Sequence = @Sequence) BEGIN UPDATE [dbo].[T_FillingRec1] SET [Organize] = @Organize, [BUTime] = @BUTime, [BUUserName] = @BUUserName WHERE SerialPort = @SerialPort And Sequence = @Sequence END ELSE BEGIN Set @FillingRec = NEWID() INSERT INTO [dbo].[T_FillingRec1] ([FillingRec],[Organize],[SerialPort],[Sequence],[BUTime],[BUUserName]) Select @FillingRec,@Organize,@SerialPort,@Sequence,@BUTime,@BUUserName END
--如果你的系统是2008的,那么使用merge将是完美方案 IF EXISTS(SELECT * FROM [dbo].[T_FillingRec1] WHERE SerialPort = @SerialPort And Sequence = @Sequence) BEGIN UPDATE [dbo].[T_FillingRec1] SET [Organize] = @Organize, [BUTime] = @BUTime, [BUUserName] = @BUUserName WHERE SerialPort = @SerialPort And Sequence = @Sequence END ELSE BEGIN Set @FillingRec = NEWID() INSERT INTO [dbo].[T_FillingRec1] ([FillingRec],[Organize],[SerialPort],[Sequence],[BUTime],[BUUserName]) Select @FillingRec,@Organize,@SerialPort,@Sequence,@BUTime,@BUUserName END MERGE dbo.[T_FillingRec1] AS Target USING (SELECT SerialPort, Sequence,[BUTime],[BUUserName],[Organize] from xxx /*所有的新值*/) AS Source ON (Target.SerialPort = Source.SerialPort AND Target.Sequence = Source.Sequence) WHEN MATCHED THEN UPDATE SET Target.[BUTime] = Source.[BUTime], Target.[BUUserName] = Source.[BUUserName], Target.[Organize] = Source.[Organize], WHEN NOT MATCHED BY TARGET THEN INSERT ([FillingRec],[Organize],[SerialPort],[Sequence],[BUTime],[BUUserName]) VALUES (Source.[FillingRec], Source.[Organize], Source.[SerialPort], Source.[Sequence], Source.[BUTime], Source.[BUUserName]);
------解决方案--------------------
--只是下面这段 MERGE dbo.[T_FillingRec1] AS Target USING (SELECT SerialPort, Sequence,[BUTime],[BUUserName],[Organize] from xxx /*所有的新值*/) AS Source ON (Target.SerialPort = Source.SerialPort AND Target.Sequence = Source.Sequence) WHEN MATCHED THEN UPDATE SET Target.[BUTime] = Source.[BUTi