INSERT INTO @tblTemp(OrderBy,EmployeeID_Name,ProductID,ProductName,ProductSpec,Qty)
SELECT dbo.产品表.排序, CONVERT(varchar, dbo.btj_Qry计件明细.计件雇员)
+ dbo.btj_Qry计件明细.姓名 AS 工号姓名, dbo.产品表.产品编号,
dbo.产品表.产品名称, dbo.产品表.产品规格, dbo.btj_Qry计件明细.良品数
FROM dbo.产品表 INNER JOIN
dbo.btj_Qry计件明细 ON
dbo.产品表.产品编号 = dbo.btj_Qry计件明细.产品编号
WHERE (dbo.btj_Qry计件明细.计件日期 BETWEEN CONVERT(DATETIME,
'2006-09-26 00:00:00', 102) AND CONVERT(DATETIME, '2006-10-25 00:00:00', 102))
AND (dbo.btj_Qry计件明细.计件职场 = @PostID)
ECUTE ('DECLARE corss_cursor CURSOR FOR SELECT DISTINCT EmployeeID_Name from [url=mailto:'+@tblTemp+']'+@tblTemp+'[/url] for read only ')
BEGIN
SET NOCOUNT ON
SET @strSql ='SELECT OrderBy,ProductID,ProductName,ProductSpec, Sum(Qty) AS QtyTotal'
OPEN corss_cursor
WHILE (0=0)
BEGIN
FETCH NEXT FROM corss_cursor
INTO @strTmpCol
IF (@@fetch_status<>0) BREAK
SET @strSql = @strSql + ', Sum(CASE EmployeeID_Name WHEN ''' + @strTmpCol + ''' THEN Qty ELSE Null END) AS ['+ @strTmpCol+']'
END
SET @strSql = @strSql + ' from [url=mailto:'+@tblTemp+']'+@tblTemp+'[/url] group by OrderBy,ProductID,ProductName,ProductSpec Order by OrderBy'
EXECUTE(@strSql)
IF @@error <>0 RETURN @@error
CLOSE corss_cursor
DEALLOCATE corss_cursor RETURN 0