2013-03-11 58 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聲明中提到的細節。