Tree Image
I want to create a multilevel tree structure for user Registration , a members can add up to 5 members if he adds another member which greater than the limit it should be added as the first child member. for example as in the figure tree Image, 1 can directly add members 2,3,4,5,6 if 1 add a member again it should be added as the first child of 2,and if 1 add again it should be added as the first child of 3 and so on up to first child of 6, then if one again adds it should be the second child of 2, again second child of 3 and so on how can i create a stored procedure to insert into my user table which automatically perform this here i am attaching my table structure also
Here I am attaching the table how can i achieve this
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
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
Stored Procedure
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
CREATE TABLE Phone_Customer(
Customer_ID Int NOT NULL,
First_Name VARCHAR2(20) NOT NULL,
Last_Name VARCHAR2(20) NOT NULL,
E_mail VARCHAR2(20) NOT NULL,
Address VARCHAR2(20) NOT NULL,
First_Name VARCHAR (20) NOT NULL,
City VARCHAR (20) NOT NULL,
State CHAR (2) NOT NULL,
Age INT NOT NULL,
Zip INT NOT NULL,
Phone VarChar(30)
CONSTRAINT Phone_Customer_pk PRIMARY KEY (Customer_ID)
);
CREATE TABLE Phone_Order(
Order_ID INT NOT NULL,
Customer_ID INT NOT NULL,
Item_ID INT NOT NULL,
Shipping_Method_ID INT NOT NULL,
Shipping_Status_ID INT NOT NULL,
Quantity INT NOT NULL,
Shipping_Date DATE,
Total_Due DECIMAL(18,2)
Credit_Card_Number Char(16)
Credit_Card_Expiration Varchar2(5)
CONSTRAINT Phone_Order_pk PRIMARY KEY (Order_ID),
CONSTRAINT fk_Customer_Id FOREIGN KEY(Customer_Id)
References Customer(Customer_ID),
CONSTRAINT fk_Item_ID FOREIGN KEY(Item_ID)
References Item(Item_ID),
CONSTRAINT fk_Shipping_Method_ID FOREIGN KEY(Shipping_Method_Id) References Shipping Method(Shipping_Method_ID),
CONSTRAINT fk_Shipping_Status_ID FOREIGN KEY(Shiping_Status_ID)
References Shipping Status(Shipping_Status_ID)
);
CREATE TABLE Phone_Item(
Item_ID INT NOT NULL,
Brand VARCHAR2 (20) NOT NULL,
Model VARCHAR2 (20) NOT NULL,
E_mail VARCHAR2 (20) NOT NULL,
Operating_System VARCHAR2 (20) NOT NULL,
Price DECIMAL(18,2),
Screen_Size DECIMAL(4,2),
Camera_Resolution VARCHAR2(10),
Network VARCHAR2(4),
RAM INT NOT NULL,
storage INT NOT NULL,
Talk_Time INT NOT NULL,
COLOR VARCHAR2(16),
CONSTRAINT Phone_Item_pk PRIMARY_KEY(Item_ID)
);
CREATE TABLE Phone_Shipping_Method(
Shipping_Method_ID INT NOT NULL,
Name VARCHAR2(20) NOT NULL,
Price DECIMAL(18,2) NOT NULL,
CONSTRAINT Phone_Shipping_Method_pk PRIMARY_KEY(Shipping_Method_ID)
);
CREATE TABLE Phone_Shipping_Status(
Shipping_Status_ID INT NOT NULL,
Name VARCHAR2(20) NOT NULL,
Reset_Shipping_Date DATE NOT NULL,
CONSTRAINT Phone_Shipping_Status_pk PRIMARY_KEY(Shipping_Method_ID)
);
INSERT ALL
INTO Phone_Shipping_Status(Shipping_Status_Id,Name, Reset_Shipping_Date)VALUES(5029508, 'James',To_Date( '010988', 'MMDDYY'))
INTO Phone_Shipping_Status(Shipping_Status_Id,Name, Reset_Shipping_Date)
VALUES(450767, 'James Walker',TO_DATE( '050978', 'MMDDYY'))
INTO Phone_Shipping_Status(Shipping_Status_Id,Name, Reset_Shipping_Date)
VALUES(3, 'Jim Curack',TO_DATE( '070989', 'MMDDYY'))
INTO Phone_Shipping_Status(Shipping_Status_Id,Name, Reset_Shipping_Date)
VALUES(8, 'Jaime Heimberger',TO_DATE( '070989', 'MMDDYY'))
INTO Phone_Shipping_Status(Shipping_Status_Id,Name, Reset_Shipping_Date)
VALUES(9, 'Dwayne Johnson',TO_DATE( '100999', 'MMDDYY'))
SELECT * FROM dual;
INSERT ALL
INTO Phone_Shipping_Method(Shipping_Method_Id,Name, Reset_Shipping_Date)
VALUES(50295656, 'Ricky Bobby',TO_DATE( '010308', 'MMDDYY'))
INTO Phone_Shipping_Method(Shipping_Method_Id,Name, Reset_Shipping_Date)
VALUES(4507655, 'Bobby Heenan',TO_DATE( '050998', 'MMDDYY'))
INTO Phone_Shipping_Method(Shipping_Method_Id,Name, Reset_Shipping_Date)
VALUES(3747547, 'Jim-long Johnson',TO_DATE( '070969', 'MMDDYY'))
INTO Phone_Shipping_Method(Shipping_Method_Id,Name, Reset_Shipping_Date)
VALUES(8432525, 'J Joshua johnson',TO_DATE( '070909', 'MMDDYY'))
INTO Phone_Shipping_Method(Shipping_Method_Id,Name, Reset_Shipping_Date)
VALUES(943252, 'Dusty Rhodes',TO_DATE( '100999', 'MMDDYY'))
SELECT * FROM dual;
INSERT ALL
INTO Phone_Customer(Customer_Id,First_Name, Last_Name, E_mail, Address, City, State , Age , Zip , Phone )
VALUES(508, 'James','Buchannen','j#gmail.com','999 james road','Los Angeles','CA',67,54666,'123456789')
INTO Phone_Customer(Customer_Id,First_Name, Last_Name, E_mail, Address, City, State , Age , Zip , Phone )
VALUES(508534, 'Jasmine','Buchannen','jasmine21#gmail.com','99056 jumanji road','RainForest,'CA',67,54666,'126956789')
INTO Phone_Customer(Customer_Id,First_Name, Last_Name, E_mail, Address, City, State , Age , Zip , Phone )
VALUES(5432508, 'Clyde','Buchannen','ClydeB57#yahoo.com','65465 james ave','Los Angeles','CA',67,54667,'125436789')
INTO Phone_Customer(Customer_Id,First_Name, Last_Name, E_mail, Address, City, State , Age , Zip , Phone )
VALUES(7657508, 'Jimmy','Buchannen','jimmyB#gmail.com','4569 roady road','Los Angeles','CA',67,54669,'165432789')
INTO Phone_Customer(Customer_Id,First_Name, Last_Name, E_mail, Address, City, State , Age , Zip , Phone )
VALUES(576708, 'Bonnie','Buchannen','Bonniej_buch#aol.com','11999 teeal road','Los Angeles','CA',67,54676,'176543289')
SELECT * FROM dual;
INSERT ALL
INTO Phone_Order(Order_Id,Customer_Id, Item_Id, Shipping_Method_Id, Shipping_Status_Id, Quantity, Shipping_date, Total_Due, Credit_Card_Number. Credit_Card_Expiration)
Values( 432532,5325234,4565,34553,675675,5, To_Date( '100999', 'MMDDYY'), 56.98,'3456456453334444,'05/17')
INTO Phone_Order(Order_Id,Customer_Id, Item_Id, Shipping_Method_Id, Shipping_Status_Id, Quantity, Shipping_date, Total_Due, Credit_Card_Number. Credit_Card_Expiration)
Values( 45435432,5254354354,47675,3765766553,675757555,5, To_Date( '100100', 'MMDDYY'), 96.99,'3456456111134444,'05/18')
INTO Phone_Order(Order_Id,Customer_Id, Item_Id, Shipping_Method_Id, Shipping_Status_Id, Quantity, Shipping_date, Total_Due, Credit_Card_Number, Credit_Card_Expiration)
Values( 4542,52554,475,766553,7555,1, To_Date( '111111', 'MMDDYY'), 96.99,'345645611342133333,'05/11')
INTO Phone_Order(Order_Id,Customer_Id, Item_Id, Shipping_Method_Id, Shipping_Status_Id, Quantity, Shipping_date, Total_Due, Credit_Card_Number, Credit_Card_Expiration)
Values( 42,554,45,765,75,100, To_Date( '121212', 'MMDDYY'), 76.99,'3456451234566786678,'05/16')
INTO Phone_Order(Order_Id,Customer_Id, Item_Id, Shipping_Method_Id, Shipping_Status_Id, Quantity, Shipping_date, Total_Due, Credit_Card_Number, Credit_Card_Expiration)
Values( 8778872,55878784,4598898,769895,765665,1, To_Date( '122317', 'MMDDYY'), 50.99,'34564512345555578,'05/115')
Select * From dual;
INSERT ALL
INTO Phone_Item(Item_Id, Brand, Model, Price, Operating_System, Screen_Size, Camera_Resolution,Network,RAM,Storage,Talk_Time,Color)
Values(123344, 'samsung','galaxy',56.78,'Android',13.7,'1200x1200','4g'56,120,178,'black')
INTO Phone_Item(Item_Id, Brand, Model, Price, Operating_System, Screen_Size, Camera_Resolution,Network,RAM,Storage,Talk_Time,Color)
Values(123134, 'samsung','galaxy',567.78,'Android',13,'120x700','4g',100,100,5178,'green')
INTO Phone_Item(Item_Id, Brand, Model, Price, Operating_System, Screen_Size, Camera_Resolution,Network,RAM,Storage,Talk_Time,Color)
Values(123321, 'samsung','galaxy',656.78,'Android',17,'100x100','4g',90,10,1678,'purple')
INTO Phone_Item(Item_Id, Brand, Model, Price, Operating_System, Screen_Size, Camera_Resolution,Network,RAM,Storage,Talk_Time,Color)
Values(12333, 'samsung','galaxy',156.78,'Android',3,'200x200','4g',90,20,8178,'brown')
INTO Phone_Item(Item_Id, Brand, Model, Price, Operating_System, Screen_Size, Camera_Resolution,Network,RAM,Storage,Talk_Time,Color)
Values(1233, 'samsung','galaxy',356.78,'Android',7,'1200x1200','4g',56,1200,1788,'blue')
Select * from dual;
duplicate column name, missing commas (every column and constraint definition should be separated by a comma), PRIMARY KEY should not have underline: "PRIMARY_KEY" is incorrect. Phone_Shipping_Status table has invalid column name for it's Primary Key and I think it should be Shipping_Status_ID. a typo in Shiping_Status_ID and it should be Shipping_Status_ID, Foreign Keys for Phone_customer and Phone_item table is typed Customer and Item...
And the most important thing is the order of Tables definition according to relations between them that I re-arranged the order.
I don't think the inserts have important issue, really I didn't check them.
It's your correct DDL code:
CREATE TABLE Phone_Customer(
Customer_ID Int NOT NULL,
First_Name VARCHAR2(20) NOT NULL,
Last_Name VARCHAR2(20) NOT NULL,
E_mail VARCHAR2(20) NOT NULL,
Address VARCHAR2(20) NOT NULL,
City VARCHAR (20) NOT NULL,
State CHAR (2) NOT NULL,
Age INT NOT NULL,
Zip INT NOT NULL,
Phone VarChar(30),
CONSTRAINT Phone_Customer_pk PRIMARY KEY (Customer_ID)
);
CREATE TABLE Phone_Item(
Item_ID INTEGER NOT NULL,
Brand VARCHAR2 (20) NOT NULL,
Model VARCHAR2 (20) NOT NULL,
E_mail VARCHAR2 (20) NOT NULL,
Operating_System VARCHAR2 (20) NOT NULL,
Price DECIMAL(18,2),
Screen_Size DECIMAL(4,2),
Camera_Resolution VARCHAR2(10),
Network VARCHAR2(4),
RAM INTEGER NOT NULL,
Storage INTEGER NOT NULL,
Talk_Time INTEGER NOT NULL,
COLOR VARCHAR2(16),
CONSTRAINT Phone_Item_pk PRIMARY KEY (Item_ID)
);
CREATE TABLE Phone_Shipping_Method(
Shipping_Method_ID INT NOT NULL,
Name VARCHAR2(20) NOT NULL,
Price DECIMAL(18,2) NOT NULL,
CONSTRAINT Phone_Shipping_Method_pk PRIMARY KEY (Shipping_Method_ID)
);
CREATE TABLE Phone_Shipping_Status(
Shipping_Status_ID INT NOT NULL,
Name VARCHAR2(20) NOT NULL,
Reset_Shipping_Date DATE NOT NULL,
CONSTRAINT Phone_Shipping_Status_pk PRIMARY KEY(Shipping_Status_ID)
);
CREATE TABLE Phone_Order(
Order_ID INT NOT NULL,
Customer_ID INT NOT NULL,
Item_ID INT NOT NULL,
Shipping_Method_ID INT NOT NULL,
Shipping_Status_ID INT NOT NULL,
Quantity INT NOT NULL,
Shipping_Date DATE,
Total_Due DECIMAL(18,2),
Credit_Card_Number Char(16),
Credit_Card_Expiration Varchar2(5),
CONSTRAINT Phone_Order_pk PRIMARY KEY (Order_ID),
CONSTRAINT fk_Customer_Id FOREIGN KEY(Customer_Id)
References Phone_Customer(Customer_ID),
CONSTRAINT fk_Item_ID FOREIGN KEY(Item_ID)
References Phone_Item(Item_ID),
CONSTRAINT fk_Shipping_Method_ID FOREIGN KEY(Shipping_Method_Id)
References Phone_Shipping_Method(Shipping_Method_ID),
CONSTRAINT fk_Shipping_Status_ID FOREIGN KEY(Shipping_Status_ID)
References Phone_Shipping_Status(Shipping_Status_ID)
);
my stored procedure is-
CREATE PROCEDURE [dbo].[usp_SetMenu](
#locationId BIGINT,
#menuId BIGINT = NULL,
#name VARCHAR(100) = NULL,
#taxable BIT = NULL,
#type VARCHAR(100) = NULL,
#dateFrom DATETIME = NULL,
#dateTo DATETIME = NULL,
#timeFrom VARCHAR(10) = NULL,
#timeTo VARCHAR(10) = NULL,
#price MONEY = NULL,
#discountPerc FLOAT = NULL,
#discTimeFrom VARCHAR(10) = NULL,
#discTimeTo VARCHAR(10) = NULL,
#textcolor varchar(10) = null,
#bodycolor varchar(10) = null,
#createdBy BIGINT = NULL,
#createdOn DATETIME = NULL,
#modifiedBy BIGINT = NULL,
#modifiedOn DATETIME = NULL,
#menuProductsXML NTEXT = NULL ,
#IsCopy VARCHAR (10) = NULL,
#CopyMenuId BIGINT = NULL,
#menuTaxXML NTEXT = NULL ,
#menuExists INT = NULL OUTPUT,
#newMenuId INT = NULL OUTPUT
)
AS
SET NOCOUNT ON
---------------------------------------------------------------------
-- Declarations of variables
---------------------------------------------------------------------
DECLARE #ptrHandle INT
---------------------------------------------------------------------
-- initialize variables
---------------------------------------------------------------------
---------------------------------------------------------------------
-- get the data
---------------------------------------------------------------------
IF(#menuId IS NULL) -- If menuid is null then create a new record
BEGIN
select #menuExists = count('x') from tblMenu
where [name] = #name and isDeleted = 0 and locationid=#locationId
if #menuExists > 0
Return
INSERT INTO tblMenu
(locationid
,[name]
,[type]
,taxable
,datefrom
,dateto
,timefrom
,timeto
,price
,discountperc
,disctimefrom
,disctimeto
,bodycolor
,textcolor
,createdby
,createdon)
VALUES
(#locationId
,#name
,#type
,#taxable
,#dateFrom
,#dateTo
,#timeFrom
,#timeTo
,#price
,#discountPerc
,#discTimeFrom
,#discTimeTo
,#bodycolor
,#textcolor
,#createdBy
,#createdOn)
SET #menuId = ##IDENTITY
END
ELSE -- If menuid is not null then update that record
select #menuExists = count('x') from tblMenu
where [name] = #name and MenuId <> #menuId and isDeleted = 0 and locationid=#locationId
if #menuExists > 0
Return
UPDATE tblMenu
SET locationid = #locationId
,[name] = #name
,[type] = #type
,taxable = #taxable
,datefrom = #dateFrom
,dateto = #dateTo
,timefrom = #timeFrom
,timeto = #timeTo
,price = #price
,discountperc = #discountPerc
,disctimefrom = #discTimeFrom
,disctimeto = #discTimeTo
,bodycolor = #bodycolor
,textcolor = #textcolor
,modifiedby = #modifiedBy
,modifiedon = #modifiedOn
WHERE menuid = #menuId
-- if menu product collection is passed then insert new records
IF(#menuProductsXML IS NOT NULL)
BEGIN
-- Clearing the old menu products and inserting new ones
DELETE tblMenuProduct WHERE menuid = #menuId
EXEC sp_xml_preparedocument #ptrHandle OUTPUT, #menuProductsXML
INSERT INTO tblMenuProduct
(menuid
,productid
,categoryid
,productprice
,createdby
,createdon)
SELECT #menuId,
ProductId,
CategoryId,
ProductPrice,
#createdBy,
#createdOn
FROM OPENXML (#ptrHandle, '/ArrayOfMenuProductEntity/MenuProductEntity', 2)
WITH(ProductId BIGINT,CategoryId BIGINT, ProductPrice MONEY)
END
if(#IsCopy = 'True')
Begin
INSERT INTO tblMenuProduct
(menuid
,productid
,categoryid
,productprice
,createdby
,createdon)
Select #menuId,productid,categoryid,productprice,#createdBy,#createdOn
From tblMenuProduct where menuid = #CopyMenuId
SET #newMenuId = #menuId
End
IF(#menuTaxXML IS NOT NULL)
BEGIN
DELETE tblMenuTaxClass WHERE menuid = #menuId
EXEC sp_xml_preparedocument #ptrHandle OUTPUT, #menuTaxXML
INSERT INTO tblMenuTaxClass
(menuid
,taxclassid
)
SELECT #menuId,
TaxClassId
FROM OPENXML (#ptrHandle, '/ArrayOfTaxClassEntity/TaxClassEntity', 2)
WITH(TaxClassId BIGINT)
END
---------------------------------------------------------------------
-- exit the sproc
---------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------
SET NOCOUNT OFF
END
Exception:insert statement conflicted with the foreign key constraint
Why I am getting this exception and how can I fix this?
The primary key value wont be there.you are trying to insert a Foreign key value to the table where corresponding PK wont be there.
table1
ID(PK)
1
2
3
table2
ID1(PK) ID(FK)
1 1
2 1
3 4// Error not there in PK table