【问题标题】:Conversion failed whenc convert nvarchar to int当c 将 nvarchar 转换为 int 时转换失败
【发布时间】:2014-04-16 07:15:18
【问题描述】:

大家好,请帮帮我

我有存储过程

但是当它被执行时,当将 nvarchar 转换为 int 时,它得到异常覆盖失败,这可能是什么问题。

在我的存储过程中,一个输入参数和一个输出参数,我需要导致已经在 nvarchar 数据类型中的测试变量,而不是错误将转换失败我不知道是怎么发生的,请帮助我,请解释一下这是怎么回事发生了,所以下次我会照顾这个

ALTER PROCEDURE [dbo].[DirectMarketingbyBusiness_id] 
    -- Add the parameters for the stored procedure here
    (
            @business_id int,
            @test nvarchar(500) output

    )
    as
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    select 
    @test=

ISNULL(case
 when formXml.value('(/XmlDataPairDocument/dataitem[@id="chkSmsText"]/@value)[1]','nvarchar(max)') =1
then  'SMS (text messaging),' end,'test,') +

ISNULL(case
 when formXml.value('(/XmlDataPairDocument/dataitem[@id="chkliveTelelhoneText"]/@value)[1]','nvarchar(max)') =1
 then  'Live telephone calling,' end,'test,') +

ISNULL( case
 when formXml.value('(/XmlDataPairDocument/dataitem[@id="chkvoicebroadcastText"]/@value)[1]','nvarchar(max)') =1
 then  'Voice broadcast messaging,' end,'test,') +

ISNULL( case
 when formXml.value('(/XmlDataPairDocument/dataitem[@id="chkEmailText"]/@value)[1]','nvarchar(max)') =1
 then  'Email,' end,'test,') +

ISNULL(case
 when formXml.value('(/XmlDataPairDocument/dataitem[@id="chkPostText"]/@value)[1]','nvarchar(max)') =1
 then  'Post,' end,'test,') +

ISNULL( case
 when formXml.value('(/XmlDataPairDocument/dataitem[@id="chkAnyOtherText"]/@value)[1]','nvarchar(max)') =1
 then  'chkAnyOtherText,' end,'test,') +

ISNULL( case
 when formXml.value('(/XmlDataPairDocument/dataitem[@id="TxtOtherText"]/@value)[1]','nvarchar(max)') <>''
 then  formXml.value('(/XmlDataPairDocument/dataitem[@id="TxtOtherText"]/@value)[1]','nvarchar(max)') end,'test') 




from tblRenewals
where 
business_id=@business_id and year=2014
return @test

结束

【问题讨论】:

  • 尝试从存储过程中取出源代码,并在 SSMS 中运行它,看看它是否给你错误。我们无法访问 formXml - 因此很难查明您的问题 - 但似乎函数的值和“=1”是一个潜在问题。
  • 当我将此 sp 运行到 ssms 时,它工作正常
  • 这里是我的 .net 上的代码
  • command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@test", SqlDbType.NVarChar ,50); command.Parameters["@test"].Direction = ParameterDirection.Output; command.Parameters.AddWithValue("@business_id", oBusinesse.id); command.ExecuteNonQuery(); direct = (string)command.Parameters["@test"].Value;
  • oBusinesse.id 是整数吗?如果是,请尝试在从应用程序传递参数时设置参数,使用整数数据类型。

标签: sql sql-server xml


【解决方案1】:
create PROCEDURE [dbo].[DirectMarketingbyBusiness_id] 
    -- Add the parameters for the stored procedure here
    (
            @business_id int,
            @test nvarchar(500) output

    )
    as
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    select 
    @test=

ISNULL(case
 when formXml.value('(/XmlDataPairDocument/dataitem[@id="chkSmsText"]/@value)[1]','nvarchar(max)') =1
then  'SMS (text messaging),' end,'test,') +

ISNULL(case
 when formXml.value('(/XmlDataPairDocument/dataitem[@id="chkliveTelelhoneText"]/@value)[1]','nvarchar(max)') =1
 then  'Live telephone calling,' end,'test,') +

ISNULL( case
 when formXml.value('(/XmlDataPairDocument/dataitem[@id="chkvoicebroadcastText"]/@value)[1]','nvarchar(max)') =1
 then  'Voice broadcast messaging,' end,'test,') +

ISNULL( case
 when formXml.value('(/XmlDataPairDocument/dataitem[@id="chkEmailText"]/@value)[1]','nvarchar(max)') =1
 then  'Email,' end,'test,') +

ISNULL(case
 when formXml.value('(/XmlDataPairDocument/dataitem[@id="chkPostText"]/@value)[1]','nvarchar(max)') =1
 then  'Post,' end,'test,') +

ISNULL( case
 when formXml.value('(/XmlDataPairDocument/dataitem[@id="chkAnyOtherText"]/@value)[1]','nvarchar(max)') =1
 then  'chkAnyOtherText,' end,'test,') +

ISNULL( case
 when formXml.value('(/XmlDataPairDocument/dataitem[@id="TxtOtherText"]/@value)[1]','nvarchar(max)') <>''
 then  formXml.value('(/XmlDataPairDocument/dataitem[@id="TxtOtherText"]/@value)[1]','nvarchar(max)') end,'test') 


 end

 exec [dbo].[DirectMarketingbyBusiness_id] 1, 'texttext'

执行存储过程。函数需要返回值。

【讨论】:

  • 我不清楚你的回答,请详细解释一下你想告诉我什么
猜你喜欢
  • 2011-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-17
  • 2011-08-29
  • 2015-06-20
  • 2016-02-02
  • 1970-01-01
相关资源
最近更新 更多