求sql语句(100分)

  • 主题发起人 主题发起人 美国提子
  • 开始时间 开始时间

美国提子

Unregistered / Unconfirmed
GUEST, unregistred user!
Id no Name Qty
1 01 黄色 20
2 03 黑色 10
3 01 红色 60
4 04 红色 45
5 01 黑色 20
求一sql语句
要求,例如:将no='01'的所有记录以Name+Qty+'%,'的形式相加存入一字符串变量
以上记录的字符串变量的结果就是'黄色20%,红色60%,黑色20%'
 
select name||qty||'%' from table
where no='01';
 
呵呵,试完马上给分
[?]值怎么付给变量呀,
 
用存储过程写程序吧,[:D]
 
值怎么付给变量呀
 
存储过程我没写过,你讲的详细一点好吗?[:D]
 
Select no ,name ,qty,
( Name+Qty+'%') as NewField
from TableName
where no='01'
 
to withdraw
此符 || sql 中好象没有此吧

to 美国提子
你说的是ms sql sevrer 吗?
 
要不在存储过程里写,要不把数据读到前台来用Delphi写
 
同意楼上!
 
to lvxq
你只是将本条记录相加了
 
我用的是access,在下比较不开窍,各位能不能拿出具体的东东来
 
select name+qty+'%' from table
where no='01'

取结果的时候拼出要求的串,再赋给变量。
 
不妨建一存儲過程(語句可參考如下):
Declare @str varchar(225),@str1 Varchar(255)
Select @str=''
Declare #cursor1 cursor for select Name+Convert(varchar,Qty) from tablename where no='01' order by id
Open #cursor1
Fetch Next from #cursor1 into @str1
While @@Fetch_status=0
Begin
Select @str=@str+@str1+'%,'
Fetch Next from #cursor1 into @str1
End
Close #cursor1
Deallocate #cursor1
Print @str
//@str即為所求字串
 
你还是在delphi 中写吧

 
Delphi怎么下啊,这么写吗?我是这么写的,效率太低了,也太土了
procedure TpfEditForm.ppLabel22Print(Sender: TObject);
var
CaptionStr,WhereStr: String;
begin
CaptionStr := '';
WhereStr := 'where Color.ColorID=Color_percent.ColorID and No ='''+DBEdtNo.Text+'''';
with adoqryPrint do
begin
Close;
SQL.Clear;
SQL.Add('select * from color_percent');
SQL.Add(WhereStr);
Open;

while Not eof do
begin
CaptionStr := CaptionStr+adoqryPrint['ColorName']+FloatToStr(adoqryPrint['percent'])+'% ';
Next;
end;

ppLabel22.Caption := CaptionStr;
end;
end;
 
谁说的,已经把所有no='01'的纪录做了呀
你到底是什么意思,不光01是吗
那就全部吧,只要把Where 条件去了就行
 
已经全部通过验证:只要拷过去在sql2000上运行一下就行了
use test
go
create table test
(
[id] int not null primary key,
[no] char(4) not null ,
[name] varchar(10) not null,
Qty int not null
)
go
select * from dbo.test
go
insert into test values(1,'01','黄色',20)
insert into test values(2,'03','黑色',10)
insert into test values(3,'01','红色',60)
insert into test values(4,'04','红色',45)
insert into test values(5,'01','黑色',20)
go
select [name]+cast(Qty as varchar(5)) +'%' as test
from dbo.test
where [no]='01'


declare @test1 varchar(20),@test2 varchar(20)
declare cursor_name cursor
for
select [name]+convert(varchar(5),Qty)+'%' as char_id
from dbo.test
where [no]='01'
open cursor_name
fetch next from cursor_name into @test1
while @@FETCH_STATUS=0
begin
fetch next from cursor_name into @test2
select @test2=','+@test2
select @test1=@test1+@test2
end
print @test1
close cursor_name
deallocate cursor_name

 
多人接受答案了。
 

Similar threads

D
回复
0
查看
934
DelphiTeacher的专栏
D
D
回复
0
查看
727
DelphiTeacher的专栏
D
D
回复
0
查看
688
DelphiTeacher的专栏
D
后退
顶部