这个存储过程问题在哪里?

zhangsong841 2005-06-09 04:18:12
这是我的存储过程
CREATE PROCEDURE GetEmpConInf1(
@PubDate datetime,
@GIDID int,
@Code varchar
)
AS
BEGIN

select * from cpucode
where gidid=@GIDID and pubdate>@PubDate and Code = @Code
END
GO
在查询分析器里执行EXEC GetEmpConInf1 '2005-01-03',2, 'sgtrt2'
时什么也查不出,表里是有满足这个条件的纪录的,但不报错,自己感觉是where 里的“and Code = @Code”这个写法好像有问题,
清高手指教,另外如果是“Code like '%@Code%'”该怎样写?
...全文
126 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
duanduan1122 2005-06-09
  • 打赏
  • 举报
回复
@Code varchar===本来就是varchar呀。
duanduan1122 2005-06-09
  • 打赏
  • 举报
回复
郁闷
precipitant 2005-06-09
  • 打赏
  • 举报
回复
肯定是空格,char类型总有这种问题。你应该注意,长度也要有varchar(???)
zhangsong841 2005-06-09
  • 打赏
  • 举报
回复
不是空格的问题,我又是ltrim又是rtrim,但还没什么用。
jdcxf 2005-06-09
  • 打赏
  • 举报
回复
PubDate > convert(datetime,convert(nvarchar(10),@PubDate,120))
duanduan1122 2005-06-09
  • 打赏
  • 举报
回复
LTRIM
删除起始空格后返回字符表达式。

语法
LTRIM ( character_expression )

参数
character_expression

是字符或二进制数据表达式。character_expression 可以是常量、变量或列。character_expression 必须是可以隐性转换为 varchar 的数据类型。否则,使用 CAST 显式转换 character_expression

示例
下例使用 LTRIM 字符删除字符变量中的起始空格。

DECLARE @string_to_trim varchar(60)
SET @string_to_trim = ' Five spaces are at the beginning of this
string.'
SELECT 'Here is the string without the leading spaces: ' +
LTRIM(@string_to_trim)
GO

下面是结果集:

------------------------------------------------------------------------
Here is the string without the leading spaces: Five spaces are at the beginning of this string.

(1 row(s) affected)



duanduan1122 2005-06-09
  • 打赏
  • 举报
回复
RTRIM
截断所有尾随空格后返回一个字符串。

语法
RTRIM ( character_expression )

参数
character_expression

由字符数据组成的表达式。character_expression 可以是常量、变量,也可以是字符或二进制数据的列。
示例
下例显示如何使用 RTRIM 删除字符变量中的尾随空格。

DECLARE @string_to_trim varchar(60)
SET @string_to_trim = 'Four spaces are after the period in this sentence. '
SELECT 'Here is the string without the leading spaces: ' + CHAR(13) +
RTRIM(@string_to_trim)
GO

下面是结果集:

(1 row(s) affected)
------------------------------------------------------------------------
Here is the string without the leading spaces: Four spaces are after the period in this sentence.
(1 row(s) affected)



duanduan1122 2005-06-09
  • 打赏
  • 举报
回复
这样试试:
where gidid=@GIDID and pubdate>@PubDate and Code = ltrim(rtrim(@Code))
xdhou 2005-06-09
  • 打赏
  • 举报
回复
是不是字段类型的问题啊,用rtrim去一下空格试试
mind5 2005-06-09
  • 打赏
  • 举报
回复
不知道是否是varchar 与 char的问题
不妨改一下:and rtrim(Code)=@Code试一下
zhangsong841 2005-06-09
  • 打赏
  • 举报
回复
SELECT *
FROM CPUCode
WHERE (Code = 'sgtrt2') AND (GIDID = 2) AND (PubDate > 2005 - 01 - 03)
执行这句就能查出来
zhangsong841 2005-06-09
  • 打赏
  • 举报
回复
当我把“ where gidid=@GIDID and pubdate>@PubDate and Code = @Code”改为“where gidid=@GIDID and pubdate>@PubDate ”,EXEC GetEmpConInf1 '2005-01-03',2, 'sgtrt2'
就能正常执行,所以日期格式肯定是没问题,问题是在“and Code = @Code” 这个写法上,请高手指教
szh3210 2005-06-09
  • 打赏
  • 举报
回复
日期格式可能不对
yjs_lh 2005-06-09
  • 打赏
  • 举报
回复
select * from cpucode
where gidid=@GIDID and pubdate>@PubDate and Code like '%' + @Code + '%'

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