SELECT
@MaxSeq = (CASE
WHEN (SELECT COUNT(*) FROM app_Attachments
WHERE app_Attachments.AppId = @appID) <= 0
THEN 1
ELSE (SELECT (MAX(seq)+1)
FROM app_Attachments
WHERE app_Attachments.AppId = @appID)
END)
DECLARE @Id INT;
SELECT @Id = (COALESCE((SELECT MAX(Id)+1 FROM app_Attachments), 1))
INSERT INTO app_Attachments (Id, AppID, AttName, AttContentType, AttData, AddedBy, AddedDate, Seq)
SELECT
@Id, AppId, ImgNames, ImgType, Bytes, @AddedBy, @AddedDate, @MaxSeq
FROM
@Attachments --user defined table
CREATE TYPE [dbo].[AppsAttachments] AS TABLE
(
[AppId] [INT] NULL,
[Bytes] [VARBINARY](MAX) NULL,
[ImgNames] [NVARCHAR](MAX) NULL,
[ImgType] [NVARCHAR](200) NULL
)
使用存储过程将表插入到表中,该存储过程具有用户定义的表作为参数,仅当传递的表有一行时才成功,将主键设置为标识后,将主键更改为要输入的手动整数后,它运行良好问题发生了
答案 0 :(得分:2)
如果Id不是身份,那么您需要确保Id不重复。使用Row_number()
生成一个连续的数字序列
insert into app_Attachments (Id,AppID,AttName,AttContentType,AttData,AddedBy,AddedDate,Seq)
select @Id + ROW_NUMBER() OVER (ORDER BY AppId) - 1,
AppId,
ImgNames,
ImgType,
Bytes,
@AddedBy,
@AddedDate,
@MaxSeq
from @Attachments --user defined table
如果您还需要增加Seq
,请在查询中添加row_number()