【发布时间】:2016-06-20 22:49:32
【问题描述】:
我有一个 UI,它允许用户选择他们想要添加到表中的一个或多个字段。该数据还有一个与之关联的orderID,用于确定字段顺序。
当用户添加新字段时,我需要找到该用户最后使用的orderID并将其加1,提交所有新字段。
例如,如果数据库中已经存在一条记录,则它的orderID 为 1。当我选择再添加三个字段时,它会检查我使用的最后一个 orderID (1) 然后为它添加的每个新记录递增 1-4。
-- Get the last ID orderID for this user and increment it by 1 as our starting point
DECLARE @lastID INT = (SELECT TOP 1 orderID FROM dbo.BS_ContentRequests_Tasks_User_Fields WHERE QID = @QID ORDER BY orderID DESC)
SET @lastID = @lastID+1;
-- Create a temp table to hold our fields that we are adding
DECLARE @temp AS TABLE (fieldID int, orderID int)
-- Insert our fields and incremented numbers
INSERT INTO @temp( fieldID, orderID )
SELECT ParamValues.x1.value('selected[1]', 'int'),
@lastID++
FROM @xml.nodes('/root/data/fields/field') AS ParamValues(x1);
显然@lastID++ 部分是我的问题所在,但希望它有助于理解我想要做什么。
还有什么方法可以解决这个问题?
【问题讨论】:
标签: sql-server tsql stored-procedures sql-server-2012