sql的高手都到哪去了,这个子查询在赋值时怎么老是出错呀!急。急呀,我调试了一个多星期都搞不出 ,怪怪怪事呀,大家都进来看一下 ( 积分: 50 )

  • 主题发起人 主题发起人 方英龙
  • 开始时间 开始时间

方英龙

Unregistered / Unconfirmed
GUEST, unregistred user!
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select gbxm from hw_gb where d_id=(select d_id from hw_d where k_id=:kidh)');
adoquery1.Parameters.ParamByName('kidh').Value:=kids;
adoquery1.Prepared;
adoquery1.Open;
 
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select gbxm from hw_gb where d_id=(select d_id from hw_d where k_id=:kidh)');
adoquery1.Parameters.ParamByName('kidh').Value:=kids;
adoquery1.Prepared;
adoquery1.Open;
 
是不是k_id等于这个值的记录有多行啊,那要用d_id in (...),不要用 d_id=(...) 。
或者SQL语句直接写成
'select gbxm from hw_gb where d_id=(select d_id from hw_d where k_id='+kids+')'
 
'select gbxm from hw_gb where d_id in (select d_id from hw_d where k_id='+kids+')'
 
'select gbxm from hw_gb where d_id in (select d_id from hw_d where k_id='+kids+')'
我用in也不行呀
 
'select gbxm from hw_gb where d_id in (select d_id from hw_d where k_id='"+kids+"')'
行吗?
kids与k_id类型是否对?
 
是编译不了还是运行报错?
如果k_id是整型,kids是字符型,就用k_id='+kids+')'
如果k_id是整型,kids是整型,就用k_id='+IntToStr(kids)+')'
如果k_id是字符型,kids是字符型,就用k_id='+QuotedStr(kids)+')'
如果k_id是字符型,kids是整形型,就用k_id='+QuotedStr(IntToStr(kids))+')'
 
都是字符型的,k_id是字段,而kids是一个变量呀,请你看一下最开始的问题好吗
 
没错啊,那就用:
'select gbxm from hw_gb where d_id in (select d_id from hw_d where k_id='+QuotedStr(kids)+')'
 
楼上的大哥:刚才我编译了一上,这条语句在语法上没有错误,可运时没有结果呀
adoquery1.SQL.Add('select gbxm from hw_gb where hw_gb.d_id in (select d_id from hw_d where hw_d.k_id='+quotedstr(kids)+')');
 
sql:='select top 1 * from userlist where username<>'''+fname+''' and username not in (select touser from log where ip='''+ips+''') and yn=1 order id desc';

全是单引号。
 
参数'kidh'要写成'@kidh'
 
先改成這樣試試:
adoquery1.SQL.Add('select gbxm from hw_gb where d_id in (select d_id from hw_d where k_id=:kidh)');
adoquery1.Parameters.ParamByName('kidh').Value:=#39+kids+#39;
如果不行就換成存儲過程吧,在adoquery中用像
select d_id from hw_d where k_id=:kidh 的經常有莫名其妙的問題,不知是不是DELPHI的BUG.我都怕了這樣寫.存儲過程應該沒有問題.
 
把k_id=:kidh 改成k_id= :kidh,我上次这样改了一下就行了
 
楼上的大哥:
感谢你们,我刚才用guoqun大哥的方法调试了一下,没有出现错误了,只是查询了一下没有结果,不知道是怎么回事,
procedure Tclcq.FormCreate(Sender: TObject);
begin
adoquery1.Connection:=login.ADOConnection1;
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select gbxm from hw_gb where d_id in (select d_id from hw_d where k_id=:kidh)');
//adoquery1.SQL.Add('select d_id from hw_d where k_id=00-00');
adoquery1.Parameters.ParamByName('kidh').Value:=#39+kids+#39;
adoquery1.Prepared;
adoquery1.Open;
end;
说明一下:kids是全局变量,它是从其它地方传过来的一个变量值。是不是这里出错
 
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select gbxm from hw_gb');
adoquery1.SQL.Add(' where d_id in (select d_id from hw_d where k_id=''' + kids + ''')');
adoquery1.Open;
 
1、将上述语句在查询分析器中执行试试(如果是SQL Server)
2、hw_gb.d_id 与 hw_d.d_id 数据类型是否一致(包括CHAR与VARCHAR的差别)
3、hw_d.k_id是什么数据类型(CHAR?VARCHAR?)?值后面有没有空格?
 
hw_gb.d_id 与 hw_d.d_id 都是varchar 20
 
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select gbxm from hw_gb where d_id=(select d_id from hw_d where k_id='+''''+kidh+''''+')');
adoquery1.Open;
 
后退
顶部