2016-10-15 48 views
-1
Alter procedure spMRI_TAG_try 
@DocNum int 
as 
declare @cnt int 
declare @count int 
declare @cardname nvarchar(100) 
declare @Docdate datetime 
declare @itemCode nvarchar(50) 
declare @Dscription nvarchar(100) 
declare @Quantity numeric(19,6) 
declare @ManBtchNum char(1) 
declare @SalPackUn numeric(19,6) 
declare @ExpDate datetime 
begin 
set @cnt = 1 
select @Count = pdn1.Quantity/OITM.SalPackUn from pdn1 inner join OITM on pdn1.ItemCode=OITM.ItemCode 
while @cnt <= @count 
insert into #temp2 values(@cardname,@DocDate,@itemcode,@Dscription,@Quantity,@ManBtchNum,@SalPackUn,@ExpDate) 
select @cardname = a.CardName,@DocDate=a.DocDate,@itemcode=b.ItemCode,@Dscription=b.Dscription,@Quantity=b.Quantity,@ManBtchNum=c.ManBtchNum,@SalPackUn=c.SalPackUn,@ExpDate=d.ExpDate 
from OPDN a inner join PDN1 b on a.DocEntry = b.DocEntry inner join OITM c on c.ItemCode = b.ItemCode inner join OBTN d on c.ItemCode = d.ItemCode and [email protected] and d.ExpDate is not null 
set @[email protected]+1 
end 
select * from #temp2 

但給我一個invalid object name #temp2錯誤。,但給我錯誤,無效的對象名稱#temp2

+2

調用這個程序。會議應臨時表名爲'#temp2' –

回答

0

while循環之前創建臨時表:

create table #temp2 (Cardname ...) 
while @cnt <= @count 
insert into #temp2 values(@cardname,@DocDate,@itemcode,@Dscription,@Quantity,@ManBtchNum,@SalPackUn,@ExpDate) 
select @cardname = a.CardName,@DocDate=a.DocDate,@itemcode=b.ItemCode,@Dscription=b.Dscription,@Quantity=b.Quantity,@ManBtchNum=c.ManBtchNum,@SalPackUn=c.SalPackUn,@ExpDate=d.ExpDate 

兩點這裏:

  1. 不知道爲什麼你正在使用循環 - 嘗試設置基礎的方法。你可以解決大部分的問題,基於集合的查詢
  2. 您可以使用select *爲#TEMP2創建臨時表...檢查是否有語法在MSDN
0

說實話你的SP是一個大搞砸達代碼:)

我建議你重寫它是這樣的:

ALTER PROCEDURE spMRI_TAG_try 
    @DocNum int 
AS 
--Drop table if it exists 
IF OBJECT_ID(N'#temp2') IS NOT NULL DROP TABLE #temp2 
--create table 
CREATE TABLE #temp2 (
    cardname nvarchar(100), 
    Docdate datetime, 
    itemCode nvarchar(50), 
    Dscription nvarchar(100), 
    Quantity numeric(19,6), 
    ManBtchNum char(1), 
    SalPackUn numeric(19,6), 
    ExpDate datetime 
) 
--Make the insertion 
INSERT INTO #temp2 
SELECT a.CardName, 
     a.DocDate, 
     b.ItemCode, 
     b.Dscription, 
     b.Quantity, 
     c.ManBtchNum, 
     c.SalPackUn, 
     d.ExpDate 
FROM OPDN a 
INNER JOIN PDN1 b 
    ON a.DocEntry = b.DocEntry 
INNER JOIN OITM c 
    ON c.ItemCode = b.ItemCode 
INNER JOIN OBTN d 
    ON c.ItemCode = d.ItemCode and [email protected] and d.ExpDate is not null 
--output 
SELECT * 
FROM #temp2 

不需要while循環(我甚至無法理解你將如何通過行切換,在您的解決方案,將寫同一行'@count'次),你可以寫所有的數據可怕在臨時表中。

由於您試圖插入時沒有#temp2表,您會收到錯誤,如果表已存在,也不會檢查。

正如在回答通過卡納安Kandasamy指出,一個更加辦法是使用SELECT * INTO #temp2,可以實現這種方式:

ALTER PROCEDURE spMRI_TAG_try 
    @DocNum int 
AS 

IF OBJECT_ID(N'#temp2') IS NOT NULL DROP TABLE #temp2 

SELECT a.CardName, 
     a.DocDate, 
     b.ItemCode, 
     b.Dscription, 
     b.Quantity, 
     c.ManBtchNum, 
     c.SalPackUn, 
     d.ExpDate 
INTO #temp2 
FROM OPDN a 
INNER JOIN PDN1 b 
    ON a.DocEntry = b.DocEntry 
INNER JOIN OITM c 
    ON c.ItemCode = b.ItemCode 
INNER JOIN OBTN d 
    ON c.ItemCode = d.ItemCode and [email protected] and d.ExpDate is not null 

SELECT * 
FROM #temp2 
相關問題