SQL Server 2008 - 存储过程,嵌套变量不起作用

时间:2014-06-20 03:29:21

标签: sql sql-server nested-loops

我正在尝试在SQL Server中使用临时表来将ID从表中拉出来然后循环一年中的12个月...我可以将产品.ID拉出来没有问题,但是当我进入一年中几个月的NESTED循环,productID变量不会随之移动......

    USE [testDB]
    GO
/****** Object:  StoredProcedure [dbo].[marketInsert]    Script Date: 06/19/2014 21:38:14     ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      Me
-- Create date: 6/19/2014
-- Description: Insert transaction records for new markets
-- =============================================
ALTER PROCEDURE [dbo].[marketInsert] @marketID int
-- Add the parameters for the stored procedure here
AS

DECLARE @productID int
DECLARE @i int
DECLARE @imax int
DECLARE @year int
DECLARE @NumberRecords int, @RowCount int
set @imax = 12
set @i = 1
CREATE TABLE #Products (
productID int
 )
INSERT INTO #Products (productID) SELECT ID FROM products
SET @NumberRecords = @@ROWCOUNT
SET @RowCount = 1
WHILE @RowCount <= @NumberRecords
BEGIN
SELECT @productID = productID 
FROM #Products
WHERE productID = @RowCount
print @productID --WILL PRINT HERE!!!
WHILE (@i <= @imax) --loop through each month
    Begin
        --print @productID WILL NOT PRINT HERE!!!!!
        --print 'Year: ' + cast(@year as varchar(5)) + 'ProductID: ' + cast(@productID as varchar(30)) + ' Month: ' + cast(@i as varchar(2)) + ' MarketID: ' + cast(@marketID as varchar(2))

    SET @i = @i + 1 
END -- WHILE @i <= imax

SET @RowCount = @RowCount + 1
END    

BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here

END

4 个答案:

答案 0 :(得分:0)

这对你有用

-- =============================================
-- Author:      Me
-- Create date: 6/19/2014
-- Description: Insert transaction records for new markets
-- =============================================

    ALTER PROCEDURE [dbo].[marketInsert] @marketID int
-- Add the parameters for the stored procedure here
AS

DECLARE @productID int

DECLARE @i int

DECLARE @imax int

DECLARE @year int

DECLARE @NumberRecords int, @RowCount int

set @imax = 12

set @i = 1

CREATE TABLE #Products (
productID int , rown
 )

INSERT INTO #Products (productID,rown) 

SELECT ID,row_number() over(order by id)  FROM products

SET @NumberRecords = @@ROWCOUNT

SET @RowCount = 1

WHILE @RowCount <= @NumberRecords

BEGIN

SELECT @productID = productID 

FROM #Products

WHERE rown = @RowCount

print @productID --WILL PRINT HERE!!!

WHILE (@i <= @imax) --loop through each month

    Begin
        --print @productID WILL NOT PRINT HERE!!!!!
        --print 'Year: ' + cast(@year as varchar(5)) + 'ProductID: ' + cast(@productID as varchar(30)) + ' Month: ' + cast(@i as varchar(2)) + ' MarketID: ' + cast(@marketID as varchar(2))


    SET @i = @i + 1 

END -- WHILE @i <= imax


SET @RowCount = @RowCount + 1

END    


BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here

END 

答案 1 :(得分:0)

更改临时表,如下所示(添加IDENTITY列)

CREATE TABLE #Products (Num INT IDENTITY(1,1), productID int)

现在在While循环中获取产品ID

SELECT @productID = productID 
FROM   #Products
WHERE  Num = @RowCount

这样可行。

您的程序

ALTER PROCEDURE [dbo].[marketInsert] @marketID int
-- Add the parameters for the stored procedure here
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

DECLARE @productID int
DECLARE @i int
    DECLARE @imax int
    DECLARE @year int
    DECLARE @NumberRecords int, @RowCount int
    set @imax = 12
    set @i = 1
    CREATE TABLE #Products (Num IDENTITY(1,1),
    productID int
     )
    INSERT INTO #Products (productID) SELECT ID FROM products
    SET @NumberRecords = @@ROWCOUNT
    SET @RowCount = 1
    WHILE @RowCount <= @NumberRecords
    BEGIN
    SELECT @productID = productID 
    FROM #Products
    WHERE Num = @RowCount
    print @productID --WILL PRINT HERE!!!
    WHILE (@i <= @imax) --loop through each month
        Begin
            --print @productID WILL NOT PRINT HERE!!!!!
            --print 'Year: ' + cast(@year as varchar(5)) + 'ProductID: ' + cast(@productID as varchar(30)) + ' Month: ' + cast(@i as varchar(2)) + ' MarketID: ' + cast(@marketID as varchar(2))

        SET @i = @i + 1 
    END -- WHILE @i <= imax

    SET @RowCount = @RowCount + 1
    END    


    -- Insert statements for procedure here

    END

答案 2 :(得分:0)

我无法使用临时表...我将尝试使用其他人建议的游标。

答案 3 :(得分:0)

试试这个

ALTER PROCEDURE [dbo].[marketInsert] @marketID int
-- Add the parameters for the stored procedure here
AS
begin 
    DECLARE @productID int
    DECLARE @month int
    DECLARE @year INT = 2014 
    --i set year variable to a default value to 2014
    --if this is null then 2nd print will not print any data

    --create month table
    DECLARE @monthtable table(monthid integer);
    insert into @monthtable 
    values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12);

    --declare cursor
    DECLARE cur CURSOR FOR
    SELECT 
    Id, Monthid
    FROM products, @monthtable
    ORDER BY 1,2
    OPEN cur

    FETCH NEXT FROM cur INTO @productID,@month;
    WHILE @@FETCH_STATUS = 0
    BEGIN   

    print @productID --WILL NOW PRINT HERE!!!!!
    print 'Year: ' + cast(@year as varchar(5)) + 'ProductID: ' 
       + cast(@productID as varchar(30)) + ' Month: ' 
       + cast(@month as varchar(2)) + ' MarketID: ' 
       + cast(@marketID as varchar(2))

    FETCH NEXT FROM cur INTO @productID,@month;

    END

    CLOSE cur;
    DEALLOCATE cur;
END;


EXEC [dbo].[marketInsert] 1