Introduction
Writing an insert stored procedure in SQL for a giant table is a hard job. You have to hardcode all the tedious code, you should declare input parameters for your SP and then select all those columns in INSERT VALUES
command and pass in all those parameters again. This stored procedure can be used for creating those kind of stored procedures. It can do all the hard work for you.
Background
Intermediate knowledge of T-SQL would be enough.
Using the Code
After creating the CoolInsertGenerator
stored procedure (link on the stored procedure is available at the end of the tip), you can execute it for any table in your database. For example, you can execute it for Production.Product
in AdventureWorks
database as follows:
EXEC USP_CoolInsertGenerator 'Production' , 'Product' , @ShowOutput = 0
And it will generate the following stored procedure for you:
USE [AdventureWorks2012]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER PROC [dbo].[USP_Product_Insert]
@Name nvarchar(50)
,@ProductNumber nvarchar(25)
,@MakeFlag bit
,@FinishedGoodsFlag bit
,@Color nvarchar(15)
,@SafetyStockLevel smallint
,@ReorderPoint smallint
,@StandardCost money
,@ListPrice money
,@Size nvarchar(5)
,@SizeUnitMeasureCode nchar(3)
,@WeightUnitMeasureCode nchar(3)
,@Weight decimal
,@DaysToManufacture int
,@ProductLine nchar(2)
,@Class nchar(2)
,@Style nchar(2)
,@ProductSubcategoryID int
,@ProductModelID int
,@SellStartDate datetime
,@SellEndDate datetime
,@DiscontinuedDate datetime
,@ModifiedDate datetime
AS BEGIN
INSERT Production.Product(
[Name]
,[ProductNumber]
,[MakeFlag]
,[FinishedGoodsFlag]
,[Color]
,[SafetyStockLevel]
,[ReorderPoint]
,[StandardCost]
,[ListPrice]
,[Size]
,[SizeUnitMeasureCode]
,[WeightUnitMeasureCode]
,[Weight]
,[DaysToManufacture]
,[ProductLine]
,[Class]
,[Style]
,[ProductSubcategoryID]
,[ProductModelID]
,[SellStartDate]
,[SellEndDate]
,[DiscontinuedDate]
,[ModifiedDate]
)VALUES(
@Name
,@ProductNumber
,@MakeFlag
,@FinishedGoodsFlag
,@Color
,@SafetyStockLevel
,@ReorderPoint
,@StandardCost
,@ListPrice
,@Size
,@SizeUnitMeasureCode
,@WeightUnitMeasureCode
,@Weight
,@DaysToManufacture
,@ProductLine
,@Class
,@Style
,@ProductSubcategoryID
,@ProductModelID
,@SellStartDate
,@SellEndDate
,@DiscontinuedDate
,@ModifiedDate
) END
Points of Interest
Feel free to investigate and extend :). More information about the instructions is available on github.