有一个存储过程:
create procedure chaxunproc
@name varchar(50),      //对应字段名
@contents varchar(50)   //对应查询内容
as 
declare @s1 varchar(200)
set @sl='select * from table where '+@name+'like'''+@contents+''''//这一句这样写对不对?
exec @sl
if exists(@s1)  判断查询的内容是否存在。同时请问这样写法是否正确?
print 'ok'  //如果找到,显示OK。其实我想在这一句去改变dbgrid或STRINGGRID查找到的记录的颜色,可不可以?
else
print 'no find'在DELPHI中调用adostoredproc1
 with adostoredproc1 do
    begin
      parameters[0].value:=combobox1.text;
      parameters[1].value:=edit1.text;
      parameters.ParamByName('@name').asstring:=combox1.text;
      parameters.ParamByName('@contents').asstring:=edit1.text;
      open;
    end;

解决方案 »

  1.   

    Delphi不是很熟,存储过程应做如下修改:
    CREATE procedure chaxunproc
    @name varchar(50),      --//对应字段名
    @contents varchar(50)   --//对应查询内容
    as 
    declare @sl varchar(200) --//注意@sl与@s1的区别
    set @sl="select * from table where "+@name+" like '%"+@contents+"%'"--//这一句这样写,注意空格
    exec(@sl)
    if @@rowcount>=1     --//判断查询的内容是否存在。大于或等于1则表示有记录
    print 'ok'  --//如果找到,显示OK。
    else
    print 'no find'
      

  2.   

    set @sl="select * from table where "+@name+" like '%"+@contents+"%'"
    连这一句都通不过呀,难过
      

  3.   

    好了,高手来了:
    set @sl='select * from table where '''+@name
    +''' like ''%'+@contents+'%'''
    绝对能搞定!
      

  4.   

    set @sl='select * from table where '''+@name
    +''' like ''%'+@contents+'%'''
    这一步有希望了.....
      

  5.   

    我想把查到的记录用stringgrid显示出来.我的代码如下:
    请朋友们看看错在什么地方?
    stringgrid1.rowcount:=adostoredproc1.recordcount+1;
    stringgrid1.colcount:=adostoredproc1.fields.count;
    for q:=0 to adostoredproc1.fieldcount-1 do
    stringgrid1.cell[q,0]:=adostoredproc1.fields[q].fieldname;//显示字段名
    n:=1; 下面是希望能显示出用存储过程查到的记录。但是没有显示。请问错在哪里?
    while not adostoredproc1.eof do
    begin
    for m:=0 to stringgrid1.colcount-1 do
    begin
    stringgrid1.cells[m,n]:=adostoredproc1.fields[m].asstring;
    end;
    adostoredproc1.next;
    inc(n);
      

  6.   

    我刚试过了,显示正常,(下面是修改后的代码)
    var
    q,n,m:integer;
    begin
    stringgrid1.rowcount:=adostoredproc1.recordcount+1;
    stringgrid1.colcount:=adostoredproc1.FieldCount;
    for q:=0 to adostoredproc1.fieldcount-1 do
    stringgrid1.cells[q,0]:=adostoredproc1.fields[q].fieldname;//显示字段名
    n:=1;
    adostoredproc1.First;    //定位到第一条
    while not adostoredproc1.eof do
    begin
          for m:=0 to adostoredproc1.fieldcount-1 do
          begin
           stringgrid1.cells[m,n]:=adostoredproc1.fields[m].asstring;
          end;
          adostoredproc1.next;
          inc(n);
    end;
    end;
      

  7.   

    我倒居然好没解决
    不就是少了空格,我好像告诉过你了请大家去 http://www.new7wonders.com/c/voting.php 投长城一票
      

  8.   

    太奇怪了。我昨天还通过了一次,今天通不过了。CREATE procedure chaxunproc
    @name varchar(50),      --//对应字段名
    @contents varchar(50)   --//对应查询内容
    as 
    declare @sl varchar(200) 
    set @s1='select * from table where '''+@name+''' like '' %'+@contents+'%'''
    exec(@sl)
    用ADOSTORED调用时又说不认识参数@name.到底是怎么回事?
      

  9.   

    set @s1='select * from table where '+@name+
    ' like ''%'+@contents+'%'''