一个SQL Server存储过程中有两个Select和两个Insert
我有一个程序,我将写在下面。现在我想添加两个选项 首先,一个SQL Server存储过程中有两个Select和两个Insert,sql,sql-server,stored-procedures,Sql,Sql Server,Stored Procedures,我有一个程序,我将写在下面。现在我想添加两个选项 首先,从CategoryName=''的类别中选择ID将其保存在变量@CategoryID中 第二个select语句:从类型中选择ID,其中TypeName='并将其保存在@TypeID中 现在我想添加第二个插入: INSERT INTO Inventory(ProductID, InputQuantity, Margin, InputDateTime, ExpDate) VALUES(@ProductID, @Quantity,@Margin
从CategoryName=''的类别中选择ID
将其保存在变量@CategoryID
中
第二个select语句:从类型中选择ID,其中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()
插入项上不能有where:
INSERT INTO Inventory(ProductID, InputQuantity, Margin, InputDateTime, ExpDate)
VALUES(@ProductID, @Quantity,@Margin, @InputDateTime, @ExpDate)
where ProductID = Scope_Identity()
将数据插入Product表后,将新ProductId放入变量:
@ProductId = Scope_Identity()
然后,在插入中使用此变量:
INSERT INTO Inventory(ProductID, InputQuantity, Margin, InputDateTime, ExpDate)
VALUES(@ProductID, @Quantity,@Margin, @InputDateTime, @ExpDate)
插入项上不能有where:
INSERT INTO Inventory(ProductID, InputQuantity, Margin, InputDateTime, ExpDate)
VALUES(@ProductID, @Quantity,@Margin, @InputDateTime, @ExpDate)
where ProductID = Scope_Identity()
将数据插入Product表后,将新ProductId放入变量:
@ProductId = Scope_Identity()
然后,在插入中使用此变量:
INSERT INTO Inventory(ProductID, InputQuantity, Margin, InputDateTime, ExpDate)
VALUES(@ProductID, @Quantity,@Margin, @InputDateTime, @ExpDate)
假设您修改后的过程将被传递一个categoryname和typename,那么我认为下面是您想要的。不需要从Category和Type表中进行单独的选择,只需将其作为第一个insert查询的一部分即可
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)
有它的位置,但它不应该被盲目地用来避免考虑您明智地希望在数据库中允许什么。假设您修改的过程将被传递一个categoryname和typename,那么我想下面就是您想要的。不需要从Category和Type表中进行单独的选择,只需将其作为第一个insert查询的一部分即可
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)
有它的位置,但不应该盲目地使用它来避免思考您明智地希望在数据库中允许哪些内容。@Damien\u不相信它的人有正确的想法,但我建议您也使用try-catch块和事务。当您有多个事务需要作为一个组一起工作时,它们应该包含在一个显式事务中,如果其中一个事务失败,则回滚。否则,您将遇到数据完整性问题。你不会希望这些东西进入产品,然后在库存中失败。有关如何执行此操作,请参阅联机丛书 @Damien\u不信者的想法是正确的,但我建议您也使用try-catch块和事务。当您有多个事务需要作为一个组一起工作时,它们应该包含在一个显式事务中,如果其中一个事务失败,则回滚。否则,您将遇到数据完整性问题。你不会希望这些东西进入产品,然后在库存中失败。有关如何执行此操作,请参阅联机丛书 如何在这个过程中获得这个select和insert statmant?我想您的脚本中需要一个GO
。当前它将递归地调用自己。我如何在这个过程中获得这个select和insert statmant?我想你的脚本中需要一个GO
。目前它将递归调用自己。不。。。我需要不同的命令:1步骤:获取typeID和categoryID 2步骤插入产品3步骤获取此标识4步骤插入此库存identity@Acid-我不明白你说的不同命令是什么意思。。。我需要不同的命令:1步骤:获取typeID和categoryID 2步骤插入产品3步骤获取此标识4步骤插入此库存identity@Acid-我不明白你说的不同命令是什么意思