Vous êtes sur la page 1sur 9

C:\Documents and Settings\eb010275\Local Settings\Temp\~vs124.

sql 1
USE [CsgDocs]
GO
/****** Object: StoredProcedure [dbo].[InputFromOldSystem] Script Date: 07/14/2009 17:
29:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[InputFromOldSystem]
/*
HI
This Is where we inport the data from the old datasheet system
PKB
*/
AS
SET NOCOUNT ON

DECLARE @Title nvarchar (100)


DECLARE @Description nvarchar (1000)
DECLARE @DocumentTypeId int
DECLARE @OwnerGroupId int
set @OwnerGroupId = 1 -- new setting not stored in the old table
DECLARE @SiteCase int
DECLARE @OldLookupLanguageId nvarchar(10)
DECLARE @LookupLanguageId int
DECLARE @DateReleased varchar(20)
DECLARE @RevisionDate varchar(20)
DECLARE @FileLocation nvarchar (2048)
DECLARE @OldDataSheetId int
DECLARE @tempdata_sheet_refs varchar(20)
DECLARE @salesZoneId int
DECLARE @MarketGroupId int
DECLARE @productGroupId int
-- valuse used to store return vales from inserts
DECLARE @DocumentID int
DECLARE @DownloadFileId int
DECLARE @TempProductGroupName nvarchar (200)
DECLARE @masterProductlistId int
DECLARE @part_no nvarchar (200)
DECLARE @OldTitle nvarchar (100)
DECLARE @data_sheet_refs nvarchar (100)
DECLARE @OrderNo int
/*
-- lets be good here !!!!
BEGIN TRANSACTION
-- lets clean the tables Watch the order
--exec('samplesystem.dbo.restoreSampleSystemOrdersFromTmp_orders') -- run the restore so
we have clean data
delete from DocumentToLookupSalesZone
delete from DocumentToLookupMarketGroup
delete from DocumentToLookupMasterProductList
delete from DocumentToLookupProductGroup
delete from DownloadFileToDocumentStore
delete from DownloadFile
delete from Document -- last!
-- reset seed value to keep the id nice!!
DBCC CHECKIDENT (DocumentToLookupMasterProductList, RESEED,0)
DBCC CHECKIDENT (DocumentToLookupSalesZone, RESEED,0)
DBCC CHECKIDENT (DocumentToLookupMarketGroup, RESEED,0)
DBCC CHECKIDENT (DocumentToLookupProductGroup, RESEED,0)
DBCC CHECKIDENT (DownloadFileToDocumentStore, RESEED,0)
DBCC CHECKIDENT (DownloadFile, RESEED,0)
DBCC CHECKIDENT (Document, RESEED,0)
COMMIT TRANSACTION
*/
-- cleaning done lets start to walk the old datasheet table

DECLARE xCURSOR CURSOR FAST_FORWARD FOR


SELECT -- top 1 -- for testing
xref.dbo.Datashts.Datasheet,
xref.dbo.Datashts.description,
xref.dbo.Datashts.document_type_id,
xref.dbo.Datashts.view_case_id,
xref.dbo.Datashts.Language_ref,
C:\Documents and Settings\eb010275\Local Settings\Temp\~vs124.sql 2
xref.dbo.Datashts.Release_Date,
xref.dbo.Datashts.Location,
xref.dbo.Datashts.sales_zone_id,
xref.dbo.Datashts.market_group_id,
xref.dbo.Datashts.product_group_id,
xref.dbo.Datashts.id

FROM xref.dbo.Datashts order by Datasheet -- in order so the data is nice


OPEN xCURSOR --
FETCH xCURSOR INTO @Title,@Description,@DocumentTypeId,@SiteCase,@OldLookupLanguageId,
@DateReleased,@FileLocation,@salesZoneId,@MarketGroupId,@
productGroupId,@OldDataSheetId
WHILE @@FETCH_STATUS = 0
BEGIN
BEGIN TRANSACTION -- this is for the commits for each row in the old datasheet
table + records so if it fails it rolls back and we don't get a part up load !!!

if (@SiteCase is null or @SiteCase = 77 )select @SiteCase = 15

INSERT INTO Document(


Title,
Description,
DocumentTypeId,
OwnerGroupId,
SiteCase,
UserGroupCase)
VALUES (
@Title,
@Description,
@DocumentTypeId,
@OwnerGroupId,
15,
'0,0,0,0,0')

select @DocumentId = SCOPE_IDENTITY() -- store the new id

-- insert into the linking tables for each new document


if (@salesZoneId is not null and @salesZoneId <> 0 )
begin
INSERT INTO DocumentToLookupSalesZone (DocumentId,SalesZoneId)values(@
DocumentId,isnull(@salesZoneId,-1))
end

if (@MarketGroupId is not null and @MarketGroupId <> 0)


begin
INSERT INTO DocumentToLookupMarketGroup (DocumentId,MarketGroupId)values(@
DocumentId,isnull(@MarketGroupId,-1))
end
-- remap the id to the new id's use gto as break

if (@productGroupId = 1)begin select @productGroupId = 5 goto setProdIdEnd end


if (@productGroupId = 2)begin select @productGroupId = 7 goto setProdIdEnd end
if (@productGroupId = 3)begin select @productGroupId = 8 goto setProdIdEnd end
if (@productGroupId = 5)begin select @productGroupId = 9 goto setProdIdEnd end
if (@productGroupId = 6)begin select @productGroupId = 10 goto setProdIdEnd
end
if (@productGroupId = 7)begin select @productGroupId = 11 goto setProdIdEnd
end
if (@productGroupId = 8)begin select @productGroupId = 12 goto setProdIdEnd
end
if (@productGroupId = 9)begin select @productGroupId = 14 goto setProdIdEnd
end
if (@productGroupId = 10)begin select @productGroupId = 17 goto setProdIdEnd
end
if (@productGroupId = 11)begin select @productGroupId = 18 goto setProdIdEnd
end
if (@productGroupId = 12)begin select @productGroupId = 20 goto setProdIdEnd
end
if (@productGroupId = 14)begin select @productGroupId = 21 goto setProdIdEnd
end
if (@productGroupId = 15)begin select @productGroupId = 8 goto setProdIdEnd
end
C:\Documents and Settings\eb010275\Local Settings\Temp\~vs124.sql 3
if (@productGroupId = 17)begin select @productGroupId = 22 goto setProdIdEnd
end
if (@productGroupId = 18)begin select @productGroupId = 23 goto setProdIdEnd
end
if (@productGroupId = 19)begin select @productGroupId = 24 goto setProdIdEnd
end
if (@productGroupId = 20)begin select @productGroupId = 25 goto setProdIdEnd
end
if (@productGroupId = 21)begin select @productGroupId = 26 goto setProdIdEnd
end
if (@productGroupId = 22)begin select @productGroupId = 27 goto setProdIdEnd
end

setProdIdEnd:
if (@productGroupId is not null and @productGroupId <> 0 and @productGroupId <>
16)
begin
INSERT INTO DocumentTolookupproductGroup (DocumentId,productGroupId)values
(@DocumentId,isnull(@productGroupId,-1))
end

-- We now need to fill the mamy to many table for the doc to part no
DECLARE PartCURSOR CURSOR FAST_FORWARD FOR
SELECT DISTINCT Lookup.dbo.master_product_list.u_ref, xref.dbo.
datasheet_to_part.part_no
FROM xref.dbo.datasheet_to_part RIGHT OUTER JOIN
Lookup.dbo.master_product_list ON Lookup.dbo.
master_product_list.part_no = xref.dbo.datasheet_to_part.part_no
WHERE ( xref.dbo.datasheet_to_part.datasheet_id = @OldDataSheetId)

OPEN PartCURSOR
FETCH PartCURSOR INTO @masterProductlistId,@part_no
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO DocumentToLookupMasterProductList (DocumentId,
masterProductlistId)values(@DocumentId,@masterProductlistId)

FETCH PartCURSOR INTO @masterProductlistId,@part_no


