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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 26
    Comment on it

    EXISTS simply tests whether the inner query returns any row. If it does, then the outer query proceeds.

     

    If not, the outer query does not execute, and the entire SQL statement returns nothing.

     

    The general syntax for the Exist statement is

     

    SELECT "column_name1"
    FROM "table_name1"
    WHERE EXISTS
    (SELECT *
    FROM "table_name2"
    WHERE "condition");

     

    So we can get any kind of validation by using this clause in our SQL query.

     

    ALTER PROC [dbo].[uspSaveNudgeForFriend]
    	 @AccessToken VARCHAR(50)
    	,@Message VARCHAR(500)
    	,@Url VARCHAR(500)
    	,@LocationID BIGINT
    	,@TargetUserID BIGINT
    	,@DaysTillExpire INT
    	,@HoursTillExpire INT
    	,@IsCompleted BIT
    	,@IsDeleted BIT
    	,@IsExpired BIT
    	,@IsDelivered BIT
    AS
    	IF EXISTS(SELECT 1 FROM [dbo].[User] WHERE AccessToken = @AccessToken AND IsDeleted = 0)
    	BEGIN
    		DECLARE @CreatedDate AS DATETIME = GETDATE()
    		DECLARE @ExpiryDate AS DATETIME = (SELECT DATEADD(DAY, @DaysTillExpire, @CreatedDate))
    		SET @ExpiryDate = (SELECT DATEADD(HOUR, @HoursTillExpire, @ExpiryDate))
    
    		INSERT INTO [dbo].[Nudge]
    		(
    			 [Message]
    			,[Url]
    			,[LocationID]
    			,[TargetUserID]
    			,[DaysTillExpire]
    			,[HoursTillExpire]
    			,[IsCompleted]
    			,[IsDeleted]
    			,[ExpiryDate]
    			,[IsExpired]
    			,[IsDelivered]
    			,[CreatedBy]
    			,[CreatedDate])
    		VALUES
    		(
    			 @Message
    			,@Url
    			,@LocationID 
    			,@TargetUserID 
    			,@DaysTillExpire 
    			,@HoursTillExpire 
    			,@IsCompleted 
    			,@IsDeleted 
    			,@ExpiryDate
    			,@IsExpired 
    			,@IsDelivered 
    			,(SELECT ID FROM [dbo].[User] WHERE AccessToken = @AccessToken) 
    			,@CreatedDate
    		)
    
    		SELECT 1
    	END
    	ELSE
    	BEGIN
    		SELECT 2
    	END

     

    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: