--在博卡数据库中,分两步执行该脚本 --第一步, 开始 /* 清理 select ' EXEC PDropColumn ''' + a.name + ''', ''' + b.name +'''' FROM sysobjects as a,syscolumns as b WHERE a.id = b.id AND b.name IN('FId','FMemId','FCardId','FItemId','FCourseId','FNo','FMemNo','FCardNo','FFromChild', 'FCardTypeId','FTypeId','FMasterId','FPayTypeId') DROP TABLE TInventory */ use yangquan_meishu_bk IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'haa46i' AND id = object_id('ham01')) BEGIN ALTER TABLE ham01 ADD haa46i INT DEFAULT 1 END IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'gak44i' AND id = object_id('gam10')) BEGIN ALTER TABLE gam10 ADD gak44i INT DEFAULT 1 END IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FId' AND id = object_id('gam01')) ALTER TABLE gam01 ADD FId VARCHAR(20) NOT NULL DEFAULT abs( CAST( CAST(newid() AS BINARY(16)) AS BIGINT)) IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FId' AND id = object_id('gam04')) ALTER TABLE gam04 ADD FId VARCHAR(20) NOT NULL DEFAULT abs( CAST( CAST(newid() AS BINARY(16)) AS BIGINT)) IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FId' AND id = object_id('gdm01')) ALTER TABLE gdm01 ADD FId VARCHAR(20) NOT NULL DEFAULT abs( CAST( CAST(newid() AS BINARY(16)) AS BIGINT)) IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FNo' AND id = object_id('gdm01')) ALTER TABLE gdm01 ADD FNo VARCHAR(30) NOT NULL DEFAULT '' IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FFromChild' AND id = object_id('gdm01')) ALTER TABLE gdm01 ADD FFromChild BIT NOT NULL DEFAULT 0 IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FCourseId' AND id = object_id('gdm01')) ALTER TABLE gdm01 ADD FCourseId VARCHAR(30) NOT NULL DEFAULT abs( CAST( CAST(newid() AS BINARY(16)) AS BIGINT)) IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FId' AND id = object_id('gfm01')) ALTER TABLE gfm01 ADD FId VARCHAR(20) NOT NULL DEFAULT abs( CAST( CAST(newid() AS BINARY(16)) AS BIGINT)) IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FNo' AND id = object_id('gfm01')) ALTER TABLE gfm01 ADD FNo VARCHAR(20) NOT NULL DEFAULT '' IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FFromChild' AND id = object_id('gfm01')) ALTER TABLE gfm01 ADD FFromChild BIT NOT NULL DEFAULT 0 IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FId' AND id = object_id('ham01')) ALTER TABLE ham01 ADD FId VARCHAR(20) NOT NULL DEFAULT abs( CAST( CAST(newid() AS BINARY(16)) AS BIGINT)) IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FId' AND id = object_id('gsm02')) ALTER TABLE gsm02 ADD FId VARCHAR(20) NOT NULL DEFAULT abs( CAST( CAST(newid() AS BINARY(16)) AS BIGINT)) IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FItemId' AND id = object_id('hem01')) ALTER TABLE hem01 ADD FItemId BIGINT NOT NULL DEFAULT 0 IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FPayTypeId' AND id = object_id('hem01')) ALTER TABLE hem01 ADD FPayTypeId BIGINT NOT NULL DEFAULT 0 IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FItemId' AND id = object_id('gdm06')) ALTER TABLE gdm06 ADD FItemId VARCHAR(20) NOT NULL DEFAULT '' IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FId' AND id = object_id('gdm06')) ALTER TABLE gdm06 ADD FId VARCHAR(20) NOT NULL DEFAULT abs( CAST( CAST(newid() AS BINARY(16)) AS BIGINT)) IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FId' AND id = object_id('gdm07')) ALTER TABLE gdm07 ADD FId VARCHAR(20) NOT NULL DEFAULT abs( CAST( CAST(newid() AS BINARY(16)) AS BIGINT)) IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FMasterId' AND id = object_id('gdm07')) ALTER TABLE gdm07 ADD FMasterId VARCHAR(20) NOT NULL DEFAULT 0 IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FPayTypeId' AND id = object_id('gdm07')) ALTER TABLE gdm07 ADD FPayTypeId VARCHAR(20) NOT NULL DEFAULT 0 IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FCardTypeId' AND id = object_id('gdm07')) ALTER TABLE gdm07 ADD FCardTypeId VARCHAR(20) NOT NULL DEFAULT 0 IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FId' AND id = object_id('gam10')) ALTER TABLE gam10 ADD FId VARCHAR(20) NOT NULL DEFAULT abs( CAST( CAST(newid() AS BINARY(16)) AS BIGINT)) --消耗 IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FItemId' AND id = object_id('gfm05')) ALTER TABLE gfm05 ADD FItemId BIGINT , FGoodsId BIGINT , FUnitId BIGINT --提成方案 IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FEmpId' AND id = object_id('hem02')) ALTER TABLE hem02 ADD FEmpId VARCHAR(20) NOT NULL DEFAULT 0 IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FItemId' AND id = object_id('hem02')) ALTER TABLE hem02 ADD FItemId VARCHAR(20) NOT NULL DEFAULT 0 --会员 --ALTER TABLE gbm01 ADD FId VARCHAR(20) NOT NULL DEFAULT abs( CAST( CAST(newid() AS BINARY(16)) AS BIGINT)) IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FMemId' AND id = object_id('gcm01')) ALTER TABLE gcm01 ADD FMemId VARCHAR(20) NOT NULL DEFAULT abs( CAST( CAST(newid() AS BINARY(16)) AS BIGINT)), FCardId VARCHAR(20) NOT NULL DEFAULT abs( CAST( CAST(newid() AS BINARY(16)) AS BIGINT)), FCardTypeId VARCHAR(20) NOT NULL DEFAULT 0 IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FMemId' AND id = object_id('gbm01')) ALTER TABLE gbm01 ADD FMemId VARCHAR(20) NOT NULL DEFAULT '' IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FMemId' AND id = object_id('gcm03')) ALTER TABLE gcm03 ADD FMemId VARCHAR(20) NOT NULL DEFAULT 0, FCardId VARCHAR(20) NOT NULL DEFAULT 0 IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FId' AND id = object_id('gcm06')) ALTER TABLE gcm06 ADD FId VARCHAR(20) NOT NULL DEFAULT abs( CAST( CAST(newid() AS BINARY(16)) AS BIGINT)), FMemId VARCHAR(20) NOT NULL DEFAULT 0, FCardId VARCHAR(20) NOT NULL DEFAULT 0, FItemId VARCHAR(20) NOT NULL DEFAULT 0, FCourseId VARCHAR(20) NOT NULL DEFAULT 0 IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FMemId' AND id = object_id('gcm04')) ALTER TABLE gcm04 ADD FMemId VARCHAR(20) NOT NULL DEFAULT 0, FCardId VARCHAR(20) NOT NULL DEFAULT 0 IF NOT EXISTS(SELECT TOP 1 1 FROM syscolumns WHERE name = 'FMemId' AND id = object_id('gcm20')) ALTER TABLE gcm20 ADD FMemId VARCHAR(20) NOT NULL DEFAULT 0, FCardId VARCHAR(20) NOT NULL DEFAULT 0 --第一步,结束 ----------------------------------------------------------------------------------------------------- --第二步, 开始 --如果基本资料已经导完, 就再也不要执行第二步 --连锁店统一项目和产品编号,卡类别 IF (SELECT COUNT(1) FROM gam05) > 1 BEGIN ---------------------------------------------- --处理项目编号,ID,疗程ID,重复的项目 delete gdm01 where FFromChild = 1 UPDATE gdm01 SET FNo = gda01c select * into #gdm01 from gdm01 where 1=2 declare @CompNo VARCHAR(200) declare #Companys cursor for select distinct gae01c from gam05 where gae01c != '001' open #Companys fetch #Companys into @CompNo while @@fetch_status=0 begin delete #gdm01 --更新重复的 --declare @CompNo VARCHAR(200) -- select @compNo = '008' insert #gdm01 select b.* from (select gda01c,gda03c,FId from gdm01 where gda00c = '001') as a, (select * from gdm01 where gda00c = @CompNo) as b where (a.gda01c = b.gda01c ) and a.gda03c != b.gda03c and b.gda01c + '.' + @CompNo not in (select gda01c from gdm01 where gda00c = '001') update #gdm01 set FNo = gda01c + '.' + @CompNo update a set a.FNo = b.FNo from gdm01 as a,#gdm01 as b where a.gda00c = @CompNo and a.gda01c = b.gda01c update #gdm01 set gda01c = FNo ,gda00c = '001',FFromChild = 1 insert gdm01 select * from #gdm01 delete #gdm01 --获取001公司没有的 insert #gdm01 select * from gdm01 where gda00c = @CompNo and gda01c not in (select gda01c from gdm01 where gda00c = '001') update #gdm01 set gda00c = '001',FFromChild = 1 --插入001公司 INSERT gdm01 select * from #gdm01 fetch #Companys into @CompNo end close #Companys deallocate #Companys drop table #gdm01 update gdm01 set FCourseId =abs( CAST( CAST(newid() AS BINARY(16)) AS BIGINT)) --更新根据编号更新ID update a set a.FId = b.FId , a.FCourseId = b.FCourseId FROM gdm01 AS A, (select FNo,FId,FCourseId from gdm01 where gda00c = '001') AS B WHERE a.gda00c NOT IN('','001') AND a.FNo = B.FNo ---------------------------- --处理产品 --处理项目编号,ID,疗程ID,重复的项目 delete gfm01 where FFromChild = 1 UPDATE gfm01 SET FNo = gfa01c select * into #gfm01 from gfm01 where 1=2 -- declare @CompNo VARCHAR(200) declare #Companys cursor for select distinct gae01c from gam05 where gae01c != '001' open #Companys fetch #Companys into @CompNo while @@fetch_status=0 begin delete #gfm01 --更新重复的 --declare @CompNo VARCHAR(200) -- select @compNo = '008' insert #gfm01 select b.* from (select gfa01c,gfa03c,FId from gfm01 where gfa00c = '001') as a, (select * from gfm01 where gfa00c = @CompNo) as b where (a.gfa01c = b.gfa01c ) and a.gfa03c != b.gfa03c and b.gfa01c + '.' + @CompNo not in (select gfa01c from gfm01 where gfa00c = '001') update #gfm01 set FNo = gfa01c + '.' + @CompNo update a set a.FNo = b.FNo from gfm01 as a,#gfm01 as b where a.gfa00c = @CompNo and a.gfa01c = b.gfa01c update #gfm01 set gfa01c = FNo ,gfa00c = '001',FFromChild = 1 insert gfm01 select * from #gfm01 delete #gfm01 --获取001公司没有的 insert #gfm01 select * from gfm01 where gfa00c = @CompNo and gfa01c not in (select gfa01c from gfm01 where gfa00c = '001') update #gfm01 set gfa00c = '001',FFromChild = 1 --插入001公司 INSERT gfm01 select * from #gfm01 fetch #Companys into @CompNo end close #Companys deallocate #Companys drop table #gfm01 --更新根据编号更新ID update a set a.FId = b.FId FROM gfm01 AS A, (select FNo,FId from gfm01 where gfa00c = '001') AS B WHERE a.gfa00c NOT IN('','001') AND a.FNo = B.FNo -------------------------------------------------- --卡类别 select * into #gam10 from gam10 where 1=2 --declare @compNo varchar(10) declare dbList cursor for select distinct gae01c from gam05 where gae01c != '001' open dbList fetch dbList into @compNo while @@fetch_status=0 begin insert #gam10 select * FROM gam10 WHERE gak01c NOT IN(SELECT gak01c FROM gam10 WHERE gak00c = '001') and gak00c = @compNo and gak01c not in (select gak01c from #gam10) fetch dbList into @compNo end close dbList deallocate dbList UPDATE #gam10 SET gak00c = '001' INSERT gam10 SELECT * FROM #gam10 DROP TABLE #gam10 update a set a.FId = b.FId FROM gam10 AS A, (select gak01c,FId from gam10 where gak00c = '001') AS B WHERE a.gak00c NOT IN('','001') AND a.gak01c = B.gak01c END ELSE BEGIN UPDATE gfm01 SET FNo = gfa01c UPDATE gdm01 SET FNo = gda01c END --拷贝到001 /* select * into #gam10 from gam10 where 1=2 declare @compNo varchar(10) declare dbList cursor for select distinct gae01c from gam05 where gae01c != '001' open dbList fetch dbList into @compNo while @@fetch_status=0 begin insert #gam10 select * FROM gam10 WHERE gak01c NOT IN(SELECT gak01c FROM gam10 WHERE gak00c = '001') and gak00c = @compNo and gak01c not in (select gak01c from #gam10) fetch dbList into @compNo end close dbList deallocate dbList UPDATE #gam10 SET gak00c = '001' INSERT gam10 SELECT * FROM #gam10 drop table #gam10 */ UPDATE A SET A.FId = B.FId FROM Gsm02 AS A,(SELECT gsb01c,gsb02c,FId FROM gsm02 WHERE gsb00c = '001') AS B WHERE A.gsb00c != '001' AND A.gsb01c = B.gsb01c AND A.gsb02c = B.gsb02c --新建不存在的卡类别 INSERT gam10(gak00c,gak01c,gak02c,gak03i) SELECT distinct'001', gca02c,'不存在的卡类别',1 FROM gcm01 WHERE gca00c = gca13d AND gca01c NOT LIKE '%散客%' AND gca02c NOT IN(select gak01c FROM gam10) --项目提成 UPDATE A SET A.FItemId = B.FId FROM gdm01 AS B, hem01 AS A WHERE B.gda00c = A.hea00c AND A.hea01c = B.gda01c --提成方案 UPDATE A SET A.FEmpId = B.FId FROM hem02 AS A,ham01 AS B WHERE A.heb01c = B.haa01c AND A.heb00c = B.haa00c UPDATE A SET A.FItemID = B.FId FROM hem02 AS A,gdm01 AS B WHERE A.heb02c = B.gda01c AND A.heb00c = B.gda00c --折扣主档 UPDATE A SET A.FMasterId = B.FId FROM gdm06 AS B,gdm07 AS A WHERE B.gdf00c = A.gdg00c AND B.gdf01c = A.gdg01c AND b.gdf02c = A.gdg02c --项目折扣主档 UPDATE gdm06 SET FItemId = gsm02.FId FROM gsm02 WHERE gsb01c = 'L' AND gsb00c = '001' AND gsb02c = gdf01c AND gdf02c = '1' /* EXEC PExecImportSQL ' UPDATE A a SET A.FPayTypeId = B.FId FROM {FDB}.gdm07 AS A,TPayAccount AS B WHERE A.gdg11i = B.FBKAcctId */ -- AND a.gdg00c =''006'' -- ' UPDATE A SET A.FCardTypeId = B.FId FROM gdm07 AS A ,GAM10 AS B WHERE A.gdg03c = B.gak01c AND A.gdg00c = B.gak00c --产品折扣主档 UPDATE gdm06 SET FItemId = gsm02.FId FROM gsm02 WHERE gsb01c = 'J' AND gsb00c = '001' AND gsb02c = gdf01c AND gdf02c = '2' --消耗 UPDATE A SET A.FItemId = B.FId FROM gfm05 AS A,gdm01 AS B WHERE A.gfe01c = gda01c AND a.gfe00c = gda00c UPDATE A SET A.FGoodsId = B.FId, A.FUnitId = (SELECT FId FROM gsm02 WHERE gsb00c = '001' AND gsb01c = 'k' AND gsb02c = ISNULL(gfa08c,gfa07c)) FROM gfm05 AS A,gfm01 AS B WHERE A.gfe02c = gfa01c AND a.gfe00c = gfa00c -----------------第二步结束------------------ -----如果基本资料已经导完, 就再也不要执行第二步 -----------------第三步------------------ --更新会员资料与基本资料的绑定关系 --如果执行完这步后, 又产生了业务单据, 必须再执行这一步(只单独执行这一步) --select * from gcm03 where FMemId = '0' --select * from gfm01 where FFromChild = 1 IF EXISTS(SELECT 1 FROM gcm06,gcm01 WHERE gcf00c = gca00c AND gca00c = gca13d AND gca01c = gcf01c AND gcf02c NOT IN(SELECT gda01c FROM gdm01) AND gcf02c != '') BEGIN SELECT '注意注意, 执行失败, 下面这些会员的疗程项目不存在,但是已经有会员使用' SELECT GCM06.* FROM gcm06,gcm01 WHERE gcf00c = gca00c AND gca00c = gca13d AND gca01c = gcf01c AND gcf02c NOT IN(SELECT gda01c FROM gdm01) AND gcf02c != '' return END --会员基本资料 UPDATE A SET A.FMemId = B.FMemId FROM gcm01 AS B,gbm01 AS A WHERE A.gba01c = B.gca04c AND B.gca00c = B.gca13d --卡类别 UPDATE A SET A.FCardTypeId = B.FId FROM gcm01 AS A,gam10 AS B WHERE A.gca00c = A.gca13d AND A.gca02c = B.gak01c AND A.gca00c = B.gak00c --卡类别 UPDATE A SET A.FCardTypeId = B.FId FROM gcm01 AS A,gam10 AS B WHERE A.gca00c = A.gca13d AND A.gca02c = B.gak01c AND '001'= B.gak00c --没有的取001 AND ISNULL(FCardTypeId,'0') = '0' --会员账户 UPDATE A SET A.FMemId = B.FMemId, A.FCardId = B.FCardId FROM gcm03 AS A,gcm01 AS B WHERE A.gcc01c = B.gca01c AND B.gca00c = B.gca13d --疗程 UPDATE A SET A.FMemId = B.FMemId, A.FCardId = B.FCardId FROM gcm06 AS A,gcm01 AS B WHERE A.gcf01c = B.gca01c AND B.gca00c = B.gca13d --账户历史 UPDATE A SET A.FMemId = B.FMemId, A.FCardId = B.FCardId FROM gcm04 AS A,gcm01 AS B WHERE A.gcd01c = B.gca01c AND B.gca00c = B.gca13d --交易历史 UPDATE A SET A.FMemId = B.FMemId, A.FCardId = B.FCardId FROM gcm20 AS A,gcm01 AS B WHERE A.gct02c = B.gca01c AND B.gca00c = B.gca13d --会员疗程ID UPDATE A SET A.FItemId = B.FId, A.FCourseId = B.FCourseId FROM gcm06 AS A,gcm01 AS C,gdm01 AS B WHERE A.gcf00c = C.gca00c AND C.gca00c = C.gca13d AND A.gcf01c = C.gca01c AND A.gcf02c = B.gda01c AND A.gcf00c = B.gda00c --库存 EXEC ('IF OBJECT_ID(''dbo.TInventory'') IS NOT NULL drop table dbo.TInventory') EXEC ('create table dbo.TInventory ( FCompNo VARCHAR(50) NOT NULL DEFAULT '''', FGoodsId BIGINT NOT NULL DEFAULT 0, goodsNo varchar(50), goodsName varchar(50), FStoreId BIGINT NOT NULL DEFAULT 0, storeNo varchar(50), storeName varchar(50), goodsType varchar(50), FPrice float, FUnitId BIGINT NOT NULL DEFAULT 0, cost varchar(50), unit varchar(50), quantity varchar(50), amt_cost varchar(50), FAmount float, FQuantity float, stattype varchar(50), goodspec varchar(100), ) CREATE NONCLUSTERED INDEX IX_INVENTORY ON dbo.TInventory(FGoodsId) ') declare @CompNo VARCHAR(200) declare @Today VARCHAR(20) SELECT @Today = CONVERT(VARCHAR(20),GETDATE(),112) PRINT @Today declare #Companys cursor for select distinct gae01c from gam05-- where gae01c = '001' open #Companys fetch #Companys into @CompNo while @@fetch_status=0 begin insert TInventory(goodsNo,goodsName,storeNo,storeName,goodsType,Fprice,cost,unit,quantity,amt_cost,FAmount,FQuantity,stattype)--,goodspec) exec upg_stock_statistic @compNo,@Today,'*','','*','',5,0,'*','*','',0 UPDATE TInventory SET FCompNo = @CompNo WHERE FCompNo = '' fetch #Companys into @CompNo END close #Companys deallocate #Companys UPDATE A SET A.FGoodsId = B.FId FROM TInventory AS A,gfm01 AS B WHERE A.goodsNo = B.gfa01c AND B.gfa00c = A.FCompNo UPDATE A SET A.FStoreId = B.FId FROM TInventory AS A,gam04 AS B WHERE A.storeNo = B.gad01c AND gad00c = '001' --第三步,结束 -----------------------------------------------------------