Use @DBNAME

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
SET ANSI_PADDING ON
Begin Transaction createMessage

IF  Not EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[dbo].[Message]') AND type in (N'U'))
Begin
	CREATE TABLE [dbo].[Message](
		[MessageId] [int] IDENTITY(1,1) NOT NULL,
		[FromId] [char](15) NOT NULL,
		[ToId] [char](15) NOT NULL,
		[Subject] [nvarchar](100) NOT NULL,
		[Message] [nvarchar](2000) NOT NULL,
		[SentDate] [char](16) NOT NULL,
		[ReadDate] [char](16) NULL,
		[IsForced] [bit] NOT NULL,
	CONSTRAINT [PK_Message] PRIMARY KEY CLUSTERED 
	(
		[MessageId] ASC
	)
	) ON [PRIMARY]
End
Else
Begin
	ALTER TABLE Message
		ALTER COLUMN ToId CHAR(15) NOT NULL
End

IF  EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[dbo].[messages]') AND type in (N'U'))
Begin
    INSERT INTO Message
                          (FromId, ToId, Subject, Message, SentDate, ReadDate, IsForced)
    SELECT     messageFrom, messageTo, messageTitle, messageText, submitDate, N'' AS ReadDate, 0 AS IsForced
    FROM         messages

    DROP TABLE [dbo].[messages]
End


If (@@ERROR > 0 )
  Rollback Transaction createMessage
Else
    Commit Transaction createMessage
