I am using 4 select queries using union all, getting error saying incorrect syntax near union.but when i am using just 2 select statements it is working is there a limit with union all, i am really confued. i could'nt find any mistake the way i wrote the select statements.please help:Alter PROCEDURE [dbo].[USP_GetDMTree] @UserName nvarchar(50), @ContractID int ASDECLARE @ErrorCode intDeclare @projid intDeclare @progid intSet @progid = (Select top 1 progid from TAB_ccsNetContracts where contractid=@ContractID and deleted='0')Set @projid = (Select top 1 projid from TAB_ccsNetContracts where contractid=@ContractID and deleted='0')SET NOCOUNT ONSELECT @ErrorCode = @@ErrorIF @ErrorCode = 0BEGINSELECT DISTINCT DMID, DMTitle, '' as AccessMode FROM TAB_ccsNetDM where agency='1' and deleted = '0'union all -- get the DM rows with userid and contractid SELECT DISTINCT A.DMID, A.DMTitle, B.AccessMode FROM TAB_ccsNetDM AS A join TAB_ccsNetUserAccess AS B ON A.ContractID = B.ContractID WHERE B.UserName = @UserName AND B.ccsNetModule = 'DM' AND A.ContractID=@ContractID AND A.Deleted = '0' AND B.Deleted = '0' ORDER BY A.DMIDunion all -- get the DM rows with userid and progid SELECT DISTINCT A.DMID, A.DMTitle, B.AccessMode FROM TAB_ccsNetDM AS A join TAB_ccsNetUserAccess AS B ON A.ProgID = B.ProgID AND A.ContractID = 0 WHERE B.UserName = @UserName AND B.ccsNetModule = 'DM' AND A.ProgID=@progid AND A.Deleted = '0' AND B.Deleted = '0' ORDER BY A.DMIDunion all -- get the DM rows with userid and projid SELECT DISTINCT A.DMID, A.DMTitle, B.AccessMode FROM TAB_ccsNetDM AS A join TAB_ccsNetUserAccess AS B ON A.ProjID = B.ProjID AND A.ContractID = 0 WHERE B.UserName = @UserName AND B.ccsNetModule = 'DM' AND A.ProjID=@projid AND A.Deleted = '0' AND B.Deleted = '0' ORDER BY A.DMID SELECT @ErrorCode = @@ErrorENDSET NOCOUNT OFFRETURN @ErrorCode
Thank you very much for the helpful info.