First off, this isn't "urgent". I have a working solution, but my solution grates at me. Every time I do something like this, it's like fingernails on chalkboard.The problem: The app I maintain the database for is all proprietary ad-hoc queries, so I cannot change the way the system does things, or even tune the queries.For all tables, the system uses an ID field as the primary key (ASSETID, VENDORID, etc.) There is also a sequence table that holds the next available number for each table. When a new record is inserted, the system looks at the sequence table for the new ID and increments the sequence table.A problem arises when a developer goes into SSMS and inserts a record or two into the table with a dummy ID. When the system gets to that ID, it throws up with a "primary key" error.So, I have to go through the tables and update the sequence table with the max(ID) + 1 from the base table.Here is the abbreviated DDL for the tables in questionSequence table:CREATE TABLE [dbo].[sequence]( [tbname] [varchar](60) NOT NULL, [name] [varchar](60) NOT NULL, [reserved] [bigint] NOT NULL, [maxvalue] [bigint] NULL) ON [PRIMARY]
Sample tables:CREATE TABLE [dbo].[asset]( [assetnum] [varchar](60) NOT NULL, [assetid] [bigint] NOT NULL) ON [PRIMARY]CREATE TABLE [dbo].[vendor]( [vendornum] [varchar](60) NOT NULL, [vendorid] [bigint] NOT NULL) ON [PRIMARY]
Sample data:INSERT INTO sequence(tbname, name, reserved, maxvalue)SELECT asset, assetid, 50, 40 UNION ALLSELECT vendor, vendorid, 50, 40 insert into asset (assetnum, assetid)select 'Asset1', 38 union allselect 'Asset2', 39 union allselect 'Asset3', 40insert into vendor (vendornum, vendorid)select 'Vendor1', 38 union allselect 'Vendor2', 39 union allselect 'Vendor3', 40 union allselect 'Vendor4', 55
So as you can see, a developer went in (despite the decapatation threats) and added Vendor4 with a rogue vendorid. What I need to do is leave the "asset" record alone on the sequence table but update the "reserved" value on the "Vendor" record to 56.What I have -- and this works fine, I would just love to learn a more elegant solution:--create temp tablecreate table #sequence(tbname varchar(50),colname varchar(50),reserved numeric,value numeric default 0,sql varchar(300),seq numeric)--populate basic info into temp tableinsert into #sequence (tbname, colname, reserved) select tbname, name, reserved from sequence--populate sequence numbersdeclare @seq numeric = 0update #sequence set @seq = seq = @seq + 1--build preliminary dynamic sqlupdate #sequence set sql = 'select max(' + colname + ') from '+ tbname--populate value column with max value from the tablesdeclare @count numeric = 0declare @sqlstmt varchar(300)declare @maxvalue numericwhile @count <= (select max(seq) from #sequence) begin set @sqlstmt = 'update #sequence set value = (' + (select SQL from #sequence where seq = @count) + ') where seq = ' + cast(@count as varchar(10)) exec (@sqlstmt) set @count = @count + 1 end--Build and execute dynamic sql to update sequencedeclare @tblname varchar(50) declare @colname varchar(50)set @count = 1delete from #sequence where value is nulldelete from #sequence where value < reservedwhile @count <= (select max(seq) from #sequence) begin set @tblname = (select tbname from #sequence where seq = @count) set @colname = (select colname from #sequence where seq = @count) set @maxvalue = (select value from #sequence where seq = @count) + 1 set @sqlstmt = 'update sequence set reserved = ' + cast(@maxvalue as varchar(10)) + ' where tbname = ''' + @tblname + ''' and name = ''' + @colname + '''' exec (@sqlstmt) set @count = @count + 1 end drop table #sequence
If anyone can show me how to do this without the loops and dynamic SQL, I would bow deeply in your direction. Thanks,Curt[/code]