开发者

SQL 2008: SPROC throw error so I can catch in ASP.NET

开发者 https://www.devze.com 2023-02-10 01:46 出处:网络
I\'ve got this simple SPROC: BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT stat开发者_StackOverflowements.

I've got this simple SPROC:

BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT stat开发者_StackOverflowements.
    SET NOCOUNT ON;

    IF EXISTS(SELECT UserName FROM Party WHERE UserName = @UserName)
    BEGIN
        --This means it exists, return it to ASP and tell us
        SELECT 'This record already exists!'
    END
    ELSE
    BEGIN
        --This means the record isn't in there already, let's go ahead and add it
        SELECT 'Record Added'

        -- Insert statements for procedure here
        INSERT INTO Party
            (EmailAddress, UserName, LoginPin)
        VALUES (@EmailAddress, @UserName, @LoginPin)
    END

END

How do I throw an exception from the SPROC so that my .NET C# app can catch the error using a TRY CATCH block?


You would use RAISERROR in your stored procedure.

See this Microsoft article on the topic.

Also, see the MSDN reference on RAISERROR

0

精彩评论

暂无评论...
验证码 换一张
取 消