Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Joins in SQL with default values

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 415
    Comment on it

    While making joins in SQL you can have parameters which are passed in your query for getting result

    ALTER PROC [dbo].[uspGetNudgesDetails]
    @AccessToken VARCHAR(50),
    @BeaconID VARCHAR(50)=NULL,
    @CompanyID BIGINT =0,
    @TargetUserID BIGINT=0 
    AS
    
    
    
    BEGIN
    
    
    -- Get user and company
    
    SELECT @TargetUserID=[ID] ,@CompanyID=[CompanyID]  from [User] WHERE AccessToken=@AccessToken
    
    -- For general message with no location
    
    SELECT N.[ID],N.[Message],N.[LocationID],N.[TargetUserID],N.[Url],CONCAT(U.[FirstName],U.[LastName]) AS [CreatedBy],CONVERT(VARCHAR(20), N.[CreatedDate], 103) as [CreatedDate],N.[DaysTillExpire],N.[HoursTillExpire] 
    	FROM [NUDGE] AS N INNER JOIN  [User] AS U ON N.[CreatedBy]=U.[ID]
    	WHERE N.[LocationID]=0 
    	AND N.[TargetUserID]=@TargetUserID
    	AND N.[IsDeleted]=0 
    	AND N.[IsExpired]=0 
    	AND N.[IsCompleted]=0 
    
    	UNION
    
    -- For location dependent messages
    
    SELECT N.[ID],N.[Message],N.[LocationID],N.[TargetUserID],N.[Url],CONCAT(U.[FirstName],U.[LastName]) AS [CreatedBy],CONVERT(VARCHAR(20), N.[CreatedDate], 103) as [CreatedDate],N.[DaysTillExpire],N.[HoursTillExpire] 
    	FROM [NUDGE] AS N INNER JOIN  [User] AS U ON N.[CreatedBy]=U.[ID]
    	WHERE N.[LocationID]=(SELECT [LocationID] FROM [Location] WHERE [BeaconUUID]=@BeaconID and [CompanyID]=@CompanyID)
    	AND N.[TargetUserID]=@TargetUserID
    	AND N.[IsDeleted]=0 
    	AND N.[IsExpired]=0 
    	AND N.[IsCompleted]=0 
    
    END

    So what we do is we will create joins based on values passed to these parameters by passing default values if user does not provide any value while invoking it

    SQL Server

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: