use [BDDAdminDB]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[IdentifyComputer]')
and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[IdentifyComputer]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[MachineNameSequence]')
and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[MachineNameSequence]
GO
CREATE TABLE [dbo].[MachineNameSequence] (
[Prefix] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Sequence] [int] NOT NULL
) ON [PRIMARY]
GO
INSERT INTO [dbo].[MachineNameSequence] (Prefix, Sequence) VALUES ('PC', 0)
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE [dbo].[IdentifyComputer]
@MacAddress CHAR(17),
@Make VARCHAR(50),
@Model VARCHAR(50)
AS
DECLARE @Cnt INT,
@Prefix VARCHAR(50),
@Sequence INT,
@NewName VARCHAR(50)
SET NOCOUNT ON
/* See if there is an existing record for this machine */
SELECT @Cnt=COUNT(*) FROM BDDAdminCore
WHERE MacAddress = @MacAddress
/* No record? Add one. */
IF @Cnt = 0
BEGIN
/* Create a new machine name */
BEGIN TRAN
SELECT @Prefix=Prefix, @Sequence=Sequence FROM MachineNameSequence
SET @Sequence = @Sequence + 1
UPDATE MachineNameSequence SET Sequence = @Sequence
SET @NewName = @Prefix + Right('00000'+LTrim(Str(@Sequence)),5)
/* Insert the new record */
INSERT INTO BDDAdminCore (MacAddress, Make, Model, ComputerName, OSDNewMachineName,
OSDInstallSilent, OSInstall)
VALUES (@MacAddress, @Make, @Model, @NewName, @NewName, '1', 'Y')
COMMIT TRAN
END
/* Return the record as the result set */
SELECT * FROM BDDAdminCore
WHERE MacAddress = @MacAddress
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO