MSSQL中的存储过程用于连接两个表

时间:2015-08-25 05:10:38

标签: mysql sql sql-server sql-server-2008 stored-procedures

我正在尝试编写一个存储过程来验证登录。

我有两个表,它们将保存数据表结构,如下所示。

Table : UserMaster

Userid  Password  IsActive
1111    xyz        1
2222    abc        0

Table : Userdetailes

Userid   Status  StartDate    EndDate
1111     1       2015-08-01   2015-08-24
2222     0       2015-08-01   2015-08-10

这是数据存储在两个表中的方式

我想编写一个存储过程来查看使用join

的数据

1 个答案:

答案 0 :(得分:1)

尝试这样的事情......

CREATE PROCEDURE [dbo].[uspauth]
   @LoginId        NVARCHAR(30)
  ,@ActiveStatus INT OUTPUT
AS
BEGIN
    SET NOCOUNT ON
    BEGIN TRY

        --1 - Both Active, 2 - User InActive & Package Active, 3 - User Inactive & Package Inactive , 4 - Not authosrised
        DECLARE @Username VARCHAR(40), @isAct Bit,@stat Bit   

        IF NOT EXISTS(SELECT 1 FROM UserSet where UserID = @LoginId)
          BEGIN
               RAISERROR('login does not exist',16,1)
               RETURN;
          END
        SELECT @Username = ISNULL(UserID,0), @isAct  = ISNULL(IsActive, 0) from UserSet where UserID = @LoginId)
        Select @stat = ISNULL([status],0) from Subscription where UserID = @LoginId 
                                                   and (DateOfStart <= CAST(GETDATE() as DATE) or DateOfStart is NULL) 
                                                   and (DateOfEnd   >= Cast(GETDATE() as DATE) or DateOfEnd   is NULL)

         IF (@isAct = 1 AND @stat = 1)  --1 - Both Active
           BEGIN
             SET @ActiveStatus  = 1;
           END
        ELSE IF (@isAct = 0 AND @stat = 1) -- 2 - User InActive & Package Active
           BEGIN
             SET @ActiveStatus  = 2;
           END
        ELSE IF (@isAct = 1 AND @stat = 0) -- 3 - User Active & Package Inactive
           BEGIN
             SET @ActiveStatus  = 3;
           END
        ELSE IF (@isAct = 0 AND @stat = 0) -- 4 - Not authosrised
           BEGIN
             SET @ActiveStatus  = 4;
           END

    END TRY
    BEGIN CATCH

        IF @@TRANCOUNT > 0 
            BEGIN
                ROLLBACK TRAN
            END

        EXEC uspProcErrorLog    
    END CATCH
END