SQL Server 用户定义表类型

用户定义表类型:

CREATE TYPE [dbo].[TVP_Location] AS TABLE(
[Block] [int] NULL,
[BlockName] [varchar](50) NULL,
[Location] [varchar](50) NOT NULL,
[Address] [varchar](4) NULL,
[WorkUnit] [int] NULL,
[WorkUnitName] [varchar](50) NULL,
[ItemCode] [varchar](100) NULL,
[ItemName] [nvarchar](500) NULL,
[Row] [int] NULL,
[Column] [int] NULL,
[LampAddress] [varchar](4) NULL
)

表类型的使用:

CREATE PROCEDURE [dbo].[P_Location]
  @tvpLocation TVP_Location1 Readonly
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;


begin transaction TRAN1

declare @errors int

delete from L_Location -- 删除已有库位
set @errors = @@ERROR;

insert into L_Location(
[Location],[Address],[WorkUnit],[ItemCode],[ItemName],[Row],[Column],[LampAddress])
select
[Location],[Address],[WorkUnit],[ItemCode],[ItemName],[Row],[Column],[LampAddress]
from @tvpLocation

set @errors = @errors + @@ERROR;

if (@errors<>0)
begin
rollback transaction TRAN1
end
else
begin
commit transaction TRAN1
end

END

猜你喜欢

转载自www.cnblogs.com/wfy680/p/11971738.html