日期:2014-05-17 浏览次数:20389 次
CREATE TABLE a(h VARCHAR(2), y NUMERIC(3,1));
INSERT INTO a(h,y)
SELECT 'A', 1.7 UNION ALL
SELECT 'B', 0.1;
CREATE TABLE n(h VARCHAR(2), l INT, y NUMERIC(3,1), z INT);
INSERT INTO n(h,l,y,z)
SELECT
'A',2,0.1,222 UNION ALL SELECT
'A',3,0.5,333 UNION ALL SELECT
'A',4,0.6,111 UNION ALL SELECT
'A',22,0.8,666 UNION ALL SELECT
'B',5,1,555 UNION ALL SELECT
'B',11,0.2,777;
CREATE TABLE n_back(h VARCHAR(2), l INT, y NUMERIC(3,1), z INT);
------------------------------------------------
ALTER PROC test_up_a_n
AS
/*
EXEC test_up_a_n
*/
BEGIN
DECLARE @Id INT, @h VARCHAR(2), @h2 VARCHAR(2), @l INT, @y NUMERIC(3,1), @z INT, @a_y NUMERIC(3,1), @flag INT
DECLARE @sum_y NUMERIC(3,1), @save_id INT
SET @sum_y=0
CREATE TABLE #n_temp1(Id INT IDENTITY(1,1), h VARCHAR(2), l INT, y NUMERIC(3,1), z INT, a_y NUMERIC(3,1), flag INT DEFAULT 0);
INSERT INTO #n_temp1(h, l, y, z, a_y)
SELECT n.h, n.l, n.y, n.z, a.y FROM n LEFT JOIN a ON a.h=n.h ORDER BY n.h ASC, n.y DESC;
CREATE TABLE #n_back(Id INT, h VARCHAR(2), l INT, y NUMERIC(3,1), z INT);
--游标检索
DECLARE @MyData CURSOR
SET @MyData = CURSOR FOR
SELECT Id, h, l, y, z, a_y, flag from #n_temp1;
OPEN @MyData
FETCH NEXT FROM @MyData INTO @Id, @h, @l, @y, @z, @a_y
WHILE @@FETCH_STATUS = 0
BEGIN
I