存储过程中的用户定义表类型问题

时间:2017-05-23 14:25:50

标签: sql sql-server stored-procedures

我创建了一个用户定义的表格类型,如下所示

CREATE TYPE [emp_TT] AS TABLE 
(
 em_number            char(12),
 email                varchar(50),
 emergency_relation   char(32),
 option1              char(16),
)
GO

它是在类型

下创建的

enter image description here

尝试在我的存储过程中使用它们,如

 ALTER PROCEDURE [dbo].[uspInsertorUpdateArchibusINF]
 @emp_TT AS emp_TT READONLY
 AS
 BEGIN
 SET NOCOUNT ON;

 BEGIN TRANSACTION;

 UPDATE e
 SET e.email                 = t.email,
     e.emergency_relation    = t.emergency_relation,
     e.option1               = t.option1,
     FROM [afm].[em] e 
  INNER JOIN @emp_TT t ON e.em_id = t.em_id

  INSERT INTO [afm].[em](email, emergency_relation, option1, em_id)
  SELECT t.email
    , t.emergency_relation,t.option1
    , t.em_id
 FROM @emp_TT t 
 WHERE NOT EXISTS (SELECT 1 
                FROM [afm].[em]
                WHERE em_id = t.em_id)
 COMMIT TRANSACTION;

 END;

当我执行存储过程时,它会抛出错误,如

Column, parameter, or variable #1: Cannot find data type emp_TT. Parameter or variable '@emp_TT' has an invalid data type.

Must declare the table variable "@emp_TT".

Must declare the table variable "@emp_TT".

请告诉我这里缺少的东西

3 个答案:

答案 0 :(得分:0)

在第2行使用afm之类的架构afm.emp_TT,如下所示

 ALTER PROCEDURE [dbo].[uspInsertorUpdateArchibusINF]
 @emp_TT AS afm.emp_TT READONLY
 AS
 BEGIN
 SET NOCOUNT ON;

 BEGIN TRANSACTION;

 UPDATE e
 SET e.email                 = t.email,
     e.emergency_relation    = t.emergency_relation,
     e.option1               = t.option1,
     FROM [afm].[em] e 
  INNER JOIN @emp_TT t ON e.em_id = t.em_id

  INSERT INTO [afm].[em](email, emergency_relation, option1, em_id)
  SELECT t.email
    , t.emergency_relation,t.option1
    , t.em_id
 FROM @emp_TT t 
 WHERE NOT EXISTS (SELECT 1 
                FROM [afm].[em]
                WHERE em_id = t.em_id)
 COMMIT TRANSACTION;

 END;

答案 1 :(得分:0)

在使用它之前,您需要声明它...如下所示

DECLARE @emp_TT AS emp_TT

INSERT INTO @emp_TT 
 ALL VALUES

最后,您可以将其传递给Stored Proc

exec [dbo].[uspInsertorUpdateArchibusINF] @emp_tt

答案 2 :(得分:0)

CREATE TYPE [emp_TT] AS TABLE 
(
 em_number            char(12),
 email                varchar(50),
 emergency_relation   char(32),
 option1              char(16)  --Removed , here
)
GO


ALTER PROCEDURE [dbo].[uspInsertorUpdateArchibusINF]
 @emp_TT AS emp_TT READONLY
 AS
 BEGIN
 SET NOCOUNT ON;

 BEGIN TRANSACTION;

 UPDATE e
 SET e.email                 = t.email,
     e.emergency_relation    = t.emergency_relation,
     e.option1               = t.option1 --Removed , here
     FROM [afm].[em] e 
  INNER JOIN @emp_TT t ON e.em_id = t.em_id

  INSERT INTO [afm].[em](email, emergency_relation, option1, em_id)
  SELECT t.email
    , t.emergency_relation,t.option1
    , t.em_id
 FROM @emp_TT t 
 WHERE NOT EXISTS (SELECT 1 
                FROM [afm].[em]
                WHERE em_id = t.em_id)
 COMMIT TRANSACTION;

 END;

执行如下所示的存储过程

DECLARE  @emp_TT AS [emp_TT] 

 INSERT INTO @emp_TT 
 SELECT 'Your All Values'

 EXEC [dbo].[uspInsertorUpdateArchibusINF] @emp_tt