locked
id select sql RRS feed

  • Question

  • User639567535 posted

     Here i want to verifiy if employee is exist in that client or not if exist then i want to shows message success el
                 else i want to show invalid employee

                 declare @Employeeid int
                
                if @Employeeid=1616
                Select @Employeeid=employeeindex from Employee_table
                 where clientindex in (Select Clientid from Employee_table where Clientid=658 )
                select * from Employee_table where Employeeid=@Employeeid
                
                 else
                    raiserror ('Invalid Employee! ', 16,1)
    
                 return



            

            this shows an error Incorrect syntax near the keyword 'else'.

    Tuesday, October 31, 2017 9:14 AM

All replies

  • User1992938117 posted

    Can you try to add BEGIN END to define condition boundary

    DECLARE @Employeeid INT
    IF @Employeeid = 1616
    BEGIN
    	SELECT @Employeeid = employeeindex
    	FROM Employee_table
    	WHERE clientindex IN (
    			SELECT Clientid
    			FROM Employee_table
    			WHERE Clientid = 658
    			)
    
    	SELECT *
    	FROM Employee_table
    	WHERE Employeeid = @Employeeid
    END
    ELSE
    BEGIN
    	RAISERROR (
    			'Invalid Employee! '
    			,16
    			,1
    			)
    END
    
    RETURN

    Also I would say, check in code if you have a value then its exist else not exist that throwing error from SQL.

    Tuesday, October 31, 2017 9:24 AM
  • User639567535 posted

    this always shows invalid employee where as 1616 exist in clientid=658

    Tuesday, October 31, 2017 10:20 AM
  • User475983607 posted

    Capricon User

    this always shows invalid employee where as 1616 exist in clientid=658

    The code is very confusing.  @Employeeid (if condition) is evaluated before being set.  I would expect the code as written always raises an error.

    Secondly, this line can produce multiple results which can throw an error.

    Select @Employeeid=employeeindex from Employee_table
        where clientindex in (Select Clientid from Employee_table where Clientid=658 )

    Tuesday, October 31, 2017 10:32 AM
  • User1992938117 posted
    If condition will not evaluate because it's not having any value, in current script only else part will execute
    Tuesday, October 31, 2017 2:44 PM
  • User-707554951 posted

    Hi Capricon,

    As you have declared @Employeeid, but you have not set value to it.

    So though Employeeid=1616 exits in clientid=658, the message always shows Invalid Employeeid!.

    You could refer to the code as below:

    declare @Employeeid int
    set @Employeeid=1616
    if @Employeeid=1616
    begin
        select * from [Employee_table] where Employeeid=@Employeeid
                    --Select Employeeid from Employee_table
                    --where clientindex in (Select Clientid from Employee_table where Clientid=658 )
                    --select * from Employee_table where Employeeid=@Employeeid
    end
    else
    begin
        raiserror ('Invalid Employee! ', 16,1)
                    end
        return

    Output:

    Best regards

    Cathy

    Wednesday, November 1, 2017 6:59 AM