我的存储过程是
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Customer_Registrations]
@type varchar(50) = null,
@Customer_ID int = null,
@Customer_Name varchar(50) = null,
@Email varchar(50) = null,
@Password varchar(50) = null,
@Mobile varchar(50) = null
AS
BEGIN
SET NOCOUNT ON;
IF @type = 'select'
BEGIN
SELECT *
FROM ssshub..Customer_Master
END
IF @type = 'specific'
BEGIN
SELECT *
FROM ssshub..Customer_Master
WHERE Customer_ID = @Customer_ID
END
IF @type = 'insert'
BEGIN
INSERT INTO [dbo].[Customer_Master]([Customer_Name], [Email],[Password], [Mobile], [SDate])
VALUES ('@Customer_Name', '@Email', '@Password', '@Mobile', CURRENT_TIMESTAMP)
END
END
我的桌子
这很好用
这个查询工作正常但是当我尝试使用存储过程插入它时会抛出错误。
我对identity
有customer_id
规范,因此无需插入
INSERT INTO [dbo].[Customer_Master] ([Customer_Name], [Email], [Password], [Mobile], [SDate])
VALUES('ewcewc', 'dewdw@dwc.com', 'dewdwd', '9999999999', CURRENT_TIMESTAMP)
现在,当我尝试使用以下语句执行我的存储过程时
exec customer_registrations 'insert', 'dsad', 'test@test.com', 'pass', '9999900000'
我明白了:
将数据类型varchar转换为int
时出错
答案 0 :(得分:3)
使用参数排列参数:
services.AddMvc().AddRazorOptions(options =>
{
options.ViewLocationExpanders.Add(new BetterViewEngine());
});
...您可以看到'insert' -> @type varchar(50)
'dsad' -> @Customer_ID int
'test@test.com' -> @Customer_Name varchar(50)
'pass' -> @Email varchar(50)
'9999900000' -> @Password varchar(50)
-> @Mobile varchar(50) = null
无法被理解为'dsad'
。也许在其他地方也存在问题,但至少,存储过程没有被正确调用。
<强>更新强>:
如果您的意图是省略某些与某些情况无关的某些参数,则必须使用命名参数;否则,参数只能以参数出现的相同顺序应用:
int
答案 1 :(得分:0)
在表格中插入数据时,请在程序中传递空值。在表中插入数据时运行以下过程。你应该管理所有程序参数。您未通过Customer_ID
的值,因此发生错误。
EXEC [dbo].[Customer_Registrations] 'insert',null,'ewcewc', 'dewdw@dwc.com', 'dewdwd', '9999999999'
答案 2 :(得分:0)
Create Database Test
Create Table Employee
(
EmpId int identity(1,1) not null primary Key,
EmpName varchar(50),
Address varchar(50),
MobileNo int
)
Select * From [dbo].[Employee]
Alter Table [dbo].[Employee] Alter Column MobileNo Bigint
Insert into [dbo].[Employee] values('Harsh Kumar','Banglore','9748342075')
Create Proc MyProc
As
Select * From [dbo].[Employee]
EXEC MyProc
Create Proc SP_GetEmp
@EmpId int,
@EmpName varchar(50),
@Address varchar(50),
@MobileNo int
As
Insert into [dbo].[Employee](EmpId, EmpName, Address, MobileNo) Values(@EmpId, @EmpName, @Address, @MobileNo)
EXEC SP_GetEmp 'Rahul Kr','Pune', 8250707544
EXEC MyProc
Error:- Error converting data type varchar to int.