有一表记录
单位 月份
a 1
b 2,3
v 3,7
d 5,8
e 3,5,7,12
....
想在stringgrid中实现
a b v d e..
1
2
3 (#)
4
5 (#)
6
7 (#)
8
....
比如:e单位有3,5,7,12月份,在stringgrid中用“#”分别标识出来,该如何实现!!!
单位 月份
a 1
b 2,3
v 3,7
d 5,8
e 3,5,7,12
....
想在stringgrid中实现
a b v d e..
1
2
3 (#)
4
5 (#)
6
7 (#)
8
....
比如:e单位有3,5,7,12月份,在stringgrid中用“#”分别标识出来,该如何实现!!!
解决方案 »
- 用同样的SQL语句,在SQL PLUS里可以操作,但在delphi里却要报错。这是为什么?
- 请问这个页面如何用IDHTTP获取内容?
- 用dbchart做统计图,坐标可以是字符串型的吗?
- 使用Delphi调用VB可执行程序的问题,恳请指教!!!!!!
- 关于mouse的hook问题
- 长此以往,CSDN休矣!
- 采用缓存模式的Dataset如何一次性进行所有行的数据合法性校验?
- 請問怎樣用ado連上同一工作組里其它電腦上的sql server
- ListView如何设置选中某行
- 怎样在程序中动态配置连接SQL SERVER的ODBC
- 当更新或插入的记录超过60条时提示“net error 206”错误
- 這麼晚了還有人在嗎?怎麼樣禁止CheckBox的點擊,不要用enabled=false
1
2
3 (#)
4
5 (#)
6
7 (#)
8对这个结构不清楚你的意思.
SQL语句 case when end应该可以实现的.
1,2,3,4...月份列在stringgrid的rows[0]。
好像是个挺简单的问题,就是不知该如何写语句来实想要的功能!!!
case???怎么写哟??
我是delphi初学者。
if exists(select id from sysobjects where id = object_id(N'TestTable')) drop table TestTable
gocreate table TestTable(
FUnit nvarchar(20),
FMonth nvarchar(50)
)
goinsert into TestTable values('a','1')
insert into TestTable values('b','1,3')
insert into TestTable values('c','1,2,5,7,9')
insert into TestTable values('d','1,3,4,5,6,7,8')
insert into TestTable values('e','1,9,10,11,12')
insert into TestTable values('f','1,2,3,4,5,9')
goselect * from TestTable
go
--生成目标表
if exists(select id from sysobjects where id = object_id(N'TempTable')) drop table TempTable
gocreate table TempTable(
FID nvarchar(10)
)
goinsert into TempTable values(1)
insert into TempTable values(2)
insert into TempTable values(3)
insert into TempTable values(4)
insert into TempTable values(5)
insert into TempTable values(6)
insert into TempTable values(7)
insert into TempTable values(8)
insert into TempTable values(9)
insert into TempTable values(10)
insert into TempTable values(11)
insert into TempTable values(12)
goselect * from TempTable
go --循环用游标得到插入数据
declare @Fmonth nvarchar(50),
@Funit nvarchar(10)
declare TestCursor cursor for
select * from TestTable
open TestCursorset nocount offfetch next from TestCursor into @Funit, @FMonthwhile @@Fetch_Status = 0
begin
--插入相应列并更新数据
print(' alter table TempTable add ' + @Funit + ' nvarchar(10) null
go
update TempTable set '+ @Funit+' = ''*'' where charindex(FID,'''+@FMonth+''')> 0
go ')
exec(' alter table TempTable add ' + @Funit + ' nvarchar(10) null
go
update TempTable set '+ @Funit+' = ''*'' where charindex(FID,'''+@FMonth+''')> 0
go ') fetch next from TestCursor into @Funit, @FMonth
endclose TestCursor
Deallocate TestCursorselect * from TempTablecommit tran Test
if exists(select id from sysobjects where id = object_id(N'TestTable')) drop table TestTable
gocreate table TestTable(
FUnit nvarchar(20),
FMonth nvarchar(50)
)
goinsert into TestTable values('a','1')
insert into TestTable values('b','1,3')
insert into TestTable values('c','1,2,5,7,9')
insert into TestTable values('d','1,3,4,5,6,7,8')
insert into TestTable values('e','1,9,10,11,12')
insert into TestTable values('f','1,2,3,4,5,9')
goselect * from TestTable
go
--生成目标表
if exists(select id from sysobjects where id = object_id(N'TempTable')) drop table TempTable
gocreate table TempTable(
FID nvarchar(10)
)
goinsert into TempTable values(1)
insert into TempTable values(2)
insert into TempTable values(3)
insert into TempTable values(4)
insert into TempTable values(5)
insert into TempTable values(6)
insert into TempTable values(7)
insert into TempTable values(8)
insert into TempTable values(9)
insert into TempTable values(10)
insert into TempTable values(11)
insert into TempTable values(12)
goselect * from TempTable
go --循环用游标得到插入数据
declare @Fmonth nvarchar(50),
@Funit nvarchar(10),
@SQLstr nvarchar(1000)
declare TestCursor cursor for
select * from TestTable
open TestCursorset nocount offfetch next from TestCursor into @Funit, @FMonthwhile @@Fetch_Status = 0 -- + char(13) +' go '+ char(13)+ char(13) +' go '
begin
--插入相应列并更新数据
exec(' alter table TempTable add ' + @Funit + ' nvarchar(10) null ' )
exec(' update TempTable set '+ @Funit+' = ''*'' where charindex(FID,'''+@FMonth+''')> 0 ') fetch next from TestCursor into @Funit, @FMonth
endclose TestCursor
Deallocate TestCursorselect * from TempTablecommit tran test
我只是要从表中选出单位所对应的月份,然后在stringgrid中标识一下而已。关键是这(1,2,5,12)这样的情况该如何查找?
exec(' update TempTable set '+ @Funit+' = ''*'' where charindex(FID,'''+@FMonth+''')> 0 ')
但好像如果查询“1”结果就不对???
47522341的方法在找“1”这个数据时把“11”也找了出来,好像是类似于通配符的作用.
java有将逗号前数据强制转换为数值的indexof,不知delphi有否类似的???
我都等了三天了,这问题难道都没人能回答???
急呀!!!
第一条记录就是需要更新StringGrid中的第一列Cell,那么就分割“1”,转换成整数得到1,即得到了行坐标,那么就可以在Cell[1,1]:='#';
第二条记录就是需要更新StringGrid中的第二列Cell,那么就分割“2,3”,转换成整数得到2和3,即得到了两个行坐标2和3,那么就可以得到Cell[1,2]:='#'和Cell[1,3]:='#'
第三条记录……难道很困难吗?
至于cell这个当然知道!
if exists(select id from sysobjects where id = object_id(N'TestTable')) drop table TestTable
gocreate table TestTable(
FUnit nvarchar(20),
FMonth nvarchar(50)
)
goinsert into TestTable values('a','01')
insert into TestTable values('b','01,03')
insert into TestTable values('c','01,02,05,07,09')
insert into TestTable values('d','01,03,04,05,06,07,08')
insert into TestTable values('e','01,09,10,11,12')
insert into TestTable values('f','01,02,03,04,05,09')
goselect * from TestTable
go
--生成目标表
if exists(select id from sysobjects where id = object_id(N'TempTable')) drop table TempTable
gocreate table TempTable(
FID nvarchar(10)
)
goinsert into TempTable values('01')
insert into TempTable values('02')
insert into TempTable values('03')
insert into TempTable values('04')
insert into TempTable values('05')
insert into TempTable values('06')
insert into TempTable values('07')
insert into TempTable values('08')
insert into TempTable values('09')
insert into TempTable values('10')
insert into TempTable values('11')
insert into TempTable values('12')
goselect * from TempTable
go --循环用游标得到插入数据
declare @Fmonth nvarchar(50),
@Funit nvarchar(10),
@SQLstr nvarchar(1000)
declare TestCursor cursor for
select * from TestTable
open TestCursorset nocount offfetch next from TestCursor into @Funit, @FMonthwhile @@Fetch_Status = 0 -- + char(13) +' go '+ char(13)+ char(13) +' go '
begin
--插入相应列并更新数据
exec(' alter table TempTable add ' + @Funit + ' nvarchar(10) null ' )
exec(' update TempTable set '+ @Funit+' = ''*'' where charindex(FID,'''+@FMonth+''')> 0 ') fetch next from TestCursor into @Funit, @FMonth
endclose TestCursor
Deallocate TestCursorselect * from TempTablecommit tran test
转换字符To整型函数:StrToInt、StrToIntDefvar
s1: String;
slTmp: TStrings;
begin
s1 := '1,3,5';
slTmp := TStringlist.Create;
ExtractStrings([','], [], PChar(s1), slTmp);
//得到结果:slTmp[0] := '1', slTmp[1] := '3', slTmp[2] := '5';
end;下次要问就问明白一点,免的人家想帮你都帮不到!
if exists(select id from sysobjects where id = object_id(N'TestTable')) drop table TestTable
gocreate table TestTable(
FUnit nvarchar(20),
FMonth nvarchar(50)
)
goinsert into TestTable values('a','1')
insert into TestTable values('b','1,3')
insert into TestTable values('c','1,2,5,7,9')
insert into TestTable values('d','1,3,4,5,6,7,8')
insert into TestTable values('e','9,10,11,12')
insert into TestTable values('f','1,2,3,4,5,9')
goselect * from TestTable
go
--生成目标表
if exists(select id from sysobjects where id = object_id(N'TempTable')) drop table TempTable
gocreate table TempTable(
FID int
)
goinsert into TempTable values(1)
insert into TempTable values(2)
insert into TempTable values(3)
insert into TempTable values(4)
insert into TempTable values(5)
insert into TempTable values(6)
insert into TempTable values(7)
insert into TempTable values(8)
insert into TempTable values(9)
insert into TempTable values(10)
insert into TempTable values(11)
insert into TempTable values(12)
goselect * from TempTable
go --循环用游标得到插入数据
declare @Fmonth nvarchar(50),
@Funit nvarchar(10),
@SQLstr nvarchar(1000)
declare TestCursor cursor for
select * from TestTable
open TestCursorset nocount offfetch next from TestCursor into @Funit, @FMonthwhile @@Fetch_Status = 0 -- + char(13) +' go '+ char(13)+ char(13) +' go '
begin
--插入相应列并更新数据
print(' alter table TempTable add ' + @Funit + ' nvarchar(10) null ' )
print(' update TempTable set '+ @Funit+' = ''*'' where FID in ('+@FMonth+') ')
exec(' alter table TempTable add ' + @Funit + ' nvarchar(10) null ' )
exec(' update TempTable set '+ @Funit+' = ''*'' where FID in ('+@FMonth+') ') fetch next from TestCursor into @Funit, @FMonth
endclose TestCursor
Deallocate TestCursorselect * from TempTablecommit tran test
-------------
其实就是这句话有用
exec(' update TempTable set '+ @Funit+' = ''*'' where FID in ('+@FMonth+') ')
但好像如果查询“1”结果也对
-----------------