Along the way of programming I discovered a number of useful things, like how to get a list of all the fields names in a database, how to search for code containing a string or strings inside SQL Server, and all sorts of other tid-bits!
Anyway, today I needed to take a table and set a field to a number all through the field. Easy enough, if it's just one number, but what if it's two or more numbers, and you have to distribute them so that record 1 gets number a, record 2 number b, etc. until you run out of numbers and then repeat the sequence over, until the end of the table? Oh yes, and you don't know in advance how many numbers there'll be to distribute!
declare @taPeople Table ( RowID int Primary Key Identity(1, 1),
PersonID int)
-- ----------------------------------------------------
-- Find out how many are needed
-- ----------------------------------------------------
select @intNrIDs = count(*)
from tblIDList
where CompanyID = @intCompany and
-- ----------------------------------------------------
-- Fill the table var. with the IDs we want to use,
-- and the Identity fied will number them for me
-- ----------------------------------------------------
insert into @taPeople
select PersonID
from tblPeople
where CompanyID = @intCompany
-- ----------------------------------------------------
-- Arrange for tblData also to have a contiguous
-- monotonic-increasing key (i.e. identity (1, 1))
-- ----------------------------------------------------
update dbo.tblData
set PersonID = (select PersonID
from @taPeople
where RowID = (DataRowID % @intNrIDs ) + 1) where CompanyID = @intCompany
-- ----------------------------------------------------
Well, I thought it kinda neat, 'cos it lets me do non-random distribution of data across all the records of a table - not a thing usually easily done without a cursor.
Yarn Stuff
Back to the subject that's been consuming this blog for the last few episodes - wool !
As always, we wnt to The GrindHouse Cafe in Haddonfield today (Tuesday) and I programmed and Tola and the other 25 or so ladies all knitted and chatted.

At Loop, The Better Half wanted a signed book. I might have lost her then, but the signature duly materialised. As you can imagine, the signature of a cartoon artist is seriously attractive.
No comments:
Post a Comment