mySQL 存储过程中未知的无效类型转换
Posted
技术标签:
【中文标题】mySQL 存储过程中未知的无效类型转换【英文标题】:Unknown invalid type converson in my SQL stored Procedure 【发布时间】:2014-06-25 18:41:35 【问题描述】:错误:将 nvarchar 值“WOID 的值”转换为数据类型 int 时转换失败
系统背景: 使用 MS-Access 2010 在 VBA 中编码。目前正在处理模块后面的代码并调用存储过程。存储过程是用 SQL 编写的,并在存储数据库的 Ms-SQL server 2008 应用程序上运行。
存储过程:存储过程的目的是:
检索三个输入参数:WOID、SampleID 和 Analyte 加入两个表:tblWoSampleTest , tblTest 在三个值匹配的地方选择 testID 将检索到的 testId 的值保存在输出参数中注意:WOID 和 SampleID 列在 tblWoSampleTest 中,Analyte 在 tbltest 中
这是我在 SQL management Studio 上测试过的代码,似乎可以正常工作。有一个 IsNUll 语句,因为有时 testId 不存在,如果返回零,我知道跳过某些逻辑。
CREATE PROCEDURE upGetTestIDForAnalyte @WOID nvarchar(60), @SampleID nvarchar(60),@Analyte nvarchar(60), @TestId int OUT
AS
SELECT @TestID = t1.TestID
FROM tblWOSampleTest t1
JOIN tblTest t2
ON t1.TestID=t2.TestID
WHERE @WOID = t1.WOID AND @SampleID = t1.SampleID AND @Analyte = t2.Analyte
SELECT @TestID =ISNULL(@TestID,0)
go
在访问过程中,WOID 被声明为一个字符串。此外,当我查看 tblWOSAmpleTest 的 WOID 列中的数据类型时,它是 nvarchar,SampleID 是 int,Analyte 是 int。关于什么可能是错的任何建议?如果在调用与实际存储过程本身相反的存储过程时经常发生这种类型的错误,我也可以提供我的 VBA 代码 更新调用代码:
Set cmd = New ADODB.Command
cmd.ActiveConnection = Conn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "upGetTestIDForAnalyte"
cmd.Parameters.Append cmd.CreateParameter("@Analyte", adVarChar, adParamInput, 60, Analyte)
cmd.Parameters.Append cmd.CreateParameter("@WOID", adVarChar, adParamInput, 60, ThisWOID)
cmd.Parameters.Append cmd.CreateParameter("@SampleID", adDouble, adParamInput, 60, ThisSampleID)
cmd.Parameters.Append cmd.CreateParameter("@testid", adDouble, adParamOutput, , adParamReturnValue)
cmd.Execute
Conn.Close
ThisTestID = cmd.Parameters("@testid").Value
t1.WOID 也是 tblWOsampletest 中列 WOID 的数据类型,类型为 nvarchar (10)。我传递给存储过程的字符串也看起来像这样“1406-00591” Update2:这里是tblwosampletest(t1)的表定义
CREATE TABLE [dbo].[tblWOSampleTest](
[WOID] [nvarchar](10) NOT NULL,
[SampleID] [int] NOT NULL,
[TestID] [int] NOT NULL,
[TestPrice] [float] NULL,
[StatusID] [int] NULL,
[EnterID] [int] NULL,
[ModID] [int] NULL,
[EnterDate] [datetime] NULL,
[ModDate] [datetime] NULL,
[AnalysisId] [int] NULL,
[DateAnalyzed] [datetime] NULL,
[ActResult] [float] NULL,
[QueueId] [int] NULL,
[OnHold] [bit] NOT NULL,
[Approved] [bit] NOT NULL,
[AddToQueue] [bit] NOT NULL,
[TemplateID] [int] NULL,
[FormulaId] [int] NULL,
[Signature] [int] NULL,
[Initials] [int] NULL,
[FlagTest] [int] NULL,
[DisplayedResult] [nvarchar](50) NULL,
[TechId] [int] NULL,
[DeleteTest] [bit] NOT NULL,
[RdSerialNum] [nvarchar](50) NULL,
[RdStartDate] [datetime] NULL,
[RdEndDate] [datetime] NULL,
[ScinilatedDT] [datetime] NULL,
[RdCPMA] [float] NULL,
[RdBackground] [float] NULL,
[RdDecayhrs] [float] NULL,
[RdExposhrs] [float] NULL,
[RdElutehrs] [float] NULL,
[RdDegreesF] [float] NULL,
[RdWCmpSample] [float] NULL,
[RdWCpmBKrnd] [float] NULL,
[RdWTFourAnaTime] [float] NULL,
[RdWEluTime] [float] NULL,
CONSTRAINT [PK_tblWOSampleTest] PRIMARY KEY CLUSTERED
(
[WOID] ASC,
[SampleID] ASC,
[TestID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tblWOSampleTest] WITH NOCHECK ADD CONSTRAINT [FK_tblWOSampleTest_tblTest] FOREIGN KEY([TestID])
REFERENCES [dbo].[tblTest] ([TestID])
GO
ALTER TABLE [dbo].[tblWOSampleTest] CHECK CONSTRAINT [FK_tblWOSampleTest_tblTest]
GO
ALTER TABLE [dbo].[tblWOSampleTest] WITH NOCHECK ADD CONSTRAINT [FK_tblWOSampleTest_tblWO] FOREIGN KEY([WOID])
REFERENCES [dbo].[tblWO] ([WOID])
GO
ALTER TABLE [dbo].[tblWOSampleTest] CHECK CONSTRAINT [FK_tblWOSampleTest_tblWO]
GO
ALTER TABLE [dbo].[tblWOSampleTest] WITH NOCHECK ADD CONSTRAINT [FK_tblWOSampleTest_tlkpQueue] FOREIGN KEY([QueueId])
REFERENCES [dbo].[tlkpQueue] ([QueueId])
GO
ALTER TABLE [dbo].[tblWOSampleTest] CHECK CONSTRAINT [FK_tblWOSampleTest_tlkpQueue]
GO
ALTER TABLE [dbo].[tblWOSampleTest] ADD CONSTRAINT [DF_tblWOSampleTest_TestPrice] DEFAULT (0) FOR [TestPrice]
GO
ALTER TABLE [dbo].[tblWOSampleTest] ADD CONSTRAINT [DF_tblWOSampleTest_ActResult] DEFAULT (0) FOR [ActResult]
GO
ALTER TABLE [dbo].[tblWOSampleTest] ADD CONSTRAINT [DF_tblWOSampleTest_QueueId] DEFAULT (0) FOR [QueueId]
GO
ALTER TABLE [dbo].[tblWOSampleTest] ADD CONSTRAINT [DF_tblWOSampleTest_OnHold] DEFAULT (1) FOR [OnHold]
GO
ALTER TABLE [dbo].[tblWOSampleTest] ADD CONSTRAINT [DF_tblWOSampleTest_Approved] DEFAULT (0) FOR [Approved]
GO
ALTER TABLE [dbo].[tblWOSampleTest] ADD CONSTRAINT [DF_tblWOSampleTest_AddToQueue] DEFAULT (0) FOR [AddToQueue]
GO
ALTER TABLE [dbo].[tblWOSampleTest] ADD CONSTRAINT [DF_tblWOSampleTest_DeleteTest] DEFAULT (0) FOR [DeleteTest]
GO
ALTER TABLE [dbo].[tblWOSampleTest] ADD CONSTRAINT [DF_tblWOSampleTest_RdCPMA] DEFAULT (0) FOR [RdCPMA]
GO
ALTER TABLE [dbo].[tblWOSampleTest] ADD CONSTRAINT [DF_tblWOSampleTest_RdBackground] DEFAULT (0) FOR [RdBackground]
GO
ALTER TABLE [dbo].[tblWOSampleTest] ADD CONSTRAINT [DF_tblWOSampleTest_RdDecayhrs] DEFAULT (0) FOR [RdDecayhrs]
GO
ALTER TABLE [dbo].[tblWOSampleTest] ADD CONSTRAINT [DF_tblWOSampleTest_RdExposhrs] DEFAULT (0) FOR [RdExposhrs]
GO
ALTER TABLE [dbo].[tblWOSampleTest] ADD CONSTRAINT [DF_tblWOSampleTest_RdElutehrs] DEFAULT (0) FOR [RdElutehrs]
GO
ALTER TABLE [dbo].[tblWOSampleTest] ADD CONSTRAINT [DF_tblWOSampleTest_RdDegreesF] DEFAULT (0) FOR [RdDegreesF]
GO
ALTER TABLE [dbo].[tblWOSampleTest] ADD CONSTRAINT [DF_tblWOSampleTest_RdWCmpSample] DEFAULT (0) FOR [RdWCmpSample]
GO
ALTER TABLE [dbo].[tblWOSampleTest] ADD CONSTRAINT [DF_tblWOSampleTest_RdWCpmBKrnd] DEFAULT (0) FOR [RdWCpmBKrnd]
GO
ALTER TABLE [dbo].[tblWOSampleTest] ADD CONSTRAINT [DF_tblWOSampleTest_RdWTFourAnaTime] DEFAULT (0) FOR [RdWTFourAnaTime]
GO
ALTER TABLE [dbo].[tblWOSampleTest] ADD CONSTRAINT [DF_tblWOSampleTest_RdWEluTime] DEFAULT (0) FOR [RdWEluTime]
GO
【问题讨论】:
请附上电话号码。 这部分WHERE @WOID = t1.WOID
很可能失败了。 t1.WOID
是什么类型的?
您能否再次检查 t1.WOID 的数据类型?我不相信它是表中的 nvarchar。
我更新了我的问题以包括一些说明
您能否在您的问题中也添加 tblWOSampleTest 的表定义?
【参考方案1】:
尝试手动将 t1.WOID 转换为 nvarchar。
CREATE PROCEDURE upGetTestIDForAnalyte @WOID nvarchar(60), @SampleID nvarchar(60),@Analyte nvarchar(60), @TestId int OUT
AS
SELECT @TestID = t1.TestID
FROM tblWOSampleTest t1
JOIN tblTest t2
ON t1.TestID=t2.TestID
WHERE @WOID = CONVERT(NVARCHAR,t1.WOID) AND @SampleID = t1.SampleID AND @Analyte = t2.Analyte
SELECT @TestID =ISNULL(@TestID,0)
希望这会有所帮助。
【讨论】:
如果数据库中已经存在 nvarchar (10),为什么还要键入 cast? 我过去也遇到过类似的问题,如果皈依者能提供帮助,那么他遇到的问题和我一样。 仅供参考,我不是对您的答案投反对票的人。以上是关于mySQL 存储过程中未知的无效类型转换的主要内容,如果未能解决你的问题,请参考以下文章
ORA-01722: 无效编号 当我尝试执行存储过程时。甲骨文错误