怎么利用动态拼接的SQL语句的执行结果
如何利用动态拼接的SQL语句的执行结果
执行SQL语句后能正常返回结果集。
但问题是这个结果集怎么使用,比如要实现类似效果:
即动态拼接的SQL执行后的结果集如何使用,谢谢
------解决方案--------------------
- SQL code
--if object_id('CHENGJI') is not null drop table CHENGJI --create table CHENGJI --( -- id int NOT NULL, -- chengji int NOT NULL, -- xingming varchar(30) NOT NULL, -- banji int NOT NULL --) --insert CHENGJI --select 1,90,'张三',14 union --select 2,70,'李四',17 union --select 3,80,'张三',14 union --select 4,60,'张三',14 union --select 5,77,'李四',17 union --select 6,80,'张三',14 union --select 7,90,'李四',17 union --select 8,70,'李四',17 --go declare @Sql varchar(300) declare @chengji int Set @chengji=80 Set @Sql=' Select id,chengji,xingming ,banji From CHENGJI Where chengji='+cast(@Chengji as varchar(10)) Print(@Sql) exec(@Sql)
执行SQL语句后能正常返回结果集。
但问题是这个结果集怎么使用,比如要实现类似效果:
- Delphi(Pascal) code
procedure TForm1.Button1Click(Sender: TObject); begin ADOQuery1.Close; ADOQuery1.SQL.Clear; ADOQuery1.SQL.Add('Select id,chengji,banji,xingming From CHENGJI where chengji=80'); ADOQuery1.Open; Memo1.Lines.Clear; While ADOQuery1.Eof =false do begin Memo1.Lines.Add(ADOQuery1.FieldByName('id').AsString); Memo1.Lines.Add(IntToStr(ADOQuery1.FieldByName('chengji').AsInteger)); Memo1.Lines.Add(ADOQuery1.FieldByName('xingming').AsString); Memo1.Lines.Add(ADOQuery1.FieldByName('banji').AsString); ADOQuery1.Next; end; ADOQuery1.Close; end;
即动态拼接的SQL执行后的结果集如何使用,谢谢
------解决方案--------------------