可以在存储过程中进行条件判断吗

ruyunluck 2002-09-24 10:06:19
有一个存储过程:
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;

...全文
238 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ruyunluck 2002-10-01
  • 打赏
  • 举报
回复
那个问题其实并没有解决好,祝国庆愉快
u2m 2002-09-28
  • 打赏
  • 举报
回复
进行条件判断,你写个触发器多好
forgot 2002-09-27
  • 打赏
  • 举报
回复
set @s1='select * from table where '+@name+
' like ''%'+@contents+'%'''
ruyunluck 2002-09-27
  • 打赏
  • 举报
回复
太奇怪了。我昨天还通过了一次,今天通不过了。

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.到底是怎么回事?
ruyunluck 2002-09-27
  • 打赏
  • 举报
回复
痛苦...
newyj 2002-09-27
  • 打赏
  • 举报
回复
我倒居然好没解决
不就是少了空格,我好像告诉过你了



请大家去 http://www.new7wonders.com/c/voting.php 投长城一票
ssl2000 2002-09-26
  • 打赏
  • 举报
回复
我刚试过了,显示正常,(下面是修改后的代码)
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;
ruyunluck 2002-09-26
  • 打赏
  • 举报
回复
我想把查到的记录用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);

ruyunluck 2002-09-26
  • 打赏
  • 举报
回复
set @sl='select * from table where '''+@name
+''' like ''%'+@contents+'%'''
这一步有希望了.....
forgot 2002-09-26
  • 打赏
  • 举报
回复
好了,高手来了:
set @sl='select * from table where '''+@name
+''' like ''%'+@contents+'%'''
绝对能搞定!
chenypluck 2002-09-26
  • 打赏
  • 举报
回复
set @sl="select * from table where "+@name+" like '%"+@contents+"%'"
连这一句都通不过呀,难过
hycao 2002-09-24
  • 打赏
  • 举报
回复
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'
ruyunluck 2002-09-24
  • 打赏
  • 举报
回复
不难吧
ruyunluck 2002-09-24
  • 打赏
  • 举报
回复
请朋友们发表高见。谢谢

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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