全部博文(788)
分类:
2008-12-11 10:11:15
10~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~分?!
不懂行情,一般这样的问题该多少分?
if exists(select 1 from sysobjects where id = object_id(N'testTable'))
drop table testTable
create table testTable
(id int primary key identity(1,1),
str1 varchar(1),
str2 varchar(1),
str3 varchar(1),
addstr varchar(100)
)
insert into testTable values('Z','L','M','')
insert into testTable values('L','M','N','')
insert into testTable values('L','M','N','')
insert into testTable values('L','M','N','')
insert into testTable values('L','M','N','')
insert into testTable values('L','M','N','')
insert into testTable values('L','M','N','')
insert into testTable values('L','M','N','')
insert into testTable values('L','M','T','')
go
alter procedure mytestProcedure
@FCurrentid int
as
declare
@FieldStr varchar(100),
@MaxId int,
@CurrentID int
set @Fieldstr = ''
set @CurrentID = @FCurrentid + 1
select @Maxid = max(id) from testtable
select @FieldStr = @FieldStr + str1 +str2 +str3 from testtable where id <= @FCurrentid
update testtable set addstr = @FieldStr where id = @FCurrentid
while @CurrentID < @MaxID +1
begin
select @FieldStr = right(@FieldStr,len(@FieldStr)-3*len(str1)) + str1+str2+str3 from testtable where id = @CurrentID
update testtable set addstr = @FieldStr where id = @CurrentID
set @CurrentID = @CurrentID + 1
end
go
go
exec mytestprocedure 5
select * from testtable
执行结果
------------------
1 Z L M
2 L M N
3 L M N
4 L M N
5 L M N ZLMLMNLMNLMNLMN
6 L M N LMNLMNLMNLMNLMN
7 L M N LMNLMNLMNLMNLMN
8 L M N LMNLMNLMNLMNLMN
9 L M T LMNLMNLMNLMNLMT
中间的while循环也可以使用游标实现,但效果都差不多,
应该还有更简单的方式可以一句话搞定,但我暂时没想到。
up
下面这种解决方式,代码可能更少些,也更容易懂些,但是,其内部执行的循环次数好像也更多些。
if exists(select 1 from sysobjects where id = object_id(N'testTable'))
drop table testTable
create table testTable
(id int primary key identity(1,1),
str1 varchar(1),
str2 varchar(1),
str3 varchar(1),
addstr varchar(100)
)
insert into testTable values('Z','L','M','')
insert into testTable values('L','M','N','')
insert into testTable values('L','M','N','')
insert into testTable values('L','M','N','')
insert into testTable values('L','M','N','')
insert into testTable values('L','M','N','')
insert into testTable values('L','M','N','')
insert into testTable values('L','M','N','')
insert into testTable values('L','M','T','')
go
if exists(select 1 from sysobjects where id = object_id(N'myfunction'))
drop function myfunction
go
--自定义函数,根据传递的创始化id(就是从第几行开始),和当前id来返回一个当前id前面几行的一个字符串;
create function myfunction (@FinitID int,@FCurrID int)
returns varchar(100)
as
begin
declare
@FieldStr varchar(100)
set @Fieldstr = ''
select @FieldStr = @FieldStr + str1 +str2 +str3
from testtable
where id between (@FCurrID - @FinitID +1) and @FCurrID
return(@FieldStr)
end
go
update testtable set addstr = test.dbo.myfunction(5,id) from testtable where id >4
select * from testtable
CREATE FUNCTION AddColumn (@ID INT)
RETURNS NVARCHAR(4000) AS
BEGIN
Declare @SQL NVARCHAR(4000)
Declare @ROW INT
Select @SQL = ''
Select @ROW = COUNT(1) FROM (Select Top 5 * from TADD Where ID <= @ID Order By ID Desc) T
IF @ROW = 5
Begin
Select @SQL = @SQL + Str From (Select Top 5 Str1+Str2+Str3 Str from (Select Top 5 * from TADD Where ID <= @ID Order By ID Desc ) T Order By ID ) TT
End
Return @SQL
END
-----------------------------
Select ID, STR1, STR2, STR3, DBO.AddAtr(ID) ADDSTR FROM TADD
Select ID, STR1, STR2, STR3, DBO.AddColumn(ID) ADDSTR FROM TADD
谢谢几位的解答,不过,这和我的目的还是有些差距,可能我没有说清楚,我是想数据库不动,在DELPHI的ADOTABLE中添加一个字段,在OnCalcFields事件中用什么方法能够将上面的目的达到!
重写VCL
经过不断的实验,自己解决了,就是比较麻烦,不过还是谢谢大家了,给两位回答问题积极的帅哥送点分!!
将解决的办法说一下,TABLE表不动,添加一个计算字段,然后,做一个自定义函数,将ID传给函数,在函数中包含一个动态查询,做一个循环的查询将结果累计,然后返回给TABLE的oncalcfields过程中的计算字段的赋值。