对于sql-server如何将SKU行插入表SKU_DATA,然后将SKU插入INVENTORY表中,所有分支和Quantity on the hand = 2,Quantityonhand = 0。
我需要在SKUDATA中插入一个SKU项目,并在库存表中对应所有现有分支Quantityonhand = 2和Quantityonhand = 0。
请帮助谢谢
BRANCH
name varchar (30) not NULL,
managerNum INT NOT NULL
SKU_DATA
SKU Int NOT NULL IDENTITY (1000,1),
description varchar (40) NOT NULL UNIQUE,
department varchar(30) NOT NULL default 'Home Entertainment',
sellingPrice numeric (7,2) NOT NULL
广告
SKU Int NOT NULL,
branch varchar (30) NOT NULL ,
quantityOnHand Int NOT NUll ,
quantityOnOrder Int NOT NUll
触发
CREATE TRIGGER NewSku
ON dbo.SKU_DATA
After INSERT
as
begin
SET NOCOUNT ON;
declare
@description varchar (40),
@department varchar(30),
@sellingPrice numeric (7,2)
DECLARE @Output TABLE (SKU INT)
INSERT INTO dbo.SKU_DATA ([DESCRIPTION], DEPARTMENT, SELLINGPRICE)
OUTPUT INSERTED.SKU INTO @Output(SKU)--getting Inserted ID from SKU_DATA to @Output table
VALUES (@description, @department, @sellingPrice);
INSERT INTO INVENTORY (SKU ,branch ,quantityOnHand ,quantityOnOrder )
--Loading SKU information against all branches to INVENTORY
SELECT t.SKU,b.name,2, 0
FROM Branch b
CROSS APPLY (SELECT SKU FROM @Output)t
END
NewSku,第17行 无法将值NULL插入列'description',表'tt.dbo.SKU_DATA';列不允许空值。 INSERT失败。
答案 0 :(得分:2)
在sql中,您可以从INSERTED
表中获取插入的行详细信息。
CREATE PROCEDURE InsertNewSkuWithInventory
@description varchar (40),
@department varchar(30),
@sellingPrice numeric (7,2)
AS
BEGIN
DECLARE @Output TABLE (SKU INT)
INSERT INTO dbo.SKU_DATA ([DESCRIPTION], DEPARTMENT, SELLINGPRICE)
OUTPUT INSERTED.SKU INTO @Output(SKU)--getting Inserted ID from SKU_DATA to @Output table
VALUES (@description, @department, @sellingPrice);
INSERT INTO INVENTORY (SKU ,branch ,quantityOnHand ,quantityOnOrder )
--Loading SKU information against all branches to INVENTORY
SELECT t.SKU,b.name,2, 0
FROM Branch b
CROSS APPLY (SELECT SKU FROM @Output)t
END
如果您想编写触发器,请从上述过程中删除库存插入部件并编写如下触发器。 对于SKU_Data表中的每个插入,此触发器将被触发并将数据插入Inventory表。
程序:
CREATE PROCEDURE InsertNewSkuWithInventory
@description varchar (40),
@department varchar(30),
@sellingPrice numeric (7,2)
AS
BEGIN
INSERT INTO dbo.SKU_DATA ([DESCRIPTION], DEPARTMENT, SELLINGPRICE)
VALUES (@description, @department, @sellingPrice);
END
触发:
CREATE TRIGGER UpdateInventory
ON dbo.SKU_DATA
AFTER INSERT
AS
INSERT INTO INVENTORY (SKU ,branch ,quantityOnHand ,quantityOnOrder )
--Loading SKU information against all branches to INVENTORY
SELECT t.SKU,b.name,2, 0
FROM Branch b
CROSS APPLY (SELECT SKU FROM INSERTED)t
答案 1 :(得分:1)
您不需要光标。只需将所有分支插入#BranchList,其余部分如下。
Create procedure InsertNewSkuWithInventory
@description varchar (40),
@department varchar(30),
@sellingPrice numeric (7,2),
AS
Declare @SKU as int
CREATE TABLE #BranchList
(
branch varchar(30)
)
BEGIN
INSERT INTO dbo.SKU_DATA (description, department, sellingPrice)
VALUES (@description, @department, @sellingPrice);
Select @SKU =SKU
From dbo.SKU_DATA
Where description = @description
And department = @department
And sellingPrice = @sellingPrice;
INSERT INTO #BranchList
-- Your code to fetch all branches
INSERT INTO INVENTORY
SELECT @SKU, branch, 2, 0
FROM #BranchList
END