In the infinte wisdom of the global DBA at a firm I am working at right now he has created a table that takes an int as an ID field, but does not auto increment the number.
I am passing up a table valued parameter from .Net because it has roughly 100 or more rows of data that are being passed up at any one time and I dont want to kill the application, hammer the network or the SQL Server.
So this is my stored procedure
CREATE PROCEDURE sp_Insert_Supporting_Error_Info
(@tvp [dbo].udt_CEQZW READONLY)
as
begin
INSERT INTO CEQZW
ERROR_VAR_ID,
ERROR_ID,
ERROR_VAR_TYPE_CD,
ERROR_VAR_VALUE)
SELECT (SELECT coalesce(MAX(ERROR_VAR_ID), 0) + row_number() over (order by
(select NULL)) FROM CEQZW) as ERROR_VAR_ID,
ERROR_ID,
ERROR_VAR_TYPE_CD,
ERROR_VAR_VALUE FROM @TVP
end
go
I was hoping that this SELECT coalesce(MAX(ERROR_VAR_ID), 0) + row_number() over (order by (select NULL)) FROM CEQZW
would some how do the trick for me as when I test with this
declare @p3 dbo.udt_CEQZW
insert into @p3 values(1,N'es',N'test')
insert into @p3 values(1,N'ec',N'test')
insert into @p3 values(1,N'ec',N'test')
insert into @p3 values(1,N'ses',N'test')
insert into @p3 values(1,N'es',N'test')
exec sp_Insert_Supporting_Error_Info @p3
this is what I get back
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected) Msg 2627, Level 14, State 1, Procedure sp_Insert_Supporting_Error_Info, Line 9 Violation of PRIMARY KEY constraint 'PK_CEQZW'. Cannot insert duplicate key in object 'dbo.CEQZW'. The duplicate key value is (1). The statement has been terminated.
So the question I have is how would I, other than hammering the network, app and SQL Server auto increment and add the ID into the table