有一个存储过程,可以动态选择字段进行查询。
create procedure chaxunproc
@name varchar(50), //对应字段名
@contents varchar(50) //对应查询内容
as
declare @sl varchar(200)
set @sl='select * from table '
set @sl=' where '+@name +'like'''+ @contents +''''
exec @sl我是这样调用的:
var
m,n,:integer;
begin
storedproc1.parambyname('@name').asstring:=combobox1.text;//从combobox1选择字段
storedproc1.parambyname('@contents').asstring:=edit1.text;//在EDIT输入内容
storedproc1.open;
storedproc1.first; //下面代码是想把查询的结果在stringgrid中显示
stringgrid1.rowcount:=storedproc1.recordcount;
stringgrid1.colcount:=storedproc1.fields.count;
n:=0;
while not storedproc1.eof do
begin
for m:=0 to stringgrid1.colcount-1 do
stringgrid1.cells[m,n]:=storedproc1.fields[m].asstring;
storedroc1.next;
inc(n);
end;
但是系统在运行到storedproc1.parambynam('@name').asstring:=combobox1.text
这一句却说找不到@name,@contents,该怎么办呢?如果用QUERY来调用该怎么写?谢谢
create procedure chaxunproc
@name varchar(50), //对应字段名
@contents varchar(50) //对应查询内容
as
declare @sl varchar(200)
set @sl='select * from table '
set @sl=' where '+@name +'like'''+ @contents +''''
exec @sl我是这样调用的:
var
m,n,:integer;
begin
storedproc1.parambyname('@name').asstring:=combobox1.text;//从combobox1选择字段
storedproc1.parambyname('@contents').asstring:=edit1.text;//在EDIT输入内容
storedproc1.open;
storedproc1.first; //下面代码是想把查询的结果在stringgrid中显示
stringgrid1.rowcount:=storedproc1.recordcount;
stringgrid1.colcount:=storedproc1.fields.count;
n:=0;
while not storedproc1.eof do
begin
for m:=0 to stringgrid1.colcount-1 do
stringgrid1.cells[m,n]:=storedproc1.fields[m].asstring;
storedroc1.next;
inc(n);
end;
但是系统在运行到storedproc1.parambynam('@name').asstring:=combobox1.text
这一句却说找不到@name,@contents,该怎么办呢?如果用QUERY来调用该怎么写?谢谢
with query1 do
begin
close;
sql.clear;
sql.add('exec chaxunproc:name,:contents');
params[0].asstring:=combobox1.text;
params[1].asstring:=edit1.text;
execsql;
end;对于你出现的问题,建议重写存储过程:
create procedure chaxunproc
@name varchar(50), //对应字段名
@contents varchar(50) //对应查询内容
as
declare @sl varchar(200)
set @sl='select * from table where '+@name+'like'''+@contents+''''
Go调用的时候:
with adostoredproc1 do
begin
parameters[0].value:=combox1.text;
parameters[1].value:=edit1.text;
parameters.ParamByName('@name').asstring:=combox1.text;
......................('@contents').asstring:=edit1.text;
ExecProc;
end;
stringgrid1.rowcount:=adostoredproc1.recordcount;
stringgrid1.colcount:=adostoredproc1.fields.count;
..............................................
语句时
系统提示:
adostoredproc1:commandtext does not return a result set
请问该怎么解决?谢谢
系统提示:
list index of bounds(1)
这是怎么回事
exec后面根的是存储过程名,你那样执行sql语句不对吧。
把存储过程调到查询分析其中调试一下。
adostoredproc1.recordcount;//必须有结果集
@name varchar(50), //对应字段名
@contents varchar(50) //对应查询内容
as
declare @sl varchar(200)
set @sl='select * from table where '+@name+'like'''+@contents+''''
print @sl //加入此句
exec @sl在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;在edit中输入000119,combobox1中输入编号
系统print以下报警:
select * from table where 编号 like'000119'
create procedure chaxunproc
@contents varchar(50) //对应查询内容
as
select * from table where name=@contents //name是字段名
第二种:
create procedure chaxunproc
@contents varchar(50) //对应查询内容
as
select * from table where name like '%''+@contents+''%'第三种:
CREATE procedure chaxunproc
@contents varchar(50) --//对应查询内容
as
declare @sl varchar(200)
set @sl="select * from table where name like '%"+@contents+"%'" //请问这里'' "" 到底是用单引号还是双引号?第四种:
create procedure chaxunproc
@contents varchar(50) //对应查询内容
as
declare @sl varchar(200)
set @sl='select * from table where '+@name+'like'''+@contents+''''
exec (@sl)用adostoreproc调用:
with adostoredproc1 do
begin
parameters.ParamByName('@name').value:=edit1.text;
open;
end;系统运行却说找不到@contents,该怎么办呢?肯请朋友们提出宝贵的的意见?
我用的是delphi5+sql server7
2、应该是exec(@sl),而不是exec @sl
create procedure chaxunproc
@name varchar(50), //对应字段名
@contents varchar(50) //对应查询内容
as
declare @sl varchar(200)
set @sl='select * from table '
set @sl=' where '+@name +' like '''+ @contents +''''
exec(@sl)
3、最好
storedproc1.parambyname('@contents').asstring:=edit1.text;
改成
storedproc1.parambyname('@contents').asstring:=edit1.text+'%';
让用户不必输入%
4、有安全漏洞。
5、delphi不熟悉,调用问题不知道怎么解决,存储过程的调试可以用查询分系器试试,可以执行说明存储过程没有问题。
create procedure chaxunproc
@contents varchar(50) //对应查询内容
as
select * from table where name=@contents //name是字段名
--只要name字段事字符型的,这个存储过程没有问题,应该是调用的问题。第二种:
create procedure chaxunproc
@contents varchar(50) //对应查询内容
as
select * from table where name like '%''+@contents+''%'
--错了,应该改为:
select * from table where name like '%'+@contents+'%'
第三种:
CREATE procedure chaxunproc
@contents varchar(50) --//对应查询内容
as
declare @sl varchar(200)
set @sl="select * from table where name like '%"+@contents+"%'" //请问这里'' "" 到底是用单引号还是双引号?--应该是:
set @sl='select * from table where name like ''%'+@contents+'%'''
exec(@sl)
第四种:
create procedure chaxunproc
@contents varchar(50) //对应查询内容
as
declare @sl varchar(200)
set @sl='select * from table where '+@name+'like'''+@contents+''''
exec (@sl)--这个@name没有定义!!!
StoredProc1.Params.ParamByName('@name').AsString := combobox1.text;
%
_
[]
[^]
CREATE procedure test
@name varchar(20),
@contents varchar(50)
as
declare @sl varchar(200)
set @sl='select * from a where '+@name+' like ''%'+@contents+'%'''
--上面全是单引号
exec (@sl)
GO
程序调用
adostoredproc1.Parameters.ParamByName('@name').value:=trim(edit1.text);//字段名
adostoredproc1.Parameters.ParamByName('@contents').value:=trim(edit2.text);//内容
adostoredproc1.Open;
//dbgrid显示完全正常,不知道你哪里出的问题?·_·
exec chaxunproc 'name','%a%'
系统说
too many arguments were supplied for procedure chaxunproc然后我建立了最简单的
create procedure chaxunproc
@contents varchar(50) //对应查询内容
as
select * from table where name=@contents //name是字段名
我检查了一下name是varchar类型。
在查询分析器里仍然出现这种情况。这是怎么回事?
create procedure chaxunproc
@contents varchar(50) //对应查询内容
as
select * from table where name=@contents
调用方法:
exec chaxunproc 'aaa'第二种:
create procedure chaxunproc
@contents varchar(50) //对应查询内容
as
select * from table where name like '%'+@contents+'%'调用方法:
exec chaxunproc 'aaa'
第三种:
CREATE procedure chaxunproc
@contents varchar(50) --//对应查询内容
as
declare @sl varchar(200)
set @sl='select * from table where name like ''%'+@contents+'%'''
exec(@sl)
go调用方法:
exec chaxunproc 'aaa'第四种:
create procedure chaxunproc
@contents varchar(50) //对应查询内容
as
declare @sl varchar(200)
set @sl='select * from table '
set @sl=@sl + ' where '+@name +' like '''+ @contents +''''
exec(@sl)调用方法:
exec chaxunproc 'Name','%a%'
storedproc1.parambyname('@contents').asstring:=edit1.text;
storedproc1.open
调用第一种(最简单的存储过程)
系统运行仍然却说找不到@contents