1

我正在使用客户关系管理应用程序。我有两个表:tbl_Inquiry_master用于存储最终用户提出的查询问题详细信息,以及tbl_Inquiry_History用于存储查询答案详细信息。

然而,这显示了将一个问题存储到 table 的知识tbl_Inquiry_master以及工作人员对存储在 table 中的查询给出的答案tbl_Inquiry_History

有关更多信息,我表示表tbl_Inquiry_master模式:

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   

tbl_Inquiry_History架构:

 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 

tbl_User_master架构:

 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)

tbl_Question架构:

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

tbl_Answer架构:

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

但是我不知道如何存储多个 Inquiry 的问题(由最终用户提出)和多个 Inquiry 的答案(由员工用户给出)。

我将这个存储过程包括在我如何插入从最终用户生成的新查询以及如何将信息存储到表层次结构中。

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;

有人可以建议我对架构进行更改以存储此场景的数据吗?

4

3 回答 3

1

简单来说,您需要一个 User 表、一个 Question 表和一个 Answer 表,它们按此顺序形成层次结构。每个答案行都有一个外键返回与其相关的问题。每个问题行都有一个外键返回给提出问题的用户(我想用户会有一个外键返回给客户/合同等)。然后,您将使用仅与该实体相关的属性来装饰每个实体。您的历史表目前似乎更像是一个审计跟踪,而不是一个答案表(主表中的许多非规范化列)。

就像是:-

User table
---------------------------
Id          int
Email       varchar(50)


Question table
---------------------------
Id          int
UserId      int
Body        varchar(1024)


Answer table
---------------------------
Id          int
QuestionId  int
Body        varchar(1024)

然后在适当的级别添加架构中的列。例如,Status_id 是否描述了问题或答案的状态。我想 Priority_id 与问题有关,而不与答案有关,所以它应该只存在于问题表中。

在不知道每列的实际用途的情况下,很难给出与您的架构直接相关的更具体的答案。

于 2013-10-22T18:08:06.760 回答
1

我仍然不清楚 master 和 history 的文本字段中包含什么(以及历史表是否应该是此层次结构的一部分)但是 - 如果您使用以下示例: -

create table tbl_Inquiry_master (
    id int,
    body varchar(1024)
);
create table tbl_Inquiry_history (
    id int,
    inquiry_id int,
    body varchar(1024)
);
create table tbl_question (
    id int,
    inquiry_id int,
    body varchar(1024)
);
create table tbl_answer (
    id int,
    question_id int,
    body varchar(1024)
);
insert into tbl_Inquiry_master values (1, 'inquiry one');
insert into tbl_Inquiry_master values (2, 'inquiry two');
insert into tbl_Inquiry_master values (3, 'inquiry three');
insert into tbl_Inquiry_master values (4, 'inquiry four');
insert into tbl_Inquiry_history values (1, 1,'history 1 (relates to inquiry 1)');
insert into tbl_Inquiry_history values (2, 3,'history 2 (relates to inquiry 3)');
insert into tbl_question values (1,1,'inquiry one question one')
insert into tbl_question values (2,1,'inquiry one question two')
insert into tbl_question values (3,2,'inquiry two question one')
insert into tbl_question values (4,4,'inquiry four question one')
insert into tbl_answer values (1,1,'answer 1 to question 1')
insert into tbl_answer values (2,1,'answer 2 to question 1')
insert into tbl_answer values (3,2,'answer 1 to question 2')
insert into tbl_answer values (4,4,'answer 1 to question 4')
insert into tbl_answer values (5,4,'answer 2 to question 4')
insert into tbl_answer values (6,4,'answer 3 to question 4')

select 
    i.id as inquiry_id,
    i.body as master_body,
    h.body as history_body,
    q.body as question_body,
    a.body as answer_body
from tbl_Inquiry_master i
left join tbl_Inquiry_history h on h.inquiry_id=i.id
left join tbl_question q on q.inquiry_id=i.id
left join tbl_answer a on a.question_id=q.id

你最终得到: -

inquiry_id  master_body    history_body                      question_body              answer_body
----------- -------------- --------------------------------- -------------------------- ----------------------
1           inquiry one    history 1 (relates to inquiry 1)  inquiry one question one   answer 1 to question 1
1           inquiry one    history 1 (relates to inquiry 1)  inquiry one question one   answer 2 to question 1
1           inquiry one    history 1 (relates to inquiry 1)  inquiry one question two   answer 1 to question 2
2           inquiry two    NULL                              inquiry two question one   NULL
3           inquiry three  history 2 (relates to inquiry 3)  NULL                       NULL
4           inquiry four   NULL                              inquiry four question one  answer 1 to question 4
4           inquiry four   NULL                              inquiry four question one  answer 2 to question 4
4           inquiry four   NULL                              inquiry four question one  answer 3 to question 4

然后,您可以用它加载一个转发器,并通过抑制重复的文本,产生一些可能有意义的东西。在层次结构(XML 等)而不是矩形结果集中处理输出可能更容易——但这是一个完全不同的问题。

于 2013-10-24T19:06:13.200 回答
0

好的。我们重来。

  1. 从您的数据库中删除表 tbl_Question 并擦除您对它的任何内存。

  2. 从您的数据库中删除表 tbl_Answer 并删除您对它的所有内存。

  3. 当用户提出问题时,将一行添加到 tbl_Inquiry_master 以记录带有 NEW Id 的问题。如果用户需要提出多个问题,请在 tbl_Inquiry_master 中添加一个新行来记录每个问题(每个问题都有自己的唯一 ID)。

  4. 当员工回复问题时,在 tbl_Inquiry_History 中添加一行来记录答案。该行上的 Inquiry_id 应该指向它在 tbl_Inquiry_master 中相关的问题的 Id。如果其他员工想要对 SAME 问题添加第二个或后续回复,请在 tbl_Inquiry_History 中添加另一行,以记录他们的答案,该行上的 Inquiry_id 指向 tbl_Inquiry_master 中问题的 SAME Id。

于 2013-10-23T14:29:58.710 回答