Is there a simple way to insert multiple rows and increment a particular field, using SQL Server 2005?
Note, I am not looking for a solution involving an identity column - see the bottom of the question for an explanation
(The following schema and data have been replicated here in this SQLFiddle.)
For instance, consider the following table and data...
CREATE TABLE #TEMPTABLE (
[PKID] INT IDENTITY, [FKID] INT, [MYTEXT] VARCHAR(10), [SEQUENCE] INT
)
INSERT INTO #TEMPTABLE ([FKID], [MYTEXT], [SEQUENCE]) VALUES (1, 'one', 1)
INSERT INTO #TEMPTABLE ([FKID], [MYTEXT], [SEQUENCE]) VALUES (1, 'two', 2)
-- Table data
PKID FKID MYTEXT SEQUENCE
1 1 one 1
2 1 two 2
And the following data to be inserted...
DECLARE @FKID INT
SET @FKID = 1
DECLARE @NEWDATA XML
SET @NEWDATA = '<data><text>three</text><text>four</text></data>'
Can the following be written in such as way that the SEQUENCE field comes out as 1,2,3,4 instead of the 1,2,3,3 that is currently does?
INSERT INTO #TEMPTABLE ([FKID], [MYTEXT], [SEQUENCE])
SELECT @FKID,
X.value('.','VARCHAR(10)'),
(SELECT ISNULL(MAX([SEQUENCE]),0)+1 FROM #TEMPTABLE WHERE [FKID]=@FKID)
FROM @NEWDATA.nodes('/data/text') AS X(X)
-- Actual result...
PKID FKID MYTEXT SEQUENCE
1 1 one 1
2 1 two 2
3 1 three 3
4 1 four 3 <-- Issue
-- Required result...
PKID FKID MYTEXT SEQUENCE
1 1 one 1
2 1 two 2
3 1 three 3
4 1 four 4
Update:
In response to the comment by @marc_s...
Identity would be the best solution for 2005... best solution by far - why do you explicitly exclude it and insist on rolling your own? (with all the risks of causing duplicates and so on....)
The table in question will hold multiple sets of SEQUENCE values, each "set" based on the FKID value... therefore the table could hold data along these lines...
PKID FKID MYTEXT SEQUENCE
1 1 one 1
2 1 two 2
3 1 three 3
4 1 four 4
5 2 ett 1
6 2 tva 2
7 2 tre 3
FKID=1can have multiple rows withSEQUENCE=1,2,3andFKID=2can also haveSEQUENCE=1,2. Sorry if I didn't make that clear in the OP