簡體   English   中英

DataSource存儲過程異常行為

[英]DataSource stored procedure strange behaviour

我有一個名為ItemGetCategories的存儲過程和一個SqlDataSource

<asp:SqlDataSource ID="CategoriesDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
     SelectCommand="ItemGetCategories" SelectCommandType="StoredProcedure" OnSelected="CategoriesDataSource_Selected">
       <SelectParameters>
          <asp:RouteParameter Name="StockItemId" RouteKey="id" Type="String" />                                            
        </SelectParameters>
</asp:SqlDataSource>

SQL Server正在從數據源接收此查詢

exec ItemGetCategories @StockItemId=15

並且沒有任何數據返回到數據源,在SQL Server Management Studio中執行此查詢也不會返回任何行。

現在,從SQL Server Management Studio執行此操作的奇怪部分返回1行。

USE [MyDB]
GO

DECLARE @return_value int

EXEC    @return_value = [dbo].[ItemGetCategories]
        @StockItemId = N'15'

SELECT  'Return Value' = @return_value
GO

這是存儲過程

ALTER PROCEDURE [dbo].[ItemGetCategories]
    @StockItemId nvarchar(10) = '1'
AS
BEGIN
    SET NOCOUNT ON;

    WITH categoryPath(Id,Slug,CategoryName)
    AS
    (
        SELECT 
           Id, Slug, CategoryName 
        FROM 
           Categories
        WHERE 
           ParentCategoryId IS NULL

        UNION ALL

        SELECT 
            Categories.Id,
            CAST(categoryPath.Slug + '/' + categories.Slug AS NVARCHAR(150)),
            Categories.CategoryName
        FROM 
            Categories
        JOIN 
            categoryPath ON Categories.ParentCategoryId = categoryPath.Id
     )
     SELECT * 
     FROM [ItemCategories]
     JOIN categoryPath ON [ItemCategories].StockId = categoryPath.Id 
     WHERE [ItemCategories].StockId = @StockItemId
END

為什么存儲過程不向數據源返回任何行?

我試圖將存儲過程參數從int更改為string,並將數據源中的參數作為string傳遞,但結果仍然相同。

除了數據類型不匹配外,您的過程代碼沒有什么錯。 對以下幾項進行更改。

將過程參數@StockItemId更改為INT因為[ItemCategories].StockIdINT類型,並且您正在基於它進行過濾,其中WHERE [ItemCategories].StockId=@StockItemId

ALTER PROCEDURE [dbo].[ItemGetCategories]
    @StockItemId INT = NULL 

相應地定義數據源屬性,例如

<SelectParameters>
<asp:RouteParameter Name="StockItemId" RouteKey="id" Type="INT32" />                                            
</SelectParameters>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM