locked
Incorrect Syntax near Insert expecting '(' , or Select RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I have below code . After Union All Insert it is giving above error

    IF EXISTS(SELECT TOP 1 EmployeeID FROM dbo.Employee)
    				begin
    				  INSERT INTO dbo.Employee (EmployeeID,FirstName)
    						SELECT No_,[First Name]
    						FROM [Test] AS t0 
    						WHERE t0.No_  not in (Select EmployeeID from dbo.Employee) and t0.No_ is not null and t0.Status = 0 
    					Union All
    					INSERT INTO dbo.Employee (EmployeeID,FirstName)
    						SELECT No_,[First Name]
    						FROM [Test] AS t0 
    						WHERE t0.No_ not in (Select EmployeeID from dbo.Employee) and t0.No_ is not null 
    					END
    			else
    				Begin
    					
    				End

    Thanks

    Thursday, November 15, 2018 1:16 PM

Answers

  • User-2082239438 posted

    Hi

      I have below code . After Union All Insert it is giving above error

    IF EXISTS(SELECT TOP 1 EmployeeID FROM dbo.Employee)
    				begin
    				  INSERT INTO dbo.Employee (EmployeeID,FirstName)
    						SELECT No_,[First Name]
    						FROM [Test] AS t0 
    						WHERE t0.No_  not in (Select EmployeeID from dbo.Employee) and t0.No_ is not null and t0.Status = 0 
    					Union All
    					INSERT INTO dbo.Employee (EmployeeID,FirstName)
    						SELECT No_,[First Name]
    						FROM [Test] AS t0 
    						WHERE t0.No_ not in (Select EmployeeID from dbo.Employee) and t0.No_ is not null 
    					END
    			else
    				Begin
    					
    				End

    Thanks

    You will need to remove the second insert statement after union all. Union All will give the result set from the both the select query & then the result set will get insert into the table.

    find the sample below.

    IF EXISTS(SELECT TOP 1 EmployeeID FROM dbo.Employee)
    begin
    	INSERT INTO dbo.Employee (EmployeeID,FirstName)
    	SELECT No_,[First Name]
    	FROM [Test] AS t0 
    	WHERE t0.No_  not in (Select EmployeeID from dbo.Employee) and t0.No_ is not null and t0.Status = 0 
    	
    	Union All
    	
    	---INSERT INTO dbo.Employee (EmployeeID,FirstName) ---Remove this Lince
    	SELECT No_,[First Name]
    	FROM [Test] AS t0 
    	WHERE t0.No_ not in (Select EmployeeID from dbo.Employee) and t0.No_ is not null 
    END
    --else ---------Removed Un Used Else Statements
    --Begin
    	
    --End

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 15, 2018 1:32 PM