| USE AdventureWorksLT; GO --Create Schema CREATE SCHEMA Production GO --Create Table in which the values --will be inserted CREATE TABLE [Production].[BillOfMaterials]( [BomID] [int] NOT NULL, [ProductID] [int] NOT NULL, [StandardCost] [money] NOT NULL, CONSTRAINT [PK__BillOfMaterials__7E37BEF6] PRIMARY KEY CLUSTERED ( [BomID] ASC, [ProductID] ASC ) ON [PRIMARY] ) ON [PRIMARY] GO --Adding Constraints to the Table ALTER TABLE [Production].[BillOfMaterials] WITH CHECK ADD CONSTRAINT [FK__BillOfMat__Produ__7F2BE32F] FOREIGN KEY([ProductID]) REFERENCES [SalesLT].[Product] ([ProductID]) GO --Adding Constraints to the Table ALTER TABLE [Production].[BillOfMaterials] CHECK CONSTRAINT [FK__BillOfMat__Produ__7F2BE32F] GO --Create a table type. CREATE TYPE BomType AS TABLE ( BomID INT NOT NULL, ProductID INT NOT NULL, StandardCost INT NOT NULL ) GO CREATE PROCEDURE spInsertBOM @Bom BomType READONLY AS SET NOCOUNT ON INSERT INTO Production.BillOfMaterials ( BomID, ProductID, StandardCost ) SELECT * FROM @Bom; GO DECLARE @Bom AS BomType; /* Add data to the table variable. */ INSERT INTO @Bom (BomID, ProductID, StandardCost) SELECT 1, ProductID, StandardCost FROM SalesLT.Product WHERE [Color] = 'Silver' AND [Name] LIKE 'Mountain%' /* Pass the table variable data to a stored procedure. */ EXEC spInsertBOM @Bom; |