I'm working with a Customer Relationship Management application. I have two tables: tbl_Inquiry_master
for storing inquiry question details raised by end users, and tbl_Inquiry_History
for storing inquiry answer details.
However this shows knowledge to store one question store to table tbl_Inquiry_master
and answers that are given by staff to that inquiry stored in table tbl_Inquiry_History
.
For more information I represent table tbl_Inquiry_master
schema:
Column Name Data Type
_________________________________________
Id varchar(50)
Inquiry_subject varchar(100)
Status_id numeric(18, 0)
Created_date datetime
Priority_id numeric(18, 0)
User_id varchar(50)
Email_Address varchar(50)
Service_id numeric(18, 0)
Inquiry_Content varchar(1024)
TimeStamp datetime
Table tbl_Inquiry_History
schema:
Column Name Data Type
_________________________________________
Id numeric(18, 0)
Inquiry_id varchar(50)
Inquiry_subject varchar(50)
Service_id numeric(18, 0)
Priority_id numeric(18, 0)
User_id varchar(50)
Status_id numeric(18, 0)
Inquiry_desc varchar(1024)
IsDisplay bit
IsRead bit
IsReplied bit
TimeStamp datetime
Activity_start_time datetime
Activity_expire_time datetime
Table tbl_User_master
schema:
Column Name Data Type PK/FK Reg Table Ref Key
____________________________________________________________________________________
Id varchar(50) PK - -
User_id varchar(50) FK tbl_Login_master Id
Full_Name varchar(50)
.
.
Email_Address varchar(50)
Table tbl_Question
schema:
Column Name DatType PK/FK Ref Table Ref Key
____________________________________________________________________
Id int PK - -
UserId varchar(50) FK tbl_User_master Id
Body varchar(1024)
Inquiry_Id varchar(50) FK tbl_Inquiry_master Id
Table tbl_Answer
schema:
Column Name DatType PK/FK Ref Table Ref Key
____________________________________________________________________
Id int PK - -
QuestionId int FK tbl_Question Id
Body varchar(1024)
Inquiry_Id varchar(50) FK tbl_Inquiry_master Id
However I don't know how I can store multiple Inquiry's questions(raised by end users) and multiple Inquiry's answers (given by staff user).
i include this stored procedure for how i insert new inquiry generated from End User and how info stores to table hierarchy.
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[InsertInquiry]
(
@Inquiry_subject VARCHAR(50),
@Service_name VARCHAR(50),
@Priority_type VARCHAR(25),
@User_id VARCHAR(50),
@Inquiry_desc VARCHAR(1024),
@email VARCHAR(50),
@NewId VARCHAR(50) OUT
)
AS
SET NOCOUNT ON;
declare @var1 int
declare @var2 int
declare @var3 int
declare @uniqueRef char(14)
set @uniqueRef = dbo.UniqueRefNum(rand(), rand(), rand(), rand())
BEGIN TRAN;
BEGIN TRY
SET @var1= (SELECT [Id] FROM [OmStocks].[dbo].[tbl_Status_master] WHERE (Status_name='Open'))
SET @var2= (SELECT [Id] FROM [OmStocks].[dbo].[tbl_Service_master] WHERE (Service_name=@Service_name))
SET @var3= (SELECT [Id] FROM [OmStocks].[dbo].[tbl_Priority_master] WHERE (Priority_name=@Priority_type))
INSERT INTO [OmStocks].[dbo].[tbl_Inquiry_master]
([Id]
,[Inquiry_subject]
,[Status_id]
,[Created_date]
,[Priority_id]
,[User_id]
,[Email_Address]
,[Service_id]
,[Inquiry_desc])
VALUES
(@uniqueRef,@Inquiry_subject,@var1,CONVERT(DATETIME,GETDATE(), 101),@var3,@User_id,@email,@var2,@Inquiry_desc)
INSERT INTO [OmStocks].[dbo].[tbl_Question]
([UserId],[Body],[Inquiry_Id])
VALUES
(@User_id,@Inquiry_desc,@uniqueRef)
INSERT INTO [OmStocks].[dbo].[tbl_Inquiry_History]
([Inquiry_id]
,[Inquiry_subject]
,[Service_id]
,[Priority_id]
,[User_id]
,[Status_id]
,[Inquiry_desc]
,[IsDisplay]
,[IsRead]
,[IsReplied]
,[Activity_start_time])
VALUES
(@uniqueRef,@Inquiry_subject,@var2,@var3,@User_id,@var1,@Inquiry_desc,0,0,0,CONVERT(DATETIME,GETDATE(), 101))
SET @NewId= @uniqueRef
COMMIT TRAN;
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
-- Raise the error with the appropriate message and error severity
DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int;
SELECT @ErrMsg = ERROR_MESSAGE(), @ErrSeverity = ERROR_SEVERITY();
RAISERROR(@ErrMsg, @ErrSeverity, 1);
END CATCH;
Can someone suggest me the changes I need to make to my schema to store data for this scenario?