You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
sql-tools/copyInitDatabase.sql

667 lines
16 KiB

IF OBJECT_ID('PCopyInitDatabase') IS NOT NULL
BEGIN
DROP PROC PCopyInitDatabase
END
GO
CREATE PROC PCopyInitDatabase
(
@srcServer VARCHAR(1000),
@srcDatabase VARCHAR(1000)
)
AS
BEGIN
DECLARE @src VARCHAR(1000)
SELECT @src = @srcDatabase + '.dbo.'
IF @srcServer != ''
BEGIN
SELECT @src = @srcServer + '.' + @src
END
DECLARE @srcTable VARCHAR(1000)
SELECT @srcTable = @src + 'TCompany'
EXEC(' UPDATE TCompany SET FId = FId WHERE FId = -1')
IF @@ERROR > 0
RETURN
-----------------------------------------------------
--基本类别
-----------------------------------------------------
DELETE TBasicTypeSub
DELETE TBasicType WHERE FId > 0
IF OBJECT_ID ('TBasicType#') IS NOT NULL
BEGIN
DROP TABLE TBasicType#
END
SELECT * INTO TBasicType# FROM TBasicType WHERE 1=0
SELECT @srcTable = @src + 'TBasicType'
EXEC PCopyTable 'TBasicType#',@srcTable,'AND FId > 9'
EXEC('ALTER TABLE TBasicType# ADD FOldId BIGINT')
EXEC('UPDATE TBasicType# SET FId = dbo.FNNewId(),FOldId = FId WHERE FOldId IS NULL AND FId > 900000')
EXEC PCopyTable 'TBasicType','TBasicType#','AND FId > 0'
INSERT TBasicTypeSub(FBasicTypeId,FCompId)
SELECT A.FId,1
FROM TBasicType AS A
-----------------------------------------------------
--项目
-----------------------------------------------------
DELETE TItemSub
DELETE TCourse WHERE FId > 0
DELETE TItem WHERE FId > 0
IF OBJECT_ID ('TItem#') IS NOT NULL
BEGIN
DROP TABLE TItem#
END
SELECT * INTO TItem# FROM TItem WHERE 1=0
SELECT @srcTable = @src + 'TItem'
EXEC PCopyTable 'TItem#',@srcTable,'AND FId > 0 AND FDeleted = 0'
EXEC('ALTER TABLE TItem# ADD FOldId BIGINT')
--生成新ID
EXEC('UPDATE TItem# SET FId = dbo.FNNewId(),FOldId = FId WHERE FOldId IS NULL')
--更新项目产品类别
EXEC('
UPDATE A
SET A.FTypeId = B.FId
FROM TItem# AS A,TBasicType# AS B
WHERE A.FTypeId = B.FOldId ')
--更新项目部门
EXEC('
UPDATE A
SET A.FDeptId = B.FId
FROM TItem# AS A,TBasicType# AS B
WHERE A.FDeptId = B.FOldId')
--更新项目折扣
EXEC('
UPDATE A
SET A.FDctId = B.FId
FROM TItem# AS A,TBasicType# AS B
WHERE A.FDctId = B.FOldId')
EXEC PCopyTable 'TItem','TItem#','AND FId > 0'
INSERT TItemSub(FId,FCompId,FItemId,FPrice,FVipPrice,FTime,FDctId,FPointMode,FPointValue,FDeptId,FAllowBooking,FEnabled)
SELECT dbo.FNNewId(),1,I.FId,I.FPrice,I.FVipPrice,I.FTime,I.FDctId,I.FPointMode,I.FPointValue,I.FDeptId,I.FAllowBooking,I.FEnabled
FROM TItem AS I
WHERE I.FId != 0
-----------------------------------------------------
--项目提成
-----------------------------------------------------
DELETE TItemBaseComm
IF OBJECT_ID('TItemBaseComm#') IS NOT NULL
BEGIN
DROP TABLE TItemBaseComm#
END
SELECT * INTO TItemBaseComm# FROM TItemBaseComm WHERE 1=0
SELECT @srcTable = @src + 'TItemBaseComm'
EXEC PCopyTable 'TItemBaseComm#',@srcTable,'AND FCompId = 1 AND FItemId IN(SELECT FOldId FROM TItem#)'
EXEC('
UPDATE A
SET A.FItemId = B.FId
FROM TItemBaseComm# AS A,TItem# AS B
WHERE A.FItemId = B.FOldId')
EXEC PCopyTable 'TItemBaseComm','TItemBaseComm#','AND FId > 0'
--项目提成方案
DELETE TItemCommSchemeDetail
DELETE TItemCommSchemeSection
DELETE TItemCommSchemeMaster
IF OBJECT_ID('TItemCommSchemeDetail#') IS NOT NULL
BEGIN
DROP TABLE TItemCommSchemeDetail#
END
IF OBJECT_ID('TItemCommSchemeMaster#') IS NOT NULL
BEGIN
DROP TABLE TItemCommSchemeMaster#
END
IF OBJECT_ID('TItemCommSchemeSection#') IS NOT NULL
BEGIN
DROP TABLE TItemCommSchemeSection#
END
SELECT * INTO TItemCommSchemeDetail# FROM TItemCommSchemeDetail WHERE 1=0
SELECT * INTO TItemCommSchemeSection# FROM TItemCommSchemeSection WHERE 1=0
SELECT * INTO TItemCommSchemeMaster# FROM TItemCommSchemeMaster WHERE 1=0
SELECT @srcTable = @src + 'TItemCommSchemeMaster'
EXEC PCopyTable 'TItemCommSchemeMaster#',@srcTable, ''
SELECT @srcTable = @src + 'TItemCommSchemeSection'
EXEC PCopyTable 'TItemCommSchemeSection#',@srcTable, ''
SELECT @srcTable = @src + 'TItemCommSchemeDetail'
EXEC PCopyTable 'TItemCommSchemeDetail#',@srcTable,'AND FItemId IN(SELECT FOldId FROM TItem#)'
EXEC('
UPDATE A
SET A.FItemId = B.FId
FROM TItemCommSchemeDetail# AS A,TItem# AS B
WHERE A.FItemId = B.FOldId')
EXEC PCopyTable 'TItemCommSchemeMaster','TItemCommSchemeMaster#','AND FId >= 0'
EXEC PCopyTable 'TItemCommSchemeSection','TItemCommSchemeSection#','AND FId >= 0'
EXEC PCopyTable 'TItemCommSchemeDetail','TItemCommSchemeDetail#','AND FId > 0'
------------------------------------------------------
--项目折扣
------------------------------------------------------
DELETE TItemDiscountDetail
DELETE TItemDiscountMid
DELETE TItemDiscountMaster
IF OBJECT_ID('TItemDiscountDetail#') IS NOT NULL
BEGIN
DROP TABLE TItemDiscountDetail#
END
IF OBJECT_ID('TItemDiscountMaster#') IS NOT NULL
BEGIN
DROP TABLE TItemDiscountMaster#
END
IF OBJECT_ID('TItemDiscountMid#') IS NOT NULL
BEGIN
DROP TABLE TItemDiscountMid#
END
SELECT * INTO TItemDiscountDetail# FROM TItemDiscountDetail WHERE 1=0
SELECT * INTO TItemDiscountMid# FROM TItemDiscountMid WHERE 1=0
SELECT * INTO TItemDiscountMaster# FROM TItemDiscountMaster WHERE 1=0
SELECT @srcTable = @src + 'TItemDiscountdETAIL'
EXEC PCopyTable 'TItemDiscountDetail#',@srcTable, ''
SELECT @srcTable = @src + 'TItemDiscountMid'
EXEC PCopyTable 'TItemDiscountMid#',@srcTable, ''
SELECT @srcTable = @src + 'TItemDiscountMaster'
EXEC PCopyTable 'TItemDiscountMaster#',@srcTable,'AND FItemId IN(SELECT FOldId FROM TBasicType#)'
EXEC('
UPDATE A
SET A.FItemId = B.FId
FROM TItemDiscountMaster# AS A,TBasicType# AS B
WHERE A.FItemId = B.FOldId')
EXEC PCopyTable 'TItemDiscountMaster','TItemDiscountMaster#','AND FId >= 0'
EXEC PCopyTable 'TItemDiscountMid','TItemDiscountMid#','AND FId >= 0'
EXEC PCopyTable 'TItemDiscountDetail','TItemDiscountDetail#','AND FId > 0'
-----------------------------------------------------
--疗程
-----------------------------------------------------
DELETE TCourse WHERE FId > 0
IF OBJECT_ID ('TCourse#') IS NOT NULL
BEGIN
DROP TABLE TCourse#
END
SELECT * INTO TCourse# FROM TCourse WHERE 1=0
SELECT @srcTable = @src + 'TCourse'
EXEC PCopyTable 'TCourse#',@srcTable,'AND FId > 0 AND FPackId = 0 AND FDeleted = 0'
EXEC('ALTER TABLE TCourse# ADD FOldId BIGINT')
--生成新ID
EXEC('UPDATE TCourse# SET FId = dbo.FNNewId(),FOldId = FId WHERE FOldId IS NULL')
--更新项目id
EXEC('
UPDATE A
SET A.FItemId = B.FId
FROM TCourse# AS A,TItem# AS B
WHERE A.FItemId = B.FOldId ')
--更新类别
EXEC('
UPDATE A
SET A.FTypeId = B.FId
FROM TCourse# AS A,TBasicType# AS B
WHERE A.FTypeId = B.FOldId ')
EXEC PCopyTable 'TCourse','TCourse#','AND FId > 0'
-----------------------------------------------------
--产品
-----------------------------------------------------
DELETE TGoodsSub
DELETE TGoods WHERE FId > 0
IF OBJECT_ID ('TGoods#') IS NOT NULL
BEGIN
DROP TABLE TGoods#
END
SELECT * INTO TGoods# FROM TGoods WHERE 1=0
SELECT @srcTable = @src + 'TGoods'
EXEC PCopyTable 'TGoods#',@srcTable,'AND FId >= 0'
EXEC('ALTER TABLE TGoods# ADD FOldId BIGINT ')
EXEC('UPDATE TGoods# SET FId = dbo.FNNewId(),FOldId = FId WHERE FOldid IS NULL')
--更新项目产品类别
EXEC('
UPDATE A
SET A.FTypeId = B.FId
FROM TGoods# AS A,TBasicType# AS B
WHERE A.FTypeId = B.FOldId')
--更新项目部门
EXEC('
UPDATE A
SET A.FDeptId = B.FId
FROM TGoods# AS A,TBasicType# AS B
WHERE A.FDeptId = B.FOldId')
--更新项目折扣
EXEC('
UPDATE A
SET A.FDctId = B.FId
FROM TGoods# AS A,TBasicType# AS B
WHERE A.FDctId = B.FOldId')
--更新产品单位
EXEC('
UPDATE A
SET A.FUnitId = B.FId
FROM TGoods# AS A,TBasicType# AS B
WHERE A.FUnitId = B.FOldId')
EXEC PCopyTable 'TGoods','TGoods#','AND FId >= 0'
--复制产品标准单位
DELETE TGoodsMultiUnit WHERE FUnitType = 1
INSERT INTO TGoodsMultiUnit(FId,FGoodsId,FUnitType,FUnitId,FSequence,FConvDir,FConvRate,FSalePrice,FPurCostPrice,FSaleCostPrice,FConsCostPrice,FBarCode,FUnitLock)
SELECT dbo.FNNewId(),A.FId AS FGoodsId,1,FUnitId,0,1,1,FSalePrice,FPurCostPrice,FSaleCostPrice,FConsCostPrice,FBarCode,FUnitLock
FROM TGoods AS A
INSERT TGoodsSub(FId,FCompId,FGoodsId)
SELECT dbo.FNNewId(),1,I.FId
FROM TGoods AS I
WHERE I.FId != 0
-----------------------------------------------------
--产品提成
-----------------------------------------------------
DELETE TGoodsBaseComm
IF OBJECT_ID('TGoodsBaseComm#') IS NOT NULL
BEGIN
DROP TABLE TGoodsBaseComm#
END
SELECT * INTO TGoodsBaseComm# FROM TGoodsBaseComm WHERE 1=0
SELECT @srcTable = @src + 'TGoodsBaseComm'
EXEC PCopyTable 'TGoodsBaseComm#',@srcTable,'AND FCompId = 1 AND FGoodsId IN(SELECT FOldId FId FROM TGoods#)'
EXEC('
UPDATE A
SET A.FGoodsId = B.FId
FROM TGoodsBaseComm# AS A,TGoods# AS B
WHERE A.FGoodsId = B.FOldId')
EXEC PCopyTable 'TGoodsBaseComm','TGoodsBaseComm#','AND FId > 0'
-----------------------------------------------------
--员工
-----------------------------------------------------
DELETE TEmployeeSub
DELETE TEmployee WHERE FId > 0
IF OBJECT_ID ('TEmployee#') IS NOT NULL
BEGIN
DROP TABLE TEmployee#
END
SELECT * INTO TEmployee# FROM TEmployee WHERE 1=0
SELECT @srcTable = @src + 'TEmployee'
EXEC PCopyTable 'TEmployee#',@srcTable,'AND FId > 0 AND FDeleted = 0'
EXEC('ALTER TABLE TEmployee# ADD FOldId BIGINT')
--生成新ID
EXEC('UPDATE TEmployee# SET FId = dbo.FNNewId(),FOldId = FId WHERE FOldId IS NULL')
--更新部门
EXEC('
UPDATE A
SET A.FDeptId = B.FId
FROM TEmployee# AS A,TBasicType# AS B
WHERE A.FDeptId = B.FOldId ')
--更新岗位
EXEC('
UPDATE A
SET A.FPosId = B.FId
FROM TEmployee# AS A,TBasicType# AS B
WHERE A.FPosId = B.FOldId')
EXEC PCopyTable 'TEmployee','TEmployee#','AND FId > 0'
INSERT TEmployeeSub(FCompId,FEmpId,FSerialNo)
SELECT 1,I.FId,I.FNo
FROM TEmployee AS I
WHERE I.FId != 0
-----------------------------------------------------
--储值卡
-----------------------------------------------------
DELETE TMemberCardTypeSub
DELETE TMemberCardType WHERE FId > 9
IF OBJECT_ID ('TMemberCardType#') IS NOT NULL
BEGIN
DROP TABLE TMemberCardType#
END
SELECT * INTO TMemberCardType# FROM TMemberCardType WHERE 1=0
SELECT @srcTable = @src + 'TMemberCardType'
EXEC PCopyTable 'TMemberCardType#',@srcTable,'AND FId > 9'
ALTER TABLE TMemberCardType# ADD FOldId BIGINT
EXEC('UPDATE TMemberCardType# SET FId = dbo.FNNewId(),FOldId = FId WHERE FOldId IS NULL')
EXEC PCopyTable 'TMemberCardType','TMemberCardType#','AND FId > 0'
INSERT INTO TMemberCardTypeSub(FId,FCompId,FCardTypeId,FAllowSale,FAllowRchg,FEnabled)
SELECT dbo.FNNewId(),1,I.FId,FAllowSale,FAllowRchg,FEnabled
FROM TMemberCardType AS I
WHERE I.FId > 1
-----------------------------------------------------
--储值卡提成
-----------------------------------------------------
DELETE TMemberCardTypeBaseComm
IF OBJECT_ID('TMemberCardTypeBaseComm#') IS NOT NULL
BEGIN
DROP TABLE TMemberCardTypeBaseComm#
END
SELECT * INTO TMemberCardTypeBaseComm# FROM TMemberCardTypeBaseComm WHERE FCompId = 1
SELECT @srcTable = @src + 'TMemberCardTypeBaseComm'
EXEC PCopyTable 'TMemberCardTypeBaseComm#',@srcTable,'AND FId > 0'
EXEC('
UPDATE A
SET A.FCardTypeId = B.FId
FROM TMemberCardTypeBaseComm# AS A,TMemberCardType# AS B
WHERE A.FCardTypeId = B.FOldId')
EXEC PCopyTable 'TMemberCardTypeBaseComm','TMemberCardTypeBaseComm#','AND FId > 0'
--卡提成方案
DELETE TCardTypeCommSchemeDetail
DELETE TCardTypeCommSchemeSection
DELETE TCardTypeCommSchemeMaster
IF OBJECT_ID('TCardTypeCommSchemeDetail#') IS NOT NULL
BEGIN
DROP TABLE TCardTypeCommSchemeDetail#
END
IF OBJECT_ID('TCardTypeCommSchemeMaster#') IS NOT NULL
BEGIN
DROP TABLE TCardTypeCommSchemeMaster#
END
IF OBJECT_ID('TCardTypeCommSchemeSection#') IS NOT NULL
BEGIN
DROP TABLE TCardTypeCommSchemeSection#
END
SELECT * INTO TCardTypeCommSchemeDetail# FROM TCardTypeCommSchemeDetail WHERE 1=0
SELECT * INTO TCardTypeCommSchemeSection# FROM TCardTypeCommSchemeSection WHERE 1=0
SELECT * INTO TCardTypeCommSchemeMaster# FROM TCardTypeCommSchemeMaster WHERE 1=0
SELECT @srcTable = @src + 'TCardTypeCommSchemeMaster'
EXEC PCopyTable 'TCardTypeCommSchemeMaster#',@srcTable, ''
SELECT @srcTable = @src + 'TCardTypeCommSchemeSection'
EXEC PCopyTable 'TCardTypeCommSchemeSection#',@srcTable, ''
SELECT @srcTable = @src + 'TCardTypeCommSchemeDetail'
EXEC PCopyTable 'TCardTypeCommSchemeDetail#',@srcTable,'AND FCardTypeId IN(SELECT FOldId FROM TMemberCardType#)'
EXEC('
UPDATE A
SET A.FCardTypeId = B.FId
FROM TCardTypeCommSchemeDetail# AS A,TMemberCardType# AS B
WHERE A.FCardTypeId = B.FOldId')
EXEC PCopyTable 'TCardTypeCommSchemeMaster','TCardTypeCommSchemeMaster#','AND FId >= 0'
EXEC PCopyTable 'TCardTypeCommSchemeSection','TCardTypeCommSchemeSection#','AND FId >= 0'
EXEC PCopyTable 'TCardTypeCommSchemeDetail','TCardTypeCommSchemeDetail#','AND FId > 0'
-----------------------------------------------------
--系统参数
-----------------------------------------------------
DELETE TSystemParamSub
DELETE TSystemParam
IF OBJECT_ID ('TSystemParam#') IS NOT NULL
BEGIN
DROP TABLE TSystemParam#
END
SELECT * INTO TSystemParam# FROM TSystemParam WHERE 1=0
SELECT @srcTable = @src + 'TSystemParam'
EXEC PCopyTable 'TSystemParam#',@srcTable,' '
EXEC PCopyTable 'TSystemParam','TSystemParam#',' '
-----------------------------------------------------
--角色+用户
-----------------------------------------------------
DELETE TUserAllowCompany
DELETE TUserRole
DELETE TUser WHERE FId > 1
DELETE TRoleAccess
DELETE TRole WHERE FId > 0
IF OBJECT_ID ('TRole#') IS NOT NULL
BEGIN
DROP TABLE TRole#
END
IF OBJECT_ID ('TRoleAccess#') IS NOT NULL
BEGIN
DROP TABLE TRoleAccess#
END
SELECT * INTO TRole# FROM TRole WHERE 1=0
SELECT * INTO TRoleAccess# FROM TRoleAccess WHERE 1=0
SELECT @srcTable = @src + 'TRole'
EXEC PCopyTable 'TRole#',@srcTable,' '
SELECT @srcTable = @src + 'TRoleAccess'
EXEC PCopyTable 'TRoleAccess#',@srcTable,' AND FAccessKey IN(SELECT FKey FROM TAccessDict)'
ALTER TABLE TRole# ADD FOldId BIGINT
EXEC('UPDATE TRole# SET FId = dbo.FNNewId(),FOldId = FId WHERE FOldId IS NULL')
EXEC('UPDATE TRoleAccess# SET FId = dbo.FNNewId()')
EXEC('
UPDATE A
SET A.FRoleId = B.FId
FROM TRoleAccess# AS A,TRole# AS B
WHERE A.FRoleId = B.FOldId')
IF OBJECT_ID ('TUser#') IS NOT NULL
BEGIN
DROP TABLE TUser#
END
IF OBJECT_ID ('TUserRole#') IS NOT NULL
BEGIN
DROP TABLE TUserRole#
END
IF OBJECT_ID ('TUserAllowCompany#') IS NOT NULL
BEGIN
DROP TABLE TUserAllowCompany#
END
SELECT * INTO TUser# FROM TUser WHERE 1=0
SELECT * INTO TUserRole# FROM TUserRole WHERE 1=0
SELECT * INTO TUserAllowCompany# FROM TUserAllowCompany WHERE 1=0
SELECT @srcTable = @src + 'TUser'
EXEC PCopyTable 'TUser#',@srcTable,' AND FId > 9 '
ALTER TABLE TUser# ADD FOldId BIGINT
EXEC('UPDATE TUser# SET FId = dbo.FNNewId(),FOldId = FId WHERE FOldId IS NULL')
SELECT @srcTable = @src + 'TUserRole'
EXEC PCopyTable 'TUserRole#',@srcTable,' '
EXEC('
UPDATE A
SET A.FUserId = B.FId
FROM TUserRole# AS A,TUser# AS B
WHERE A.FUserId = B.FOldId')
EXEC('
UPDATE A
SET A.FRoleId = B.FId
FROM TUserRole# AS A,TRole# AS B
WHERE A.FRoleId = B.FOldId')
EXEC PCopyTable 'TRole','TRole#',' '
EXEC PCopyTable 'TRoleAccess','TRoleAccess#',' '
EXEC PCopyTable 'TUser','TUser#',' '
EXEC PCopyTable 'TUserRole','TUserRole#',' '
EXEC PCopyTable 'TUserAllowCompany','TUserAllowCompany#',' '
-----------------------------------------------------
--短信模板
-----------------------------------------------------
DELETE TSMSTemplate
IF OBJECT_ID ('TSMSTemplate#') IS NOT NULL
BEGIN
DROP TABLE TSMSTemplate#
END
SELECT * INTO TSMSTemplate# FROM TSMSTemplate WHERE 1=0
SELECT @srcTable = @src + 'TSMSTemplate'
EXEC PCopyTable 'TSMSTemplate#',@srcTable,' '
EXEC PCopyTable 'TSMSTemplate','TSMSTemplate#',' '
-----------------------------------------------------
--微信配置
-----------------------------------------------------
DELETE TWeChatAppConfig
IF OBJECT_ID ('TWeChatAppConfig#') IS NOT NULL
BEGIN
DROP TABLE TWeChatAppConfig#
END
SELECT * INTO TWeChatAppConfig# FROM TWeChatAppConfig WHERE 1=0
SELECT @srcTable = @src + 'TWeChatAppConfig'
EXEC PCopyTable 'TWeChatAppConfig#',@srcTable,' '
EXEC PCopyTable 'TWeChatAppConfig','TWeChatAppConfig#',' '
END
GO
EXEC PCopyInitDatabase '','JiNan_SanHao'
--drop database test
--CREATE DATABASE TEST