在存储过程中使用表值参数

时间:2015-04-15 17:06:13

标签: sql-server stored-procedures

我不知道如何使用Table Value Parameter更改正常的存储过程。我必须创建一个单独的表,或者我必须创建一个数据类型。非常感谢任何帮助。

ALTER PROCEDURE [dbo].[uspInsertorUpdateINF]
@dp_id                char(32),
@dv_id                char(32),
@em_number            char(12),
@email                varchar(50),
@emergency_relation   char(32),
@option1              char(16),
@status               char(20),
@em_id                char(35),
@em_title             varchar(64),
@date_hired           datetime
AS
BEGIN
SET NOCOUNT ON;

MERGE [dbo].[em] AS [Targ]
USING (VALUES (@dp_id, @dv_id , @em_number, @email, @emergency_relation, @option1, @status, @em_id, @em_title, @date_hired))
AS [Sourc] (dp_id, dv_id, em_number, email, emergency_relation, option1, status, em_id, em_title, date_hired)  
  ON [Targ].em_id = [Sourc].em_id

 WHEN MATCHED THEN
  UPDATE 
     SET dp_id                 = [Sourc].dp_id,
         dv_id                 = [Sourc].dv_id,
         em_number             = [Sourc].em_number,
         email                 = [Sourc].email,
         emergency_relation    = [Sourc].emergency_relation,
         option1               = [Sourc].option1,
         status                = [Sourc].status,
         em_title              = [Sourc].em_title,
         date_hired            = [Sourc].date_hired

  WHEN NOT MATCHED BY TARGET THEN
  INSERT (dp_id, dv_id, em_number, email, emergency_relation, option1, status, em_id, em_title,date_hired)
  VALUES ([Sourc].dp_id, [Sourc].dv_id, [Sourc].em_number, [Sourc].email, [Sourc].emergency_relation, [Sourc].option1, [Sourc].status, [Sourc].em_id, [Sourc].em_title, [Sourc].date_hired);

  END;

2 个答案:

答案 0 :(得分:3)

首先创建用户定义的表类型:

CREATE TYPE MyTableType AS TABLE 
( dp_id                char(32),
dv_id                char(32),
em_number            char(12),
email                varchar(50),
emergency_relation   char(32),
option1              char(16),
status               char(20),
em_id                char(35),
em_title             varchar(64),
date_hired           datetime);
GO

然后将您的程序更改为:

ALTER PROCEDURE [dbo].[uspInsertorUpdateINF]
@customTable MyTableType READONLY
AS
BEGIN
SET NOCOUNT ON;

MERGE [dbo].[em] AS [Targ]
USING @customTable as [Sourc]  
  ON [Targ].em_id = [Sourc].em_id

 WHEN MATCHED THEN
 ..........

答案 1 :(得分:2)

我会按如下方式重写整个程序。我将编写两个单独的(插入和更新)语句并将它们包装到ONE Transaction中,而不是使用Merge语句。

TYPE

CREATE TYPE em_TT AS TABLE 
(
dp_id                char(32),
dv_id                char(32),
em_number            char(12),
email                varchar(50),
emergency_relation   char(32),
option1              char(16),
[status]             char(20),
em_id                char(35),
em_title             varchar(64),
date_hired           datetime
)
GO

存储过程

ALTER PROCEDURE [dbo].[uspInsertorUpdateINF]
@em_TT AS em_TT READONLY
AS
BEGIN
SET NOCOUNT ON;

BEGIN TRANSACTION;

  UPDATE e
     SET e.dp_id                 = t.dp_id,
         e.dv_id                 = t.dv_id,
         e.em_number             = t.em_number,
         e.email                 = t.email,
         e.emergency_relation    = t.emergency_relation,
         e.option1               = t.option1,
         e.status                = t.status,
         e.em_title              = t.em_title,
         e.date_hired            = t.date_hired
    FROM [dbo].[em] e 
    INNER JOIN @em_TT t ON e.em_id = t.em_id



  INSERT INTO [dbo].[em](dp_id, dv_id, em_number, email, emergency_relation
                                    , option1, status, em_id, em_title,date_hired)
  SELECT t.dp_id, t.dv_id, t.em_number, t.email
        , t.emergency_relation, t.option1, t.status
        , t.em_id, t.em_title, t.date_hired
  FROM @em_TT t 
  WHERE NOT EXISTS (SELECT 1 
                    FROM [dbo].[em]
                    WHERE em_id = t.em_id)
COMMIT TRANSACTION;

END;