在某些字段上从用户定义的 table 插入数据,其他字段必须使用标量变量
Insert data from user-defined table on some fields, other fields must use scalar variable
我将如何利用加载数据的用户定义 table,插入到 table 但 table 字段中的 1 个不会使用来自用户定义的数据 Table。我想要一个标量变量来代替它。
@dataTableType
是下面的用户自定义table:
CREATE TYPE [dbo].[NewSequenceType] AS TABLE (
[FK_Sequence] [int] NULL,
[FK_Status] [int] NULL,
[FK_Shift] [int] NULL,
[PK_SequenceType] [int] NULL,
[TypeName] [varchar](30) NULL,
[SequenceName] [varchar](100) NULL,
[SequenceDetails] [varchar](400) NULL,
[SequenceStatus] [varchar](15) NULL,
[SequenceShift] [varchar](15) NULL,
[EmployeeId] [varchar](8) NULL,
[EquipmentId] [varchar](25) NULL,
[Comments] [varchar](512) NULL,
[EnteredDate] [datetime] NULL
)
我的存储过程:
ALTER PROCEDURE [dbo].[spNewInspectionEntry] (@dataTableType NewSequenceType READONLY)
INSERT INTO dbo.PIT_Inspection (FK_Sequence, FK_Status, FK_Shift, FK_EmployeeName, FK_EquipmentName, Comments, EnteredDate)
SELECT
FK_Sequence, FK_Status, FK_Shift, EmployeeId,
EquipmentId, Comments, EnteredDate
FROM
@dataTableType;
我想做什么:
DECLARE @EmployeeIDExists varchar(8);
--Note that I only care about one row, this is Ok.
SELECT TOP 1 @EmployeeIDExists = EmployeeId FROM @dataTableType;
--Retrieve PKEmployeeId
SELECT @EmployeeIdPK = PK_EmployeeName
FROM dbo.PIT_EmployeeName
WHERE EmployeeId = @EmployeeIDExists
--Now I need to insert the value of @EmployeeIdPK? Along with the rest, it needs to replace EmployeeID and EquipmentId, Not sure how to...
INSERT INTO dbo.PIT_Inspection (FK_Sequence, FK_Status, FK_Shift, FK_EmployeeName, FK_EquipmentName, Comments, EnteredDate)
SELECT
FK_Sequence, FK_Status, FK_Shift, EmployeeId,
EquipmentId, Comments, EnteredDate
FROM
@dataTableType;
我怎样才能得到@EmployeeIdPK
和其他的一样,它需要替换EmployeeID
,我不想要用户定义的EmployeeId
table 去那里,我想要变量 @EmployeeIdPK
代替。
您可以在 SELECT
列表中放置任何表达式、变量和文字值。请记住,这些值将针对 所有 行重复。
含义:
SELECT Field1,
Field2,
'hello' AS [LiteralText],
5 AS [LiteralNumber],
@Variable AS [ValueFromVariable]
FROM TableName;
将为所有行重复这些文字和变量值。
因此:
INSERT INTO dbo.PIT_Inspection (FK_Sequence, FK_Status, FK_Shift, FK_EmployeeName,
FK_EquipmentName, Comments, EnteredDate)
SELECT
FK_Sequence, FK_Status, FK_Shift, @EmployeeIdPK,
EquipmentId, Comments, EnteredDate
FROM
@dataTableType;
我将如何利用加载数据的用户定义 table,插入到 table 但 table 字段中的 1 个不会使用来自用户定义的数据 Table。我想要一个标量变量来代替它。
@dataTableType
是下面的用户自定义table:
CREATE TYPE [dbo].[NewSequenceType] AS TABLE (
[FK_Sequence] [int] NULL,
[FK_Status] [int] NULL,
[FK_Shift] [int] NULL,
[PK_SequenceType] [int] NULL,
[TypeName] [varchar](30) NULL,
[SequenceName] [varchar](100) NULL,
[SequenceDetails] [varchar](400) NULL,
[SequenceStatus] [varchar](15) NULL,
[SequenceShift] [varchar](15) NULL,
[EmployeeId] [varchar](8) NULL,
[EquipmentId] [varchar](25) NULL,
[Comments] [varchar](512) NULL,
[EnteredDate] [datetime] NULL
)
我的存储过程:
ALTER PROCEDURE [dbo].[spNewInspectionEntry] (@dataTableType NewSequenceType READONLY)
INSERT INTO dbo.PIT_Inspection (FK_Sequence, FK_Status, FK_Shift, FK_EmployeeName, FK_EquipmentName, Comments, EnteredDate)
SELECT
FK_Sequence, FK_Status, FK_Shift, EmployeeId,
EquipmentId, Comments, EnteredDate
FROM
@dataTableType;
我想做什么:
DECLARE @EmployeeIDExists varchar(8);
--Note that I only care about one row, this is Ok.
SELECT TOP 1 @EmployeeIDExists = EmployeeId FROM @dataTableType;
--Retrieve PKEmployeeId
SELECT @EmployeeIdPK = PK_EmployeeName
FROM dbo.PIT_EmployeeName
WHERE EmployeeId = @EmployeeIDExists
--Now I need to insert the value of @EmployeeIdPK? Along with the rest, it needs to replace EmployeeID and EquipmentId, Not sure how to...
INSERT INTO dbo.PIT_Inspection (FK_Sequence, FK_Status, FK_Shift, FK_EmployeeName, FK_EquipmentName, Comments, EnteredDate)
SELECT
FK_Sequence, FK_Status, FK_Shift, EmployeeId,
EquipmentId, Comments, EnteredDate
FROM
@dataTableType;
我怎样才能得到@EmployeeIdPK
和其他的一样,它需要替换EmployeeID
,我不想要用户定义的EmployeeId
table 去那里,我想要变量 @EmployeeIdPK
代替。
您可以在 SELECT
列表中放置任何表达式、变量和文字值。请记住,这些值将针对 所有 行重复。
含义:
SELECT Field1,
Field2,
'hello' AS [LiteralText],
5 AS [LiteralNumber],
@Variable AS [ValueFromVariable]
FROM TableName;
将为所有行重复这些文字和变量值。
因此:
INSERT INTO dbo.PIT_Inspection (FK_Sequence, FK_Status, FK_Shift, FK_EmployeeName,
FK_EquipmentName, Comments, EnteredDate)
SELECT
FK_Sequence, FK_Status, FK_Shift, @EmployeeIdPK,
EquipmentId, Comments, EnteredDate
FROM
@dataTableType;