SQL Server中的多级Marketing营销过程

时间:2016-09-11 18:09:08

标签: asp.net sql-server sql-server-2008 stored-procedures

Tree Image 我想为用户注册创建一个多级树结构,如果成员添加另一个超过应该添加为第一个子成员的限制的成员,则最多可以添加5个成员。例如,如图中的树Image,1可以直接添加成员2,3,4,5,6,如果1再添加一个成员,它应该被添加为2的第一个子节点,如果1再添加它应该添加为3的第一个孩子,依此类推,直到6的第一个孩子,然后如果一个又添加它应该是2的第二个孩子,再次是3的第二个孩子等等我怎样才能创建一个存储过程插入到我的用户表中这里自动执行此操作我也附加了我的表结构

这里我附上表格如何实现这个

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Users]
(
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Login_ID] [uniqueidentifier] NOT NULL,
    [Login_Name] [nvarchar](100) NOT NULL,
    [Login_Password] [nvarchar](100) NOT NULL,
    [CreatedBy] [uniqueidentifier] NULL,
    [ModifiedBy] [uniqueidentifier] NULL,
    [Referal_ID] [uniqueidentifier] NULL,
    [Name] [nvarchar](100) NULL,
    [User_Address] [nvarchar](max) NULL,
    [Phone] [nvarchar](14) NULL,
    [Email] [nvarchar](100) NULL,
    [BankName] [nvarchar](250) NULL,
    [AccountName] [nvarchar](100) NULL,
    [IFSC] [nvarchar](100) NULL,
    [AccountNo] [nvarchar](150) NULL,
    [DOB] [datetime] NULL,
    [Created_Date] [datetime] NULL,
    [Modified_Date] [datetime] NULL,
    [Last_Login_Date_Time] [datetime] NULL,
    [UnsuscribeEmail] [int] NULL,
    [UnsuscribeSms] [int] NULL,
    [IsBanned] [int] NULL,
    [ISDeleted] [int] NULL,

    PRIMARY KEY CLUSTERED ([Login_ID] ASC)
            WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
                  IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, 
                  ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

1 个答案:

答案 0 :(得分:0)

USE [Hyperbole]
GO

/****** Object:  Table [dbo].[Users]    Script Date: 9/26/2016 1:22:23 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Users](
    [Login_ID] [bigint] IDENTITY(1,1) NOT NULL,
    [Login_Name] [nvarchar](100) NOT NULL,
    [Login_Password] [nvarchar](100) NULL,
    [CreatedBy] [bigint] NULL,
    [ModifiedBy] [bigint] NULL,
    [Referal_ID] [bigint] NULL,
    [Name] [nvarchar](250) NULL,
    [User_Address] [nvarchar](max) NULL,
    [Phone] [nvarchar](14) NULL,
    [Email] [nvarchar](250) NULL,
    [BankName] [nvarchar](250) NULL,
    [AccountName] [nvarchar](250) NULL,
    [IFSC] [nvarchar](250) NULL,
    [AccountNo] [nvarchar](250) NULL,
    [DOB] [datetime] NULL,
    [Created_Date] [datetime] NULL,
    [Modified_Date] [datetime] NULL,
    [Last_Login_Date_Time] [datetime] NULL,
    [UnsuscribeEmail] [int] NULL,
    [UnsuscribeSms] [int] NULL,
    [IsBanned] [int] NULL,
    [ISDeleted] [int] NULL,
PRIMARY KEY CLUSTERED 
(
    [Login_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

存储过程

CREATE Proc [dbo].[User_SP]
(
           @Login_Name nvarchar(100)=null
           ,@Login_Password nvarchar(100)=null
           ,@CreatedBy BIGINT=null
           ,@ModifiedBy BIGINT=null
           ,@Referal_ID BIGINT=null
           ,@Name nvarchar(250)=null
           ,@User_Address nvarchar(max)=null
           ,@Phone nvarchar(14)=null
           ,@Email nvarchar(250)=null
           ,@BankName nvarchar(250)=null
           ,@AccountName nvarchar(250)=null
           ,@IFSC nvarchar(250)=null
           ,@AccountNo nvarchar(150)=null
           ,@DOB datetime=null
           ,@Created_Date datetime=null
           ,@Modified_Date datetime=null
           ,@Last_Login_Date_Time datetime=null
           ,@UnsuscribeEmail int=0
           ,@UnsuscribeSms int=0
           ,@IsBanned int=0
           ,@ISDeleted int=0
           )
--@UserName     NVARCHAR (255),
--@parentID BIGINT
as
begin

IF OBJECT_ID('tempdb..#ChildHierarchy') IS NOT NULL
DROP TABLE #ChildHierarchy

IF OBJECT_ID('tempdb..#ChildCount') IS NOT NULL
DROP TABLE #ChildCount

CREATE TABLE #ChildHierarchy ( ChildId BIGINT, ChildName NVARCHAR(255), LevelNo BIGINT, ParentID BIGINT )

CREATE TABLE #ChildCount ( ChildParentID BIGINT, ChildCount BIGINT, ChildParentLevelID BIGINT, MaxChildCount BIGINT, AvailableChildCount BIGINT )

DECLARE @MaxChildCount TABLE ( LevelID INT, MaxChildCount BIGINT )

DECLARE  @NextParentID BIGINT, @MaxLevelNo INT 

INSERT INTO @MaxChildCount (LevelID, MaxChildCount)
VALUES ( 1, 5 ), ( 2, 25 ), ( 3, 125 ), ( 4, 625 ), ( 5, 3125 ), ( 6, 15625 ), ( 7, 78125 ), ( 8, 390625 ), ( 9, 1953125 ), ( 10, 9765625 )

/*row generations*/
;WITH Hierarchy(ChildId, ChildName, LevelNo, ParentId)
AS
(
    SELECT Login_ID, Name, 0, Referal_ID
        FROM Users AS FirtGeneration
        WHERE Login_ID = @Referal_ID        
    UNION ALL
    SELECT NextGeneration.Login_ID, NextGeneration.Name, Parent.LevelNo + 1, Parent.ChildId
        FROM Users AS NextGeneration
        INNER JOIN Hierarchy AS Parent ON NextGeneration.Referal_ID = Parent.ChildId    
)

INSERT INTO #ChildHierarchy (ChildId,ChildName,LevelNo,ParentID)
SELECT ChildId, ChildName, LevelNo, ParentId
    FROM Hierarchy
    OPTION(MAXRECURSION 32767)

INSERT INTO #ChildCount (ChildParentID, ChildCount,ChildParentLevelID)
SELECT ParentID, COUNT(ChildId), LevelNo 
FROM #ChildHierarchy 
WHERE LevelNo > 0
GROUP BY ParentID, LevelNo

UPDATE CC SET MaxChildCount = MCC.MaxChildCount
FROM #ChildCount CC
INNER JOIN @MaxChildCount MCC ON CC.ChildParentLevelID = MCC.LevelID


SET @MaxLevelNo = ( SELECT MAX(LevelNo) FROM #ChildHierarchy )

UPDATE CC SET AvailableChildCount = CC1.AvailableChildCount 
FROM #ChildCount CC
INNER JOIN ( SELECT ChildParentLevelID,SUM(ChildCount) AS AvailableChildCount FROM #ChildCount GROUP BY ChildParentLevelID ) CC1 ON CC.ChildParentLevelID = CC1.ChildParentLevelID

IF @MaxLevelNo = 0 OR NOT EXISTS ( SELECT TOP 1 1 FROM #ChildCount )
    SET @NextParentID = @Referal_ID

IF @NextParentID IS NULL 
    SET @NextParentID = ( SELECT MIN(ChildParentID) FROM #ChildCount WHERE MaxChildCount <> AvailableChildCount AND ChildCount <> 5 )

IF @NextParentID IS NULL
    SET @NextParentID = ( SELECT MIN(CH.ChildId) FROM #ChildHierarchy CH WHERE NOT EXISTS ( SELECT TOP 1 1 FROM #ChildCount CC WHERE CH.ChildId = CC.ChildParentID ) )

INSERT INTO Users (Login_Name,Login_Password,CreatedBy,ModifiedBy,Referal_ID,Name,User_Address,Phone,Email,BankName,AccountName,IFSC,AccountNo,DOB,Created_Date,Modified_Date,UnsuscribeEmail,UnsuscribeSms,IsBanned,ISDeleted)
SELECT @Login_Name,@Login_Password,@NextParentID,@NextParentID,@NextParentID,@Name,@User_Address,@Phone,@Email,@BankName,@AccountName,@IFSC,@AccountNo,@DOB,@Created_Date,@Modified_Date,0,0,0,0

end
GO