2013-03-11 61 views
0

我有一个从存储过程要求的客户的信息在基于客户端的电子邮件地址,直到@标志SQL Server 2008的一个ASP.net。由于在这个小型组织中,客户经常在3个月后改变,但电子邮件地址保持不变。SQL Server存储过程的电子邮件验证

E.g.一个电子邮件地址为[email protected]的客户在3-4个月后完成合同,然后将该电子邮件地址分配给其他人。

现在,这里是我的问题:我想我的存储过程才能找到客户信息后,他/她进入obois_in4,并按下按钮Search。我不希望他们输入整个电子邮件的原因是因为它太长,其次他们可以在输入时出错,但打字如obois_in4不是什么大问题。

我写的可以按名称搜索客户端的代码,但同样,客户总是后3-4个月改变,但电子邮件地址保持不变。

ALTER PROCEDURE [dbo].[sp_find_client_information] 
-- Add the parameters for the stored procedure here 
@client_email varchar (50) = null 
AS Declare @numOfRows int BEGIN 
-- SET NOCOUNT ON added to prevent extra result sets from 
-- interfering with SELECT statements. 
SET NOCOUNT ON; 

-- Insert statements for procedure here 
SELECT @numOfRows = COUNT (*) 
    From helpdesk_clients 
Where --change first name and 
    client_firstName = @client_email or client_lastName = @client_email; 

begin 
if (@numOfRows = 0) 
    select @numOfRows; 
else if (@numOfRows = 1) 
select 
    client_id, 
    client_firstName, 
    client_lastName, 
    client_work_email, 
    client_work_phone, 
    client_work_phone_ext, 
    client_office, 
    dept_nom, 
    client_position 

from 
    helpdesk_clients join departments 
    on 
    helpdesk_clients.dept_id = departments.dept_id 
    where client_firstName like '%'[email protected]_email+'%'; 
end 
END 

的电子邮件地址总是obois加上下划线_再系信息技术为in的名称,然后在这种情况下,数字等4开始。例如[email protected]

回答

0

我很惊讶甚至没有人费心寻找到这一点。最好的解决方案是使用Substring()CharIndex()

使用SUBSTRING (expression ,start , length)我们可以截断从字符串中的位置开始的字符串,直到字符串中的指定位置。用CHARINDEX (expressionToFind ,expressionToSearch [ , start_location ]),我们可以找到给定字符串中某个字符的位置。

substring (work_email, 1, CHARINDEX('@', work_email)-1) = @work_email确保参数不必像[email protected],并且它是一个客户端进入他的完整电子邮件像[email protected]一个大麻烦,他将只需要输入shwan.smith,该脚本将搜索shawn.smith[email protected]直到@为止。

例如

在存储过程中,假设@work_email是参数,它的值是“shawn.smith”

select 
client_id, 
client_firstName, 
client_lastName, 
client_work_email, 
client_work_phone, 
client_work_phone_ext, 
client_office, 
dept_nom, 
client_position 
from 
helpdesk_clients join departments 
on 
helpdesk_clients.dept_id = departments.dept_id 
where substring (work_email, 1, CHARINDEX('@', work_email)-1) = @work_email; 

将返回所有在Select声明中提到的细节。