0

@サインまでのクライアントの電子メールアドレスに基づいて、SQLServer2008のストアドプロシージャからクライアントの情報を要求するASP.netがあります。この小さな組織では、クライアントは3か月後に変更されることがよくありますが、メールアドレスは同じままです。

たとえば、メールアドレスを持つクライアントはobois_in4@cegepoutaouais.qc.ca3〜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 '%'+@client_email+'%';
end
END

obois電子メールアドレスは常にアンダースコアで始まり、次に_部門情報技術の名前、そしてこの場合inのように数字が続きます。4例えばobois_in4@cegepoutaouais.qc.ca

4

1 に答える 1

0

誰もこれを調べても気にしないことに驚いています。最善の解決策はSubstring()CharIndex()

これによりSUBSTRING ( expression ,start , length )、文字列内の位置から文字列内の指定された位置まで文字列を切り捨てることができます。を使用CHARINDEX ( expressionToFind ,expressionToSearch [ , start_location ] )すると、指定された文字列内の文字の位置を見つけることができます。

substring (work_email, 1, CHARINDEX('@', work_email)-1) = @work_emailパラメータがのようである必要がないことを保証しshawn.smith@cegepoutaouais.qc.ca、クライアントがのように完全な電子メールを入力するのは非常に面倒です。入力shawn.smith@cegepoutaouais.qc.caするだけで、スクリプトはサインまでshwan.smith検索しshawn.smithます。shawn.smith@cegepoutaouais.qc.ca@

例えば

ストアドプロシージャでは、@work_emailisパラメータとその値が'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ステートメントに記載されているすべての詳細を返します。

于 2013-06-25T20:09:50.873 に答える