简单的插入存储过程不起作用

时间:2019-02-26 20:56:10

标签: sql-server tsql

我正在尝试制作一个简单的存储过程,但是它不起作用。当我使用其中的参数执行该过程时,以下内容如下:

ALTER proc [dbo].[sp_NewProduct] 
@ProductName nvarchar(50), 
@ProductNumber nvarchar(25), 
@MakeFlag bit, 
@FinishedGoodsFlag bit, 
@Color nvarchar(15), 
@SafetyStockLevel smallint, 
@ReorderPoint smallint, 
@StandardCost money, 
@ListPrice money, 
@DaysToManufacture int, 
@SellStartDate date, 
@rowguid uniqueidentifier, 
@ModifiedDate datetime 

as 

insert dbo.product 
(Name,
ProductNumber,
MakeFlag,
FinishedGoodsFlag,
Color,
SafetyStockLevel,
ReorderPoint,
StandardCost,
ListPrice,
DaysToManufacture,
SellStartDate,
rowguid,
ModifiedDate) 

values 

(@ProductName,
@ProductNumber,
@MakeFlag,
@FinishedGoodsFlag,
@Color, @SafetyStockLevel,
@ReorderPoint,
@StandardCost,
@ListPrice,
@DaysToManufacture,
@SellStartDate,
@rowguid,
@ModifiedDate)

这是带有每列值的执行查询:

exec sp_NewProduct 'AR-5516','105',0,1,'Red',5,5,0.00,0.00,5,'2018-05-01',newid(),getdate()

1 个答案:

答案 0 :(得分:0)

似乎您正在处理错误的过程语法!使用以下代码段:

Create or Alter proc [dbo].[sp_NewProduct] (@ProductName nvarchar(50), @ProductNumber nvarchar(25), @MakeFlag bit, @FinishedGoodsFlag bit, @Color nvarchar(15), @SafetyStockLevel smallint, @ReorderPoint smallint, @StandardCost money, @ListPrice money, @DaysToManufacture int, @SellStartDate date, @rowguid uniqueidentifier, @ModifiedDate datetime)
    As 
    Begin
            Insert dbo.product (Name,ProductNumber,MakeFlag,FinishedGoodsFlag,Color,SafetyStockLevel,ReorderPoint,StandardCost,ListPrice, DaysToManufacture,SellStartDate,rowguid,ModifiedDate) values (@ProductName,@ProductNumber,@MakeFlag,@FinishedGoodsFlag,@Color, @SafetyStockLevel,@ReorderPoint,@StandardCost,@ListPrice,@DaysToManufacture,@SellStartDate,@rowguid,@ModifiedDate)
    End
    Go

 Exec sp_NewProduct 'AR-5516','105',0,1,'Red',5,5,0.00,0.00,5,'2018-05-01',@Id,@DateTime;

如果仍然遇到错误,请将newid()的值和GetDate()的值存储在临时变量中,并通过该变量进行调用!

  Declare @Id AS UniqueIdentifier = NewId()
  Declare @DateTime as DateTime = GetDate()
  Exec sp_NewProduct 'AR-5516','105',0,1,'Red',5,5,0.00,0.00,5,'2018-05-01',@Id,@DateTime;