【问题标题】:Query returns correct results, stored proc does not. Need a second set of eyes查询返回正确的结果,存储过程没有。需要第二双眼睛
【发布时间】:2011-08-30 15:17:32
【问题描述】:

我的查询:

select
  *
from
  meet_cert_credit
where
  conf_number = '1132'
  and type_of_professional = 'Certified Hazardous Materials Managers'

我的存储过程:

ALTER PROCEDURE [dbo].[sp_check_credit_info] 

@cn varchar = NULL,
@top varchar = NULL
AS
BEGIN
SET NOCOUNT ON;

select 
    * 
from 
    meet_cert_credit 
where 
    Conf_number = @cn 
    and type_of_professional = @top
END

调用我的存储过程:

exec sp_check_credit_info '1132', 'Certified Hazardous Materials Managers'

运行查询时,它会返回结果。运行存储过程时,我什么也得不到。

我疯了吗?

【问题讨论】:

标签: sql sql-server-2005 tsql


【解决方案1】:

您需要为您的varchar 存储过程参数指定一个长度。

例如使用@cn varchar(30) 而不是@cn varchar

目前您传入的所有内容都被截断为 1 个字符,因此您可以有效地进行以下搜索。

select 
    * 
from 
    meet_cert_credit 
where 
    Conf_number = '1' 
    and type_of_professional = 'C'

因此没有结果。

【讨论】:

  • 哇。感谢第二双眼睛!
  • +1,我认为未指定长度 varchar 字符串的默认值是 30,但在这里它被截断为 1 个字符,试试看:declare @x varchar; set @x='123456789+123456789+123456789+123456789+';select @x 故事的寓意,总是指定字符串的长度!!!
  • @KM - 是的,CAST(@X AS VARCHAR) 默认为 30,但在这种情况下,出于某种原因,它是 1。
  • 我总是在参数上定义长度以避免错误(如果字符串 > 字段大小)。截断是一个很酷的花絮。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多