日期:2014-05-18  浏览次数:20510 次

字符型变量获取不到值,总是取到第一个,为什么?
我在开发系统的时候,写了一个SQL代码,如下:
declare   @CardID   int,@pmember_login   varchar(10),@pmember_pwd   varchar(10)
declare   @mycount   int,@i   int
select   @i   =   0
select   @mycount   =   count(*)   from   [Job_p_pmember]   where   pmember_login   = '7 '  
myLoop:
select   @CardID=pmember_CardID   from   [Job_p_pmember]   where   pmember_login   = '7 '
if   @CardID> 0  
begin
select   @pmember_login=   CardNo,@pmember_pwd   =   CardPwd   from   [Job_CardInfo]   where   CardID   =   @CardID
print   @pmember_login     第一条结果:7079158
print   @pmember_pwd         第一条结果:13038
--下面的语句将取到的值更新到pmember中
Update   [pmember]   Set   pmember_login   =   @pmember_login,pmember_pwd   =@pmember_pwd   where   pmember_CardID=   @CardID(这里有问题)
select   @i   =   @i+1
end
while   (@i <@mycount)
goto   myLoop

更新后的数据库中的结果是pmember_login= '7 ',为什么我已经将变量设置为字符型的了,更新的时候变成第一个数字了呢?

------解决方案--------------------
对的,没有任何疑问。

如果你想更新后的显示结果是pmember_login= '7 ',这样:

select @i = 0
select @mycount = count(*) from [Job_p_pmember] where pmember_login = ' ' '7 ' ' '
myLoop:
select @CardID=pmember_CardID from [Job_p_pmember] where pmember_login = ' ' '7 ' ' '
if @CardID> 0
------解决方案--------------------
Update aa Set pmember_login = bb.CardNo,pmember_pwd =bb.CardPwd
from [pmember] aa inner join
(select a.cardNo,a.CardPwd,a.CardID from [Job_CardInfo] a inner join [Job_p_pmember] b on a.CardID=b.pmember_CardID where pmember_login = '7 ' and pmember_cardid> 0) bb
on aa.pmember_CardID=bb.CardId

------解决方案--------------------
不是很清楚你要实现的目录,
不过其中
select @CardID=pmember_CardID from [Job_p_pmember] where pmember_login = '7 '
只能取得最后一条记录的pmember_cardid,也就是每次循环得到的@cardid的值是一样的
如要取得各条记录的pmember_cardID值,应该建一游标如下
declare job_p_pmember cursor for select pmember_cardid from job_p_pmember where pmember_login= '7 '
open job_p_pmember
fetch next from job_p_pmember into @cardid
while @@fetch_status=0
begin
对各个@cardid进行处理
........
fetch next from job_p_pmember into @cardid
end

------解决方案--------------------
我大致看了一下,LZ的循环有问题
------解决方案--------------------
select @CardID=pmember_CardID from [Job_p_pmember] where pmember_login = '7 '

这个@CardID值怎么可能变? 这个一定是指定顺序的pmember_CardID的最后一个数值啊