Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Matrix Question

Author  Topic 

nduggan23
Starting Member

42 Posts

Posted - 2007-12-18 : 11:59:16
have a procedure which doesnt look nice!!

I want to create a matrix of numbers, based on inputs from a user.

Can anyone show me a better way of doing this?



create procedure sp_createPlacements
(
@buildingID int = NULL,
@areaID int = NULL,
@typeID int = NULL,
@rows int = NULL
)
AS

BEGIN

DECLARE @units int
DECLARE @shelves int
DECLARE @slots int

SELECT @units = units, @shelves = shelves, @slots = slots
FROM [dbo].[PlacementType]
WHERE typeID = @typeID

DECLARE @rowCount int
DECLARE @unitCount int
DECLARE @shelfCount int
DECLARE @slotCount int

SET @rowCount = 0
SET @unitCount = 0
SET @shelfCount = 0
SET @slotCount = 0

WHILE @rowCount <= @rows
BEGIN
SET @rowCount = @rowCount + 1
WHILE @unitCount <= @units
BEGIN
SET @unitCount = @unitCount + 1
WHILE @shelfCount <= @shelves
BEGIN
SET @shelfCount = @shelfCount + 1
WHILE @slotCount <= @slots
BEGIN
SET @slotCount = @slotCount + 1
INSERT INTO [dbo].[Placement](buildingID, areaID, typeID, row, unit, shelf, slot)
VALUES(@buildingID, @areaID, @typeID, @rowCount, @unitCount, @shelfCount, @slotCount)
END
SET @slotCount = 0
END
SET @shelfCount = 0
END
SET @unitCount = 0
END

END

Ifor
Aged Yak Warrior

700 Posts

Posted - 2007-12-18 : 12:49:13
Create a Numbers/Tally table (plenty of examples on this site) and try something like:

INSERT INTO dbo.Placement (buildingID, areaID, typeID, row, unit, shelf, slot)
SELECT @buildingID, @areaID, @typeID, [Rows].Number, Units.Number, Shelves.Number, Slots.Number
FROM Numbers [Rows]
CROSS JOIN Numbers Units
CROSS JOIN Numbers Shelves
CROSS JOIN Numbers Slots
WHERE [Rows].Number <= @rows
AND Units.Number <= @units
AND Shelves.Number <= @shelves
AND Slots.Number <= @slots
Go to Top of Page
   

- Advertisement -