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
0 Comment(s)