首先我必须从查询表中找到代理商名称,如果代理商名称找到,那么如果没有找到代理商名称,那么部分将会发生,第二个查询数据是其他部分将起作用。下面是我的查询PLZ让我知道我错在哪里。提前致谢
create proc ConfirmationMail_ToAgent
(
@Enquiry_Id nvarchar(50)
)
as
begin
select Agent_Name from Enquiry where Enquiry_Id = @Enquiry_Id as jh
if(jh != 'Select')
begin
select e.Agent_Name,e.Agent_Email,e.Client_Name,e.Handled_By,ef.EMail_Id from Enquiry e inner join Employee_FullDetail ef on e.Handled_By = ef.First_Name where e.Enquiry_Id = @Enquiry_Id
end
else
begin
select e.Client_Name,e.Client_Address,e.Handled_By,ef.EMail_Id from Enquiry e inner join Employee_FullDetail ef on e.Handled_By = ef.First_Name where e.Enquiry_Id = @Enquiry_Id
end
end
答案 0 :(得分:0)
在SQL Server
中,使用以下语法
DECLARE @Var varchar(xx)
SELECT @Var = [ColName] FROM [TableName]
所以你的查询应该是这样的:
create proc ConfirmationMail_ToAgent
(
@Enquiry_Id nvarchar(50)
)
as
BEGIN
DECLARE @jh VARCHAR(100)
select @jh= Agent_Name from Enquiry where Enquiry_Id = @Enquiry_Id
if(@jh != 'Select')
begin
select e.Agent_Name,e.Agent_Email,e.Client_Name,e.Handled_By,ef.EMail_Id from Enquiry e inner join Employee_FullDetail ef on e.Handled_By = ef.First_Name where e.Enquiry_Id = @Enquiry_Id
end
else
begin
select e.Client_Name,e.Client_Address,e.Handled_By,ef.EMail_Id from Enquiry e inner join Employee_FullDetail ef on e.Handled_By = ef.First_Name where e.Enquiry_Id = @Enquiry_Id
end
end
答案 1 :(得分:0)
我真的认为你必须阅读DRY principle in programming。请遵循此原则,它将帮助您将来维护您的代码。它看起来你不需要2个查询,你可以只使用一个:
create proc ConfirmationMail_ToAgent
(
@Enquiry_Id nvarchar(50)
)
as
begin
select
case when e.Agent_Name != 'Select' then e.Agent_Name else e.Client_Name end as Name,
case when e.Agent_Name != 'Select' then e.Agent_Email else e.Client_Address end as Address,
e.Handled_By, ef.EMail_Id
from Enquiry as e
inner join Employee_FullDetail as ef on ef.First_Name = e.Handled_By
where e.Enquiry_Id = @Enquiry_Id
end
这个更易于维护 - 如果您必须向结果集添加列,则只需执行一次,更易于阅读 - 您不必想想 - 这两个查询有什么区别? 要完全遵循DRY原则,可以重写此查询,例如,如下所示:
create proc ConfirmationMail_ToAgent
(
@Enquiry_Id nvarchar(50)
)
as
begin
select
C.Name, C.Email,
e.Client_Name, e.Handled_By, ef.EMail_Id
from Enquiry as e
inner join Employee_FullDetail as ef on ef.First_Name = e.Handled_By
outer apply (
select e.Agent_Name, e.Agent_Email where e.Agent_Name != 'Select' union all
select e.Client_Name, e.Client_Address where e.Agent_Name == 'Select'
) as C(Name, Email)
where e.Enquiry_Id = @Enquiry_Id
end