Sql Server使用存储过程插入查找表

时间:2014-01-02 23:12:29

标签: asp.net sql-server tsql stored-procedures

我是SQL Server的新手,我想问一些关于插入和更新数据的问题。 我想为我的Select - Insert - Delete - Update创建存储过程。

我正在为我的应用程序使用ASP.NET。

首先让我给你我的数据库架构。我在SQL Server 2008中工作

我有4张桌子 -

tbl_Equipment   |  tbl_Location  |  tbl_Condition  | tbl_Category

tbl_Equipment

  • equip_ID int(PK - 身份)
  • 产品 nvarchar(50)
  • AquiredDate datetime
  • SerialNumber nvarchar(50)
  • PurchasedPlace nvarchar(50)
  • 制造商 nvarchar(50)
  • 型号 nvarchar(50)
  • 评论 nvarchar(500)
  • categ_ID int(FK - tbl_Category)
  • cond_ID * int(FK -tbl_Condition)*
  • loc_ID * int(FK - tbl_Location)*

tbl_Category

  • categ_ID int(PK - 识别)
  • 类别 nvarchar(50)

tbl_Condition

  • cond_ID int(PK - 识别)
  • 条件 nvarchar(50)

tbl_Location

  • loc_ID int(PK - 识别)
  • 位置 nvarchar(50)

ConditionLocationCategory是查找表。

我想在表Equipment中插入值,并从查找表中的下拉列表中进行选择并更新我的数据。

我尝试过的一种小方法:

我的选择

 SELECT  
    tbl_Equipment.Product,      
    tbl_Equipment.AquiredDate, 
tbl_Equipment.SerialNumber,          
    tbl_Equipment.PurchasedPlace, 
tbl_Equipment.Manufacturer,         
    tbl_Equipment.Model, 
tbl_Equipment.Comments,         
    tbl_Condition.Condition,
tbl_Category.Category,          
    tbl_Location.Location
FROM    
    tbl_Category 
INNER JOIN 
    tbl_Equipment ON tbl_Category.categ_ID = tbl_Equipment.categ_ID 
INNER JOIN 
    tbl_Condition ON tbl_Equipment.cond_ID = tbl_Condition.cond_ID 
INNER JOIN 
    tbl_Location ON tbl_Equipment.loc_ID = tbl_Location.loc_ID>

我的插入

CREATE PROCEDURE insertEquipTable
    @Product nvarchar(50),
    @AquiredDate datetime,
    @SerialNumber nvarchar(20),
    @PurchasedPlace nvarchar(50),
    @Insured nvarchar(10),
    @Manufacturer nvarchar(50),
    @Model nvarchar(50),
    @Comments nvarchar(50),
    @categ_ID int,
    @cond_ID int,
    @loc_ID int
AS
BEGIN
    SET NOCOUNT ON;

    SELECT
       tbl_Equipment.Product,
       tbl_Equipment.AquiredDate,
       tbl_Equipment.SerialNumber,  
       tbl_Equipment.PurchasedPlace,
       tbl_Equipment.Insured,
       tbl_Equipment.Manufacturer,
       tbl_Equipment.Model, 
       tbl_Equipment.Comments,
       tbl_Category.Category,
       tbl_Condition.Condition,
       tbl_Location.Location, 
       tbl_Equipment.categ_ID,
       tbl_Equipment.cond_ID,
       tbl_Equipment.loc_ID
    FROM        
       tbl_Category 
    INNER JOIN
       tbl_Equipment ON tbl_Category.categ_ID = tbl_Equipment.categ_ID 
    INNER JOIN
       tbl_Condition ON tbl_Equipment.cond_ID = tbl_Condition.cond_ID 
    INNER JOIN
       tbl_Location ON tbl_Equipment.loc_ID = tbl_Location.loc_ID

    INSERT INTO tbl_Equipment(Product, AquiredDate, SerialNumber, PurchasedPlace, Insured, 
                              Manufacturer, Model, Comments, categ_ID, cond_ID, loc_ID)
    VALUES(@Product, @AquiredDate, @SerialNumber, @PurchasedPlace, @Insured, 
           @Manufacturer, @Model, @Comments, @categ_ID, @cond_ID, @loc_ID)
END

任何帮助将不胜感激。或者如果我的问题还有另一种方法,比如枚举等。

感谢您的时间,对不起我的错误代码类型感到抱歉。

0 个答案:

没有答案