请教一个sql的查询问题 ( 积分: 200 )

W

wwwone

Unregistered / Unconfirmed
GUEST, unregistred user!
大家好,请教一个sql的分页查询问题

x := 1; //x为第几页数
abcd := x * 50;
with dm.ClientDataSet_Add do
begin
Close;
CommandText := '';
CommandText ::= 'select top 50 * from [客户] where (id>(select max(id) from (select top ' + inttostr(abcd) + ' id from [客户] order by id) as t)) order by id';
Open;
end;


这个可以查询到数据

现在我想把"客户"这个表变成一个子查询,也就是用这个:select * from [客户] where 客户名称='''+trim(edit3.text)+''' 替换"客户"这个表


即:

x := 1; //x为第几页数
abcd := x * 50;
with dm.ClientDataSet_Add do
begin
Close;
CommandText := '';
CommandText ::= 'select top 50 * from (select * from [客户] where 客户名称='''+trim(edit3.text)+''') where (id>(select max(id) from (select top ' + inttostr(abcd) + ' id from (select * from [客户] where 客户名称='''+trim(edit3.text)+''') order by id) as t)) order by id';
Open;
end;

执行操作:老是说where附近有语法错识,我的错在那里,应该怎样写

能否帮帮我写一下,分不够再加
 
大家好,请教一个sql的分页查询问题

x := 1; //x为第几页数
abcd := x * 50;
with dm.ClientDataSet_Add do
begin
Close;
CommandText := '';
CommandText ::= 'select top 50 * from [客户] where (id>(select max(id) from (select top ' + inttostr(abcd) + ' id from [客户] order by id) as t)) order by id';
Open;
end;


这个可以查询到数据

现在我想把"客户"这个表变成一个子查询,也就是用这个:select * from [客户] where 客户名称='''+trim(edit3.text)+''' 替换"客户"这个表


即:

x := 1; //x为第几页数
abcd := x * 50;
with dm.ClientDataSet_Add do
begin
Close;
CommandText := '';
CommandText ::= 'select top 50 * from (select * from [客户] where 客户名称='''+trim(edit3.text)+''') where (id>(select max(id) from (select top ' + inttostr(abcd) + ' id from (select * from [客户] where 客户名称='''+trim(edit3.text)+''') order by id) as t)) order by id';
Open;
end;

执行操作:老是说where附近有语法错识,我的错在那里,应该怎样写

能否帮帮我写一下,分不够再加
 
''''+trim(edit3.text)+''''
 
呵呵,就是个引号问题,不值那么多分
 
还有,你这么长的嵌套sql语句,是在烦琐
我是直觉是你的引号问题
还有sql help某些嵌套查询不允许,你看一下
我记不到了
 
建议你用存储过程和变量实现
避免复杂的嵌套和引号问题
 
[客户]替换成((select * from [客户] where 客户名称='''+trim(edit3.text)+''') a)就可以了
 
'SELECT TOP 50 * ' +
' FROM (SELECT TOP ' + IntToStr(abcd) + ' ID ' +
' FROM [客户] ' +
' ORDER BY ID ' +
' ) ' +
' ORDER BY ID DESC';
 
[red]Quotedstr[/red](Trim(edit3.text))比''''+trim(edit3.text)+'''' 方便得多。
 
to bbscom
这个[red]Quotedstr[/red](Trim(edit3.text))是什么意思啊
请解读一下
 
function QuotedStr(const S: string): string;
var
I: Integer;
begin
Result := S;
for I := Length(Result) downto 1 do
if Result = '''' then Insert('''', Result, I);
Result := '''' + Result + '''';
end;
把单个单引号替换成两个单引号
并在字符串两边加上单引号
还可以用#39 ,chr()代替单引号
 
uiit方法可以。
 
Quotedstr(Trim(edit3.text)))=''''+Trim(edit3.text)+''''
就是给字符串前后加引号
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
467
import
I
I
回复
0
查看
728
import
I
顶部