无法插入临时表。为什么?

时间:2014-10-15 05:45:15

标签: sql-server-2008

以下是原始查询

DECLARE @categoryKeys VARCHAR(max),
        @startPromos INT,
        @countPromos INT,
        @promoKey INT,
        @parse VARCHAR(max),
        @final VARCHAR(max),
        @branch_code VARCHAR(max)

CREATE TABLE #NewperBranch
(
    id INT,
    promoKey INT,
    Branch VARCHAR(50),
    Combo VARCHAR(60),
    Category VARCHAR(50),
    Item VARCHAR(60)
) 

--INSERT INTO #NewperBranch
--SELECT
SELECT  promo_key FROM dbo.branch_promos WHERE branch_lookup_key = 25

SELECT @countPromos = COUNT(*) from #NewperBranch
SET @startPromos = 1
WHILE @startPromos <= @countPromos

SELECT @branch_code = branch_code FROM dbo.branches WHERE lookup_key = @promoKey
SELECT @promoKey = promoKey FROM #NewperBranch WHERE id = @startPromos
SELECT @categoryKeys = p.category_keys FROM dbo.promos p WHERE [key] = @promoKey    

 -- INSERT INTO #NewperBranch
-- 0,
--@promoKey,
--@branch_code,
--'',
--'',
--''

INSERT INTO #NewperBranch
    SELECT npb.id,-- AS 'id',
           p.[key],-- AS 'promoKey',
           b.branch_code,-- AS 'Branch',
           p.name,-- AS 'Combo',
           c.name,-- AS 'Category',
           i.name --AS 'Item'
    FROM items i 
    INNER JOIN dbo.branches b ON i.category_key = b.lookup_key
    INNER JOIN dbo.categories c ON i.category_key = c.[key]  
    INNER JOIN dbo.promos p ON p.[key] = @promoKey
    INNER JOIN #NewperBranch npb ON npb.id = npb.id
    WHERE category_key IN (SELECT [key] FROM dbo.categories
    WHERE [key] IN (SELECT * FROM dbo.parse_comma_delimited_string(@categoryKeys)))
    SET @startPromos = @startPromos + 1

SELECT * FROM #NewperBranch
DROP TABLE #NewperBranch

以上是原始查询..  需要将结果插入临时表,但它一直显示空白  结果。这是因为内心的加入?

0 个答案:

没有答案