END
CLOSE PartCURSOR
DEALLOCATE PartCURSOR

-- Previous position of update sample system orders table


fileLoop:
-- get the ID from the Ref
select @LookupLanguageId = (Select ID from lookup.dbo.Languages where Language_ref = @
OldLookupLanguageId)

if ISDATE(@DateReleased)=1
begin
set @RevisionDate = convert(varchar(20),@DateReleased,101) -- set date into dd/
mm/yyyy
end
else
begin
set @DateReleased = convert(varchar(20),getdate(),101) -- set date into dd/mm/
yyyy
set @RevisionDate = convert(varchar(20),getdate(),101) -- set date into dd/mm/
yyyy
end

INSERT INTO DownloadFile(


DocumentId,
LookupLanguageId,
DateReleased,
RevisionDate,
FileLocation)
VALUES (
@DocumentId,
@LookupLanguageId,
@DateReleased,
@RevisionDate,
replace(lower(@FileLocation),'/resources','../..'))
SELECT @DownloadFileId = SCOPE_IDENTITY()
C:\Documents and Settings\eb010275\Local Settings\Temp\~vs124.sql 4

INSERT INTO DownloadFileToDocumentStore (DownloadFileId,DocumentStoreId)values(@


DownloadFileId,1)

-- We now need to update the samplesystem table with the new datasheet id
-- in order to stop us changing a number that we have already changed I have
add a X to the replace string
if isnull(@OldDataSheetId,-1) > -1
begin
DECLARE SampleOrdersCURSOR CURSOR FAST_FORWARD FOR
SELECT data_sheet_refs,Order_No
FROM samplesystem.dbo.Orders
WHERE (data_sheet_refs LIKE '%'+cast(@OldDataSheetId as varchar
(20))+',%')

OPEN SampleOrdersCURSOR
FETCH SampleOrdersCURSOR INTO @data_sheet_refs,@OrderNo
WHILE @@FETCH_STATUS = 0
BEGIN
set @tempdata_sheet_refs = replace(replace(','+cast(@
data_sheet_refs as varchar(20)) , ','+cast(@OldDataSheetId as varchar(20))+',' , ',X'+
cast(@DownloadFileId as varchar(20))+',' ) , ','+cast(@OldDataSheetId as varchar(20))+'
,' , ',X'+cast(@DownloadFileId as varchar(20))+',' )
update samplesystem.dbo.Orders set data_sheet_refs = substring(@
tempdata_sheet_refs,2,len(@tempdata_sheet_refs)) where Order_No = @OrderNo
FETCH SampleOrdersCURSOR INTO @data_sheet_refs,@OrderNo
END
CLOSE SampleOrdersCURSOR
DEALLOCATE SampleOrdersCURSOR
end
set @OldTitle = @Title
FETCH xCURSOR INTO @Title,@Description,@DocumentTypeId,@SiteCase,@OldLookupLanguageId,
@DateReleased,@FileLocation,@salesZoneId,@MarketGroupId,@
productGroupId,@OldDataSheetId
--print (@OldTitle +' = '+ @Title )
if (@OldTitle = @Title and @@FETCH_STATUS = 0) goto fileLoop -- loop back for each file

COMMIT TRANSACTION

END

CLOSE xCURSOR
DEALLOCATE xCURSOR
-- in order to stop us changing a number we add a X to the replace string
-- now we need to remove this X
update samplesystem.dbo.Orders set data_sheet_refs = replace(data_sheet_refs ,'X','') where
data_sheet_refs LIKE '%X%'
--- if we get here! I am either lucky or clever !!!!!!!!!
RETURN
C:\Documents and Settings\eb010275\Local Settings\Temp\~vs128.sql 1
USE [CsgDocs]
GO
/****** Object: StoredProcedure [dbo].[InputFromOldSystem_1_CleanUp] Script Date: 07/14
/2009 17:29:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[InputFromOldSystem_1_CleanUp]
/*
HI

This Is where we inport the data from the old datasheet system

PKB
*/
AS
SET NOCOUNT ON

-- lets be good here !!!!


BEGIN TRANSACTION
-- lets clean the tables Watch the order
--exec('samplesystem.dbo.restoreSampleSystemOrdersFromTmp_orders') -- run the restore so
we have clean data

truncate TABLE "DocumentToLookupSalesZone"


truncate TABLE "DocumentToLookupMarketGroup"
truncate TABLE "DocumentToLookupMasterProductList"
truncate TABLE "DocumentToLookupProductGroup"
truncate TABLE "DocumentToLookupProductFamily" -- andy 17-06-05 added these it still
doesnt work!
truncate TABLE "DownloadFileToDocumentStore"
truncate TABLE "FileExtraInfo" -- andy 17-06-05
truncate TABLE "FileOverrideURL" -- andy 17-06-05
truncate TABLE "DownloadLog"
--truncate TABLE "Thumbnail"
delete from DownloadFile -- use truncate with FOREIGN KEY
constraint.it seems !! pkb
--truncate TABLE "DownloadFile"
delete from Document
--truncate TABLE "Document" -- last!

-- reset seed value to keep the id nice!!


DBCC CHECKIDENT (DocumentToLookupSalesZone, RESEED,0)
DBCC CHECKIDENT (DocumentToLookupMarketGroup, RESEED,0)
DBCC CHECKIDENT (DocumentToLookupMasterProductList, RESEED,0)
DBCC CHECKIDENT (DocumentToLookupProductGroup, RESEED,0)
DBCC CHECKIDENT (DocumentToLookupProductFamily, RESEED,0) -- andy 17-06-05
DBCC CHECKIDENT (DownloadFileToDocumentStore, RESEED,0)
DBCC CHECKIDENT (FileExtraInfo, RESEED,0) -- andy 17-06-05
DBCC CHECKIDENT (FileOverrideURL, RESEED,0) -- andy 17-06-05
DBCC CHECKIDENT (Downloadlog, RESEED,0)
DBCC CHECKIDENT (DownloadFile, RESEED,0)
DBCC CHECKIDENT (Document, RESEED,0)

--DBCC CHECKIDENT (Thumbnail, RESEED,0)

update xref.dbo.datasheet_to_part set new_id = '' where new_id <> ''


COMMIT TRANSACTION

-- cleaning done lets start to walk the old datasheet table


C:\Documents and Settings\eb010275\Local Settings\Temp\~vs12F.sql 1
USE [CsgDocs]
GO
/****** Object: StoredProcedure [dbo].[InputFromOldSystem_2_insertIntoCscDocs] Script
Date: 07/14/2009 17:29:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[InputFromOldSystem_2_insertIntoCscDocs]
/*
HI

This Is where we inport the data from the old datasheet system

PKB
*/
AS
SET NOCOUNT ON

DECLARE @Title nvarchar (100)


DECLARE @Description nvarchar (1000)
DECLARE @DocumentTypeId int
DECLARE @OwnerGroupId int
set @OwnerGroupId = 1 -- new setting not stored in the old table
DECLARE @SiteCase int
set @SiteCase = 5 -- new setting not stored in the old table
DECLARE @UserGroupCase int

DECLARE @OldLookupLanguageId nvarchar(10)


DECLARE @LookupLanguageId int
DECLARE @DateReleased varchar(20)
DECLARE @RevisionDate varchar(20)
DECLARE @FileLocation nvarchar (2048)
DECLARE @OldDataSheetId int
DECLARE @tempdata_sheet_refs varchar(20)

DECLARE @salesZoneId int


DECLARE @MarketGroupId int
DECLARE @productGroupId int

-- valuse used to store return vales from inserts


DECLARE @DocumentID int
DECLARE @DownloadFileId int
DECLARE @TempProductGroupName nvarchar (200)
DECLARE @masterProductlistId int
DECLARE @dummby nvarchar (200)
DECLARE @OldTitle nvarchar (100)
DECLARE @data_sheet_refs nvarchar (100)
DECLARE @OrderNo int
DECLARE @FileSizeInKb int
set @FileSizeInKb = 0

DECLARE xCURSOR CURSOR FAST_FORWARD FOR


SELECT top 10 -- for testing
xref.dbo.Datashts.Datasheet,
xref.dbo.Datashts.description,
xref.dbo.Datashts.document_type_id,
xref.dbo.Datashts.view_case_id,
xref.dbo.Datashts.Language_ref,
xref.dbo.Datashts.Release_Date,
xref.dbo.Datashts.Location,
xref.dbo.Datashts.sales_zone_id,
xref.dbo.Datashts.market_group_id,
xref.dbo.Datashts.division,
xref.dbo.Datashts.id

FROM xref.dbo.Datashts order by Datasheet -- in oreder so the data to nice

OPEN xCURSOR --
FETCH xCURSOR INTO @Title,@Description,@DocumentTypeId,@UserGroupCase,@
OldLookupLanguageId,
@DateReleased,@FileLocation,@salesZoneId,@MarketGroupId,@
productGroupId,@OldDataSheetId
C:\Documents and Settings\eb010275\Local Settings\Temp\~vs12F.sql 2
WHILE @@FETCH_STATUS = 0
BEGIN
-- BEGIN TRANSACTION -- this is for the commits for each row in the old datasheet
table + records so if it fails it rolls back and we don't get a part up load !!!

exec InsertDocument @Title,@Description,@DocumentTypeId,@OwnerGroupId,@SiteCase,@


UserGroupCase,@DocumentId out

-- this returns the @DocumentId

-- insert into the linking tables for each new document


set @salesZoneId = isnull(@salesZoneId,1)
exec InsertDocumentToLookupSalesZone @DocumentId,@salesZoneId
set @MarketGroupId = isnull(@MarketGroupId,1)
exec InsertDocumentToLookupMarketGroup @DocumentId,@MarketGroupId
set @productGroupId = isnull(@productGroupId,1)
exec InsertDocumentToLookupProductGroup @DocumentId,@productGroupId

-- We now need to fill the mamy to many table for the doc to part no
DECLARE PartCURSOR CURSOR FAST_FORWARD FOR
SELECT DISTINCT Lookup.dbo.master_product_list.u_ref
FROM xref.dbo.datasheet_to_part RIGHT OUTER JOIN
Lookup.dbo.master_product_list ON Lookup.dbo.
master_product_list.part_no LIKE xref.dbo.datasheet_to_part.part_no
WHERE ( xref.dbo.datasheet_to_part.datasheet_id = @OldDataSheetId)

OPEN PartCURSOR
FETCH PartCURSOR INTO @masterProductlistId
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO DocumentToLookupMasterProductList (DocumentId,
masterProductlistId)values(@DocumentId,@masterProductlistId)

FETCH PartCURSOR INTO @masterProductlistId


END
CLOSE PartCURSOR
DEALLOCATE PartCURSOR

-- We now need to update the samplesystem table with the new datasheet id
-- in order to stop us changing a number that we have already changed I have
add a X to the replace string

update samplesystem.dbo.Orders set data_sheet_refs = substring(


replace(replace(','+cast(data_sheet_refs as varchar(20)) , ','+cast(@OldDataSheetId as
varchar(20))+',' , ',X'+cast(@DocumentId as varchar(20))+',' ) , ','+cast(@
OldDataSheetId as varchar(20))+',' , ',X'+cast(@DocumentId as varchar(20))+',' )
,2,len(
replace(replace(','+cast(data_sheet_refs as varchar(20)) , ','+cast(@OldDataSheetId as
varchar(20))+',' , ',X'+cast(@DocumentId as varchar(20))+',' ) , ','+cast(@
OldDataSheetId as varchar(20))+',' , ',X'+cast(@DocumentId as varchar(20))+',' )))
where data_sheet_refs LIKE '%'+cast(@OldDataSheetId as varchar(20))+',%'

fileLoop:
-- get the ID from the Ref
select @LookupLanguageId = (Select ID from lookup.dbo.Languages where Language_ref = @
OldLookupLanguageId)

if ISDATE(@DateReleased)=1
begin
set @RevisionDate = convert(varchar(20),@DateReleased,101) -- set date into dd/
mm/yyyy
end
else
begin
set @DateReleased = convert(varchar(20),getdate(),101) -- set date into dd/mm/
yyyy
set @RevisionDate = convert(varchar(20),getdate(),101) -- set date into dd/mm/
yyyy
end
C:\Documents and Settings\eb010275\Local Settings\Temp\~vs12F.sql 3
set @FileLocation = replace(lower(@FileLocation),'/resources','')
print (@DocumentId)
exec InsertDownloadFile @DocumentId,@LookupLanguageId,@DateReleased,@RevisionDate,@
FileLocation,@FileSizeInKb,'','','',@DownloadFileId out
exec InsertDownloadFileToDocumentStore @DownloadFileId,1
-- add the new data sheet id into the old data sheet table
update xref.dbo.datasheet_to_part set new_id = @DownloadFileId where datasheet_id = @
OldDataSheetId

set @OldTitle = @Title


--COMMIT TRANSACTION
FETCH xCURSOR INTO @Title,@Description,@DocumentTypeId,@UserGroupCase,@
OldLookupLanguageId,
@DateReleased,@FileLocation,@salesZoneId,@MarketGroupId,@
productGroupId,@OldDataSheetId
--print (@OldTitle +' = '+ @Title )
if (@OldTitle = @Title and @@FETCH_STATUS = 0) goto fileLoop -- loop back for each file

END
CLOSE xCURSOR
DEALLOCATE xCURSOR
-- in order to stop us changing a number we add a X to the replace string
-- now we need to remove this X
update samplesystem.dbo.Orders set data_sheet_refs = replace(data_sheet_refs ,'X','') where
data_sheet_refs LIKE '%X%'
-- swop the coums around in the datasheet_to_part table and remove the new_id col
--exec InputFromOldSystem_3_swopColumsInDataSheetToPart
return
C:\Documents and Settings\eb010275\Local Settings\Temp\~vs133.sql 1
USE [CsgDocs]
GO
/****** Object: StoredProcedure [dbo].[InputFromOldSystem_3_swopColumsInDataSheetToPart]
Script Date: 07/14/2009 17:29:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[InputFromOldSystem_3_swopColumsInDataSheetToPart]
/*
HI
we take the data from the new_id colum and put into the datasheet_id col
and remove the new_id colum
PKB
*/
AS
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
CREATE TABLE dbo.Tmp_datasheet_to_part
(
datasheet_id int NOT NULL,
part_no varchar(501) NOT NULL
) ON [PRIMARY]
IF EXISTS(SELECT * FROM dbo.datasheet_to_part)
EXEC('INSERT INTO dbo.Tmp_datasheet_to_part (datasheet_id, part_no)
SELECT distinct datasheet_id, new_id FROM dbo.datasheet_to_part (HOLDLOCK TABLOCKX)
')
DROP TABLE dbo.datasheet_to_part
EXECUTE sp_rename N'dbo.Tmp_datasheet_to_part', N'datasheet_to_part', 'OBJECT'
ALTER TABLE dbo.datasheet_to_part ADD CONSTRAINT
PK_datasheet_to_part PRIMARY KEY CLUSTERED
(
datasheet_id,
part_no
) ON [PRIMARY]
GRANT SELECT ON dbo.datasheet_to_part TO Xrefread AS dbo
GRANT REFERENCES ON dbo.datasheet_to_part TO Xrefupd AS dbo
GRANT SELECT ON dbo.datasheet_to_part TO Xrefupd AS dbo
GRANT UPDATE ON dbo.datasheet_to_part TO Xrefupd AS dbo
GRANT INSERT ON dbo.datasheet_to_part TO Xrefupd AS dbo
GRANT DELETE ON dbo.datasheet_to_part TO Xrefupd AS dbo
COMMIT
-- cleaning done lets start to walk the old datasheet table

Vous aimerez peut-être aussi