我正在尝试将字符串传递给存储过程。我的代码是:
CREATE PROCEDURE AddCondition
@CondName varchar(50),
@CondDesc varchar(50)
AS
DECLARE @CondID int
--This code gets the next ID for the table
SET @CondID = (SELECT MAX([Condition ID]) FROM [dbo].[Tube Conditions]) + 1
SET @CondID = ISNULL(@CondID,1)
--This code adds records to the table
INSERT INTO [dbo].[Tube Conditions] ([Condition ID],[Condition Name],[Description])
VALUES (@CondID, @CondName, @CondDesc)
GO
我尝试执行:
DECLARE @aName varchar(50), @aDesc varchar(50)
SET @aName = 'Lathed'
SET @aDesc = 'The tube has been lathed to size'
EXECUTE AddCondition @aName, @aDesc
GO
但我一直收到错误:
Msg 245,Level 16,State 1,Procedure AddCondition,Line 51
将varchar值'Lathed'转换为数据类型int时,转换失败。
这个过程在处理int和float值时起作用,所以我不确定我在这里缺少什么。我正在使用带有SQL Server Express的SQL Server 2014 Management Studio。
提前致谢!
答案 0 :(得分:5)
从你发布的代码中,只有一件事会产生你指定的错误,那就是表[Condition Name]
中的[dbo].[Tube Conditions]
列的类型是int,而不是varchar是预期的通过插入声明。
顺便说一句:这是一种非常危险的方式来生成表中的下一个ID - 当同时插入2条记录时,它会引发并发问题。 SQL将为您生成此标识,查找它。
答案 1 :(得分:0)
Agree with Jamiec you probably have [dbo].[Tube Conditions] of type int
a single statement is a transaction
INSERT INTO [dbo].[Tube Conditions] ([Condition ID],[Condition Name],[Description])
SELECT MAX(isnull([Condition ID],0)) + 1, 'Lathed', 'The tube has been lathed to size'
FROM [dbo].[Tube Conditions]