將多結果組成一張大表並使用迴圈方式,產生多筆結果
USE PA DECLARE @com_date1 varchar(8) DECLARE @com_date2 varchar(8) DECLARE @COP_NO varchar(4) DECLARE @PROJM_NO varchar(6) DECLARE @tstStr as varchar(2000) DECLARE @tstField as varchar(10) DECLARE @tstCnt as int set @com_date1 ='20151001' set @com_date2 ='20151031' set @COP_NO = 'FH01' --set @PROJM_NO ='13A003' select @tstStr = '11A010,11A005,11A006,11A007,11A009,12A005,13A001,13A002,13A003,13A004,13A005,14A006' --將這些code寫到Store Procedure中 ,這可以是AP傳入的參數 while (@tstStr <> '') begin SELECT @tstCnt = PATINDEX('%,%', @tstStr) if @tstCnt > 0 begin select @tstField = left(@tstStr,@tstCnt-1) -- select @tstField --將資料抓出 select @tstStr = right(@tstStr,len(@tstStr)-@tstCnt) end else begin select @tstField = @tstStr -- select @tstField --將資料抓出 select @tstStr = '' end --select @tstField select @PROJM_NO = @tstField select @tstFie