sql语法问题 ( 积分: 50 )

  • 主题发起人 主题发起人 evanou
  • 开始时间 开始时间
E

evanou

Unregistered / Unconfirmed
GUEST, unregistred user!
SQL语句如下:<br>sqlstr:='select&nbsp;*&nbsp;from&nbsp;table&nbsp;where&nbsp;:a&nbsp;:b&nbsp;:c';<br>adoquery1.sql.add(sqlstr);<br>adoquery1.sql.Parameters.ParamByName('a').Value:=a;<br>adoquery1.sql.Parameters.ParamByName('b').Value:=b;//这个参数是'='或'&gt;'之类的。<br>adoquery1.sql.Parameters.ParamByName('c').Value:=c;<br>adoquery1.open;<br>为什么会出错呢?提示信息为@P1&nbsp;附近有语法错误。
 
肯定错吗。<br>sqlstr:='select&nbsp;*&nbsp;from&nbsp;table&nbsp;where&nbsp;fieldName1=:a&nbsp;fieldName1=:b&nbsp;fieldName1=:c'<br>再说一般不这样用的。<br>adoquery1.sql.text&nbsp;:=&nbsp;Format('select&nbsp;*&nbsp;from&nbsp;table&nbsp;where&nbsp;fieldName1=:%s&nbsp;and&nbsp;fieldName1=%s&nbsp;and&nbsp;fieldName1=%s',[a,b,c])<br>adoquery1.open;<br>给分吧。
 
SQL语句如下:<br>sqlstr:='select&nbsp;*&nbsp;from&nbsp;table&nbsp;where&nbsp;a=:a&nbsp;and&nbsp;b=:b&nbsp;and&nbsp;c=:c';<br>adoquery1.sql.Parameters.ParamByName('a').Value:=a;<br>adoquery1.sql.Parameters.ParamByName('b').Value:=b;<br>adoquery1.sql.Parameters.ParamByName('c').Value:=c;<br>adoquery1.open;<br>呵呵,试试就知道对不对了!
 
恩,我也是用8689233写的方法..[:)]
 
这个是带参数的sql语句,sqlstr:='select&nbsp;*&nbsp;from&nbsp;table&nbsp;t&nbsp;where&nbsp;t.fieldName1=:a&nbsp;t.fieldName2=:b&nbsp;and&nbsp;&nbsp;t.fieldName3=:c'
 
sqlstr:='select&nbsp;*&nbsp;from&nbsp;table&nbsp;where&nbsp;:a'+b+':c';<br>adoquery1.sql.add(sqlstr);<br>adoquery1.sql.Parameters.ParamByName('a').Value:=a;<br>adoquery1.sql.Parameters.ParamByName('c').Value:=c;<br>adoquery1.open;<br><br>不能传&nbsp;是'='或'&gt;'之类&nbsp;作为参数,这些是运算符
 
楼主出错的原因是:<br>:a,&nbsp;:b,&nbsp;:c三个参数,因为传进去的是字符串,所以,生成的SQL语句,这三个参数相应位置会加上引号,也就是说,变成了:<br>select&nbsp;*&nbsp;from&nbsp;table&nbsp;where&nbsp;'Field1'&nbsp;'='&nbsp;'A'<br>这种形式。<br>所以会出错
 
新的问题出现了,下面的语句没有运算符,为什么也不行呢?提示信息说必须声明变量<br>&nbsp;&nbsp;adoquery1.SQL.Text:='select&nbsp;*&nbsp;from&nbsp;:a&nbsp;where&nbsp;:b&nbsp;=&nbsp;:c';<br>&nbsp;&nbsp;adoquery1.Parameters[0].Value:=trim(edit1.Text&nbsp;);<br>&nbsp;&nbsp;adoquery1.Parameters[1].Value:=trim(edit2.Text&nbsp;);<br>&nbsp;&nbsp;adoquery1.Parameters[2].Value:=trim(edit2.Text&nbsp;);<br>&nbsp;&nbsp;adoquery1.Open;
 
原因我已经说过了。<br>select&nbsp;*&nbsp;from&nbsp;'Table1'&nbsp;where&nbsp;'Field1'&nbsp;=&nbsp;'A'<br>这种形式。你觉得呢?会对吗?
 
楼主是新手哦。。上面给的答案已经很清楚了。。我觉得代码风格最好的就是<br><br>sushicheng,&nbsp;<br>sqlstr:='select&nbsp;*&nbsp;from&nbsp;table&nbsp;where&nbsp;fieldName1=:a&nbsp;fieldName1=:b&nbsp;fieldName1=:c'<br>再说一般不这样用的。<br>adoquery1.sql.text&nbsp;:=&nbsp;Format('select&nbsp;*&nbsp;from&nbsp;table&nbsp;where&nbsp;fieldName1=:%s&nbsp;and&nbsp;fieldName1=%s&nbsp;and&nbsp;fieldName1=%s',[a,b,c])<br>adoquery1.open;
 
adoquery1.Parameters[0].Value:=trim(edit1.Text&nbsp;);<br>&nbsp;&nbsp;adoquery1.Parameters[1].Value:=trim(edit2.Text&nbsp;);<br>&nbsp;&nbsp;adoquery1.Parameters[2].Value:=trim(edit2.Text&nbsp;);
 
后退
顶部