存储过程不接受任何值

时间:2016-01-06 09:13:20

标签: sql-server stored-procedures

我已经基于一组进程编写了一个存储过程,我只是将一个参数作为输入传递给过程,但它似乎没有取值。但是当我在程序中给出输入值而不是参数时,它正在工作。

流程中没有错误,但在过程语法结束时似乎缺少了一些东西。

下面的

是我使用的存储过程。

ALTER PROCEDURE [TransferIn]
    @ponumber NVARCHAR = NULL
AS
BEGIN
    --step  1  Delete Temp Pur_ID 
    IF EXISTS (
            SELECT TABLE_NAME
            FROM INFORMATION_SCHEMA.TABLES
            WHERE TABLE_NAME = 'Pur_ID_IN')
        DROP TABLE Pur_ID_IN;

    -- =============================================
    --step 2 select PO Number  
    --IF @ponumber IS NOT NULL  
    SELECT 
        ponumber, id
    INTO Pur_ID_IN
    FROM purchaseorder
    WHERE potype IN (2, 4)
      AND status = 0
      AND ponumber = @ponumber;

    -- =============================================
    --step 3 
    --delete Temp. Tabel P_Test20_12_IN 
    IF EXISTS (
            SELECT TABLE_NAME
            FROM INFORMATION_SCHEMA.TABLES
            WHERE TABLE_NAME = 'P_Test20_12_IN')
        DROP TABLE P_Test20_12_IN;

    -- =============================================
    -- step 4 (Insert Data For Invoice To Temp Tabel After Group  )
    SELECT 
        ItemDescription, PurchaseOrderID,
        SUM(QuantityOrdered) AS QuantityOrdered,
        itemid, Price
    INTO P_Test20_12_IN
    FROM PurchaseOrderEntry
    WHERE PurchaseOrderID IN (SELECT id FROM Pur_ID_IN)
    GROUP BY 
        ItemDescription, StoreID, PurchaseOrderID,
        itemid, Price;
    --order by 3

    -- =============================================
    -- step 5 Delete Record From  PurchaseOrderEntry 
    DELETE PurchaseOrderEntry
    FROM PurchaseOrderEntry
    WHERE PurchaseOrderID IN (SELECT id FROM Pur_ID_IN);
    -- =============================================

    INSERT INTO [W07].[dbo].[PurchaseOrderEntry] ([ItemDescription], [LastUpdated], [PurchaseOrderID], [QuantityOrdered], [ItemID], [Price])
        SELECT 
            [ItemDescription],
            GETDATE() AS [LastUpdated],
            [PurchaseOrderID], [QuantityOrdered],
            [ItemID], [Price]
        FROM 
            P_Test20_12_IN;
END

2 个答案:

答案 0 :(得分:2)

问题是

@ponumber nvarchar = null

将其更改为

@ponumber nvarchar(max) = null
  

注意:如果不指定大小(char,nchar,varchar,nvarchar),   sql server默认为1个字符。

答案 1 :(得分:0)

ALTER PROCEDURE [TransferIn]
(
    @ponumber NVARCHAR(100)
)
AS BEGIN

    SET NOCOUNT ON

    IF OBJECT_ID('tempdb.dbo.#temp') IS NOT NULL
        DROP TABLE #temp

    CREATE TABLE #temp (id INT PRIMARY KEY)

    INSERT INTO #temp (id)
    SELECT /*DISTINCT*/ id
    FROM dbo.purchaseorder
    WHERE potype IN (2, 4)
        AND [status] = 0
        AND ponumber = @ponumber

    IF OBJECT_ID('tempdb.dbo.#temp2') IS NOT NULL
        DROP TABLE #temp2

    SELECT ItemDescription,
           PurchaseOrderID,
           SUM(QuantityOrdered) AS QuantityOrdered,
           itemid,
           Price
    INTO #temp2
    FROM PurchaseOrderEntry
    WHERE PurchaseOrderID IN (SELECT * FROM #temp)
    GROUP BY ItemDescription,
             StoreID, --?
             PurchaseOrderID,
             itemid,
             Price;

    DELETE PurchaseOrderEntry
    FROM PurchaseOrderEntry
    WHERE PurchaseOrderID IN (SELECT * FROM #temp)

    INSERT INTO [W07].[dbo].[PurchaseOrderEntry] ([ItemDescription], [LastUpdated], [PurchaseOrderID], [QuantityOrdered], [ItemID], [Price])
    SELECT [ItemDescription],
           GETDATE() AS [LastUpdated],
           [PurchaseOrderID],
           [QuantityOrdered],
           [ItemID],
           [Price]
    FROM #temp2

END