2011-03-24 77 views
1

我有一个过程,我将在下面写。现在我想添加两个选择。两个选择和两个插入一个SQL Server存储过程

首先,SELECT ID FROM Category Where CategoryName = ''将其保存在变量@CategoryID中。

第二选择语句:SELECT ID FROM Type WHERE TypeName = ''并将其保存在@TypeID中。

现在我想添加第二个插入:

INSERT INTO Inventory(ProductID, InputQuantity, Margin, InputDateTime, ExpDate) 
VALUES(@ProductID, @Quantity,@Margin, @InputDateTime, @ExpDate) 
where ProductID = Scope_Identity() 

谢谢:)

这是我的程序:

USE [AcidDB] 
GO 
/****** Object: StoredProcedure [dbo].[InsertProducts] Script Date: 03/24/2011 15:29:30 ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
ALTER PROC [dbo].[InsertProducts] 

@CategoryID int, 
@TypeID int, 
@BarCode NvarChar(MAX), 
@ArtNumber NvarChar(MAX), 
@ProductName NvarChar(MAX), 
@Price Decimal(18, 2), 
@SelfPrice Decimal(18, 2), 
@PriceWithOutAWD Decimal(18, 2), 
@UnitsInStock int, 
@Comment NvarChar(MAX) 

AS 

INSERT INTO Products(CategoryID, TypeID, BarCode, ArtNumber, ProductName, Price, SelfPrice, PriceWithOutAWD, UnitsInStock, Comment) 
VALUES(@CategoryID, @TypeID, @BarCode, @ArtNumber, @ProductName, @Price, @SelfPrice, @PriceWithOutAWD, @UnitsInStock, @Comment) 

Exec InsertProducts '','','','','','','','','','' 
Select SCOPE_IDENTITY() 
+2

你的问题是什么? – 2011-03-24 13:11:53

+0

我怎样才能得到这个选择并在这个过程中插入statmant? – 2011-03-24 13:14:44

+1

我认为你需要在你的脚本中使用“GO”。目前它会自动递归调用。 – 2011-03-24 13:19:14

回答

1

假设你的修订程序将传递一个类别名称和typename,那么我认为以下是你想要的。无需从类别和类型表中单独选择 - 只需将其作为第一个插入查询的一部分即可。

ALTER PROC [dbo].[InsertProducts] 

@CategoryName nvarchar(max), 
@TypeName nvarchar(max), 
@BarCode NvarChar(MAX), 
@ArtNumber NvarChar(MAX), 
@ProductName NvarChar(MAX), 
@Price Decimal(18, 2), 
@SelfPrice Decimal(18, 2), 
@PriceWithOutAWD Decimal(18, 2), 
@UnitsInStock int, 
@Comment NvarChar(MAX), 
@Quantity int, 
@Margin decimal(14,2), 
@InputDateTime datetime, 
@ExpDate datetime 
AS 
declare @ProductID int 

INSERT INTO Products(CategoryID, TypeID, BarCode, ArtNumber, ProductName, Price, SelfPrice, PriceWithOutAWD, UnitsInStock, Comment) 
select c.CategoryID, t.TypeID, @BarCode, @ArtNumber, @ProductName, @Price, @SelfPrice, @PriceWithOutAWD, @UnitsInStock, @Comment 
from Category c cross join Type t 
where c.CategoryName = @CategoryName and 
t.TypeName = @TypeName 

set @ProductID = SCOPE_IDENTITY() 

INSERT INTO Inventory(ProductID, InputQuantity, Margin, InputDateTime, ExpDate) 
VALUES(@ProductID, @Quantity,@Margin, @InputDateTime, @ExpDate) 

顺便说一句 - 你真的希望有人把罗密欧和朱丽叶的整个脚本作为产品名吗? nvarchar(max)有它的位置,但它不应该被盲目地用来避免考虑你想要在数据库中明确允许的内容。

3

你不能对一个插入一个,其中:

INSERT INTO Inventory(ProductID, InputQuantity, Margin, InputDateTime, ExpDate) 
VALUES(@ProductID, @Quantity,@Margin, @InputDateTime, @ExpDate) 
where ProductID = Scope_Identity() 

y OU中的数据插入到产品表,把新的产品编号为变量:

@ProductId = Scope_Identity() 

然后,在插入使用这个变量:

INSERT INTO Inventory(ProductID, InputQuantity, Margin, InputDateTime, ExpDate) 
VALUES(@ProductID, @Quantity,@Margin, @InputDateTime, @ExpDate) 
+0

没有...我想要不同的命令:1step:获取typeID和categoryID 2step插入到产品3step获取这个identtity 4step插入到清单中这个标识 – 2011-03-24 14:12:17

+0

@Acid - 我不明白你的意思是不同的命令 – Martin 2011-03-25 13:13:59

0

@Damien_The_Unbeliever有正确的想法,但我建议你使用try catch块和事务。当你有多个事务需要作为一个组一起工作时,它们应该包含在一个显式事务中,如果两个事务都失败了,就会回滚。否则,你将会遇到数据完整性问题。你不会希望它进入产品,然后在库存中失败。请参阅联机丛书以了解如何执行此操作。

相关问题