SQL Scripting, The Museum System

A Place for Everything: Part 1

My mother has always been a proponent of finding a place for everything, and putting everything in its place. Before you can use the Location & Movement sub-module to record where everything is placed, you must first use the Locations Authority to create the places in which everything can be placed. While easy to use, the Locations Authority is not always efficient for adding a large number of new locations. In Part 1, I’ll introduce a script I use to add a series of numeric units to a site/subsite.

  • Site » Subsite » 1
  • Site » Subsite » 2
  • Site » Subsite » 3

When the length of @qty is greater then 9, Unit Numbers will automatically be padded with zeros to allow for an appropriate sort order. The value of @qty sets the total number of units to be added to the site/subsite.

The fields @active and @public are used to set the whether the location is active or public access, respectively, and can be either 1 or 0.

/* BATCH ASSIGN LOCATIONS TO AUTHORITY */
DECLARE @site varchar(128), @subsite varchar(128), @description varchar(255), @public int, @active int, @login varchar(32), @qty int, @counter int, @unitType varchar(32), @spaces int

/* EDIT THESE VALUES */
SET @site = 'Museum'
SET @subsite = 'Gallery 120'

SET @description = NULL

SET @active = 1
SET @public = 1

SET @login = 'chad.petrovay'

SET @qty = 6

/* DO NOT EDIT SCRIPT BELOW */
SET NOCOUNT ON
SET @counter = 1
SET @spaces = LEN(@qty)

WHILE @counter <= @qty
BEGIN
	SET @unitType = (RIGHT(REPLICATE('0', @spaces) + CAST(@counter AS VARCHAR), @spaces))
	INSERT INTO Locations (Site, Room, UnitType, Description, LoginID, Active, PublicAccess) VALUES (@site, @subsite, @unitType, @description, @login, @active, @public)
	SET @counter = @counter + 1
	PRINT 'Added: '+@site+' >> '+@subsite+' >> '+@unitType
END

SET NOCOUNT OFF

PRINT ''
PRINT 'COMPLETE: '+@site+' >> '+@subsite

And now, you have a place to put everything… in TMS at least.

HINT: I use this script to add locations that will not have numeric unit numbers, because I have found it is quicker to edit the unit numbers, then to add locations one by one.

4 Comments

  1. crikey you’re smart. I’m a fan. here’s hoping CI2010 extra training classes will help me along!

Leave a Comment