【问题标题】:Creating a complex stored-procedure to get multi-tables output创建复杂的存储过程以获取多表输出
【发布时间】:2012-10-04 02:38:24
【问题描述】:

我的产品目录有两个级别的类别和与产品相关的品牌。

类似的表格:

产品:产品ID |类别ID |品牌ID

类别:类别 ID |家长 ID |图片(位)

我需要在浏览品牌页面时选择所有“父类别”,这些“父类别”是与已使用存储过程在该品牌下添加的产品相关联的类别的父类别...并确保获得图像列的值

我用了什么

CREATE PROCEDURE [dbo].[GetBrandProductsCats]
@iLanguageID int,
@iBrandID int
As
Begin
SELECT C.Image, C.CategoryID, C.ParentID,CD.Title, CD.Summary, BrandID = @iBrandID
FROM Categories C
    Join CategoryData CD on C.ParentID = CD.CategoryID
WHERE C.CategoryID in 
    (SELECT Products.CategoryID  
     FROM Products 
     WHERE Products.BrandID = @iBrandID)

End
GO

这确实有效,但图像列值不适用于父类别

有什么想法吗?希望你有一个原因我累了:)

提前谢谢大家

【问题讨论】:

  • 再次,我在具有 BrandID 的产品的 CategoryID 列中选择具有子类别的父类别 ... :( 谢谢

标签: sql-server-2005 stored-procedures sql-server-2000 asp.net-4.0


【解决方案1】:

这是我的代码:

对于 SQL 存储过程

CREATE PROCEDURE [dbo].[GetBrandProductsCats]
@iLanguageID int,
@iBrandID int
As
Begin
select C.CategoryID,C.ParentID, BrandID = @iBrandID, C.Image, CD.Title, CD.Summary from Categories C
Join CategoryData CD on C.CategoryID = CD.CategoryID
Join BrandsCategories BC on BC.CategoryID = C.CategoryID and  BC.BrandID = @iBrandID
Where C.ParentID=0 and C.Status=1 and CD.LanguageID=@iLanguageID
End
GO

代码:

我使用存储中的父 ID 来获取产品猫和它的图像,并为子猫做了一个函数:

<asp:Repeater id="rpCats" runat="server" >
<ItemTemplate>
<a href="categories.aspx?pid=<%# DataBinder.Eval(Container.DataItem, "ParentID") %>&brandid=<%# DataBinder.Eval(Container.DataItem, "BrandID") %>"><%# DataBinder.Eval(Container.DataItem, "Title") %></a>
<%# DataBinder.Eval(Container.DataItem, "Summary") %>
<asp:Repeater id="rpSubCats" runat="server" DataSource='<%# GetSubCats(CInt(DataBinder.Eval(Container.DataItem, "ParentID")))%>'>
<ItemTemplate >
<a href='products.aspx?pid=<%# DataBinder.Eval(Container.DataItem, "ParentID") %>&cid=<%# DataBinder.Eval(Container.DataItem, "CategoryID") %>&brandid=<%# DataBinder.Eval(Container.DataItem, "BrandID") %>'><%# DataBinder.Eval(Container.DataItem, "Title") %></a>
</ItemTemplate>
</asp:Repeater>
<div id="tblPic" runat="server">
<asp:Image id="imgThumb" runat="server" Borderwidth="0" Width="70px"></asp:Image>
</div>
<asp:TextBox id="txtImage" runat="server" Visible="False" Text='<%# DataBinder.Eval(Container.DataItem, "Image") %>'></asp:TextBox>
<asp:TextBox id="txtID" runat="server" Visible="False" Text='<%# DataBinder.Eval(Container.DataItem, "ParentID") %>'></asp:TextBox>
</ItemTemplate>
</asp:Repeater>

这里是 GetSubCats 函数

Public Function GetSubCats(ByVal intParentID As Integer) As DataView
If Request("brandid") <> "" Then
Dim objDB As New ProductsDB
Dim dsSubCats As New DataSet
objDB.daGetBrandProductsSubCats.SelectCommand.Parameters("@iParentID").Value = intParentID
objDB.daGetBrandProductsSubCats.SelectCommand.Parameters("@iLanguageID").Value = System.Configuration.ConfigurationManager.AppSettings("LanguageID")
objDB.daGetBrandProductsSubCats.SelectCommand.Parameters("@iBrandID").Value = Request("brandid")
objDB.daGetBrandProductsSubCats.Fill(dsSubCats)
Dim dvSubCats As DataView = dsSubCats.Tables(0).DefaultView
GetSubCats = dvSubCats
End If
End Function

谢谢大家,希望有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-23
    • 2021-10-04
    • 2018-11-04
    • 2014-04-25
    • 2019-09-06
    • 2023-03-04
    • 2023-02-17
    • 2016-03-23
    相关资源
    最近更新 更多