Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Table alias while making joins

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 63
    Comment on it

    While performing joins you need specific columns of the joined tables so for doing that you need to mention the name of the column with their table name.

     

    This task gets tedious because you have to write your table name with every column you want.

     

    So a better way would be to create table alias while performing joins it will reduce your effort by substituting a big table name with a smaller one.

     

    ALTER PROC [dbo].[uspGetNudgesDetails]
    @AccessToken VARCHAR(50),
    @BeaconID VARCHAR(50)=NULL
    AS
    
    BEGIN
    
    	DECLARE @CompanyID BIGINT =0
    	DECLARE @TargetUserID BIGINT=0
    	DECLARE @UserName varchar(50)
    -- Get user and company
    
    	SELECT @UserName=[FirstName] + ' ' + LastName, 
    			@TargetUserID=[ID],
    			@CompanyID=[CompanyID]
    		FROM [User] WHERE AccessToken=@AccessToken
    
    
    		----DECLARE @CreatedDate AS DATETIME = GETDATE()
    		----DECLARE @ExpiryDate AS DATETIME = (SELECT DATEADD(DAY, @DaysTillExpire, @CreatedDate))
    		----SET @ExpiryDate = (SELECT DATEADD(HOUR, @HoursTillExpire, @ExpiryDate))
    
    		UPDATE NUDGE
    		SET DaysTillExpire = CASE  WHEN DATEDIFF(day, GETDATE(), ExpiryDate) < 0 THEN 0
    									ELSE DATEDIFF(day, GETDATE(), ExpiryDate)
    							END				
    		,
    		HoursTillExpire = CASE  WHEN DATEDIFF( hour, GETDATE(), ExpiryDate) < 0 THEN 0
    									ELSE DATEDIFF(day, GETDATE(), ExpiryDate)
    							END				
    
    		,
    		IsExpired = CASE WHEN DATEDIFF(day, GETDATE(), ExpiryDate) < 0 AND DATEDIFF( hour, GETDATE(), ExpiryDate) < 0 THEN 1
    							ELSE 0
    							END
    -- For general message with no location
    
    	SELECT N.[ID],
    		N.[Message],
    		@BeaconID as [BeaconID],		
    		N.[LocationID],
    		NULL AS Location,
    		N.[TargetUserID],
    		@UserName as [TargetUser],	
    		N.[IsCompleted],
    		N.[IsDelivered],
    		N.[IsExpired],
    		N.[IsDeleted],
    		N.[Url],		
    		N.[DaysTillExpire],
    		N.[HoursTillExpire],
    		--DATEDIFF( day, GETDATE(), N.ExpiryDate ) AS DaysTillExpire,
    		--DATEDIFF( hour, GETDATE(), N.ExpiryDate ) AS HoursTillExpire,
    		(U.[FirstName] + ' ' + U.[LastName]) AS [CreatedBy],
    		CONVERT(VARCHAR(20), N.[CreatedDate], 103) as [CreatedDate],
    		@CompanyID as [CompanyID]
    		
    		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],
    		@BeaconID as [BeaconID],
    		N.[LocationID],
    		L.[Location],
    		N.[TargetUserID],
    		@UserName as [TargetUser],	
    		N.[IsCompleted],
    		N.[IsDelivered],
    		N.[IsExpired],
    		N.[IsDeleted],		
    		N.[Url],		
    		N.[DaysTillExpire],
    		N.[HoursTillExpire],
    		--DATEDIFF( day, GETDATE(), N.ExpiryDate ) AS DaysTillExpire,
    		--DATEDIFF( hour, GETDATE(), N.ExpiryDate ) AS HoursTillExpire,	
    		(U.[FirstName] + ' ' + U.[LastName]) AS [CreatedBy],
    		CONVERT(VARCHAR(20), N.[CreatedDate], 103) as [CreatedDate],
    		@CompanyID as [CompanyID]
    		
    		
    		FROM [NUDGE] AS N 
    			INNER JOIN  [User] AS U ON N.[CreatedBy]=U.[ID] 
    			INNER JOIN [LOCATION] AS L ON L.[ID]=N.[LocationID] 
    		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

     

    Here while making joins we are using table alias like for User we are using U and for Location we are using L

     

    This makes our query writing easy and effective in terms of execution

    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: