IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[ProductDescription]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
CREATE TABLE [dbo].[ProductDescription]
(
	[FK_ProductNumber] [int] NOT NULL,
	[Type] [int] NOT NULL,
	[Description] [text] NULL,
 CONSTRAINT [PK_ProductDescription] PRIMARY KEY CLUSTERED 
(
	[FK_ProductNumber] ASC,
	[Type] ASC
))
GO

INSERT INTO ProductDescription
SELECT ProductNumber, 1, TextDescription 
FROM Products(NOLOCK) WHERE DATALENGTH(TextDescription)>1
GO

IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[ProductExtended]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
CREATE TABLE [dbo].[ProductExtended](
	[FK_ProductNumber] [int] NOT NULL,
	[ProductCourseNumber] [varchar](50) NULL,
	[ProductCourseStatus] [bit] NULL,
	[FK_CertificateNumberTemplate] [int] NULL,
	[FK_CertificateTemplate] [int] NULL,
	[MonthDuration] [int] NULL,
	[DayCorrection] [int] NULL,
	[IsOPITOCourse] [bit] NULL,
 CONSTRAINT [PK_ProductExtended] PRIMARY KEY CLUSTERED 
(
	[FK_ProductNumber] ASC
))
GO

DECLARE @cnt INT

SELECT @cnt = COUNT(*) FROM SysColumns
INNER JOIN SysObjects ON Syscolumns.Id = SysObjects.Id
AND SysObjects.Name = 'Products'
AND SysColumns.Name = 'ProductCourseNumber'

IF ISNULL(@cnt, 0) > 0
BEGIN
  DECLARE @sql VARCHAR(8000)
  SET @sql = 'INSERT INTO ProductExtended SELECT ProductNumber, ProductCourseNumber, ProductCourseStatus, FK_CertificateNumberTemplate, FK_CertificateTemplate, NULL, NULL, NULL ' +
  'FROM Products(NOLOCK) WHERE ISNULL(Products.ProductCourseNumber, '''') <> '''' OR ISNULL(Products.ProductCourseStatus, 0) <> 0 OR ISNULL(Products.FK_CertificateNumberTemplate, 0) <> 0 OR ISNULL(Products.FK_CertificateTemplate, 0) <> 0'
  EXEC sp_sqlexec @sql
END