SQL Server存储过程插入/更新哪个有日期

时间:2015-04-09 14:56:17

标签: sql-server stored-procedures

具有dd-MMM-yy格式日期的源,必须将其转换为yyyy-mm-dd hh:mm:ss:SS并使用存储过程插入/更新到数据库中。存储过程的新功能我设法编写了一个存储过程,但我没有在哪里或如何转换日期格式。

ALTER PROCEDURE [dbo].[uspInsertorUpdate]
    @dp             char(32),
    @dv             char(32),
    @e_num          char(12),
    @mail           varchar(50),
    @emerg          char(32),
    @opt1           char(16),
    @stat           char(20),
    @e_id           char(35),
    @e_tit          varchar(64),
    @e_date         datetime
AS
BEGIN

SET NOCOUNT ON;

MERGE [dbo].[sampleemployee] AS [Target]
USING (SELECT @dp, @dv , @e_num, @mail, @emerg, @opt1, @stat, @e_id, @e_tit, @e_date)
AS [Source] ( dp, dv, e_num, mail, emerg, opt1, stat, e_id, e_tit, e_date)  
ON [Target].e_id = [Source].e_id
WHEN MATCHED THEN

UPDATE SET  dp               = [Source].dp,
            dv               = [Source].dv,
            e_num            = [Source].e_num,
            mail             = [Source].mail,
            emerg            = [Source].emerg,
            opt1             = [Source].opt1,
            stat             = [Source].stat,
            e_id             = [Source].e_id,
            e_tit            = [Source].e_tit,
            e_date           = [Source].e_date

WHEN NOT MATCHED THEN

INSERT ( dp, dv, e_num, mail, emerg, opt1, stat, e_id, e_tit, e_date)
VALUES ( [Source].dp, [Source].dv, [Source].e_num, [Source].mail, [Source].emerg, [Source].opt1, [Source].stat, [Source].e_id, [Source].e_tit, [Source].e_date);

END;

2 个答案:

答案 0 :(得分:0)

它应该像这样简单.....避免使用合并声明

ALTER PROCEDURE [dbo].[uspInsertorUpdate]
@dp             char(32),
@dv             char(32),
@e_num          char(12),
@mail           varchar(50),
@emerg          char(32),
@opt1           char(16),
@stat           char(20),
@e_id           char(35),
@e_tit          varchar(64),
@e_date         datetime
AS
BEGIN
SET NOCOUNT ON;

IF EXISTS (SELECT 1 FROM [dbo].[sampleemployee] WHERE e_id= @e_id)
  BEGIN
     UPDATE [dbo].[sampleemployee]
       SET  dp      = @dp,
            dv      = @dv,
            e_num   = @e_num,
            mail    = @mail,
            emerg   = @emerg,
            opt1    = @opt1,
            stat    = @stat,
            e_tit   = @e_tit,
            e_date  = @e_date
     WHERE  e_id    = @e_id
  END
ELSE 
  BEGIN
     INSERT INTO [dbo].[sampleemployee]( dp, dv, e_num, mail, emerg, opt1, stat, e_id, e_tit, e_date) 
     VALUES ( @dp, @dv, @e_num, @mail, @emerg, @opt1, @stat, @e_id, @e_tit, @e_date);
  END

END;

答案 1 :(得分:0)

Just put this.. convert(varchar(50),@e_date,121) at your using select statement

.....

USING (SELECT @dp, @dv , @e_num, @mail, @emerg, @opt1, @stat, @e_id, @e_tit, convert(varchar(50),@e_date,121))

...