将数据类型varchar转换为int时出错。

时间:2017-09-02 04:46:04

标签: sql sql-server tsql

我的桌子在下面

CREATE TABLE Customers
(
     CustomerID int identity(1,1) not null primary key,
     Name varchar(50) not null,
     PhoneNumber varchar(20) not null 
         constraint chk_PhoneNumber check(PhoneNumber not like '%[^0-9]%'),
     DoorNo varchar(50) not null,
     StreetName varchar(50) not null,
     City varchar(50) not null,
     Statee varchar(50) not null,
     Zipcode int not null
)

我的存储过程:

ALTER PROCEDURE stp_customers_insert
    (@customerid int, 
     @name varchar(50),
     @phone varchar(50),
     @doorno varchar(50),
     @streetname varchar(50),
     @city varchar(50),
     @state varchar(50),
     @zip int)
AS
BEGIN
    IF EXISTS (SELECT CustomerID FROM Customers WHERE CustomerID = @customerid)
    BEGIN
        RAISERROR ('employee id already exists', 1, 1)
    END
    ELSE
    BEGIN
        INSERT INTO Customers (Name, PhoneNumber, DoorNo, StreetName, City, Statee, Zipcode) 
        VALUES (@name, @phone, @doorno, @streetname, @city, @state, @zip)
    END
END

示例电话:

exec stp_customers_insert 'ram', '674673932', '122', '5th cross', 'trichy', 'tamilnadu', 620001

我收到此错误:

  

Msg 8114,Level 16,State 5,Procedure stp_customers_insert,Line 23
  将数据类型varchar转换为int时出错。

4 个答案:

答案 0 :(得分:1)

问题似乎是您的存储过程需要8个参数:

stp_customers_insert(@customerid int, @name varchar(50), @phone varchar(50),
                     @doorno varchar(50), @streetname varchar(50), @city varchar(50),
                     @state varchar(50), @zip int)

但实际调用proc时只传递7个参数:

exec stp_customers_insert 'ram','674673932','122','5th cross','trichy','tamilnadu',620001

如果您不知道或不想对CustomerID执行重复检查,那么您可以略微修改您的通话,只需通过NULL

exec stp_customers_insert NULL, 'ram','674673932','122','5th cross','trichy','tamilnadu',620001

顺便说一句,如果proc甚至没有插入CustomerID,并且这个字段是自动递增的,那么我没有看到传递它的意义。相反,您可能需要考虑使用唯一约束来实现相同的目标。

答案 1 :(得分:1)

exec stp_customers_insert 1,'ram','674673932','122','5thcross','trichy','tamilnadu',620001

您必须在过程参数中传递@customerid值 - 然后它将无错执行。

答案 2 :(得分:-1)

在您的表格结构CustomerID中定义为INT。但是,因为您的存储过程是以这种格式定义的:

stp_customers_insert(@customerid int,@name ....

您要将ram作为customerid的值发送到

exec stp_customers_insert 'ram','674673932'....

将此更正为:

exec stp_customers_insert '*enter the CustId value here*','ram','674673932'....

*enter the CustId value here*替换为CustomerID

同样改变:

insert into Customers(Name,PhoneNumber,DoorNo,StreetName,City,Statee,Zipcode) values(@name,@phone,@doorno,@streetname,@city,@state,@zip)

CustomerID包括为:

insert into Customers(CustomerID,Name,PhoneNumber,DoorNo,StreetName,City,Statee,Zipcode) values(@customerid,@name,@phone,@doorno,@streetname,@city,@state,@zip)

答案 3 :(得分:-1)

我建议从sp @customerid删除CustomerID是自动增量字段,因此无需为CustomerID传递任何值。它应该是这样的:

alter procedure stp_customers_insert(@name varchar(50),@phone varchar(50),@doorno varchar(50),@streetname varchar(50),@city varchar(50),@state varchar(50),@zip int)
as
    begin
    if exists(select CustomerID from Customers where Name = @name ,phone = @phone ,doorno = @doorno ,streetname = @streetname ,city= @city,state= @state , zip = @zip )
    begin
        raiserror('employee id already exists',1,1)
    end
else
    begin
        insert into Customers(Name,PhoneNumber,DoorNo,StreetName,City,Statee,Zipcode) values(@name,@phone,@doorno,@streetname,@city,@state,@zip)
    end
end

exec stp_customers_insert 'ram','674673932','122','5th cross','trichy','tamilnadu',620001
相关问题