这个存储过程怎么写???(50分)

  • 主题发起人 主题发起人 kongfei
  • 开始时间 开始时间
K

kongfei

Unregistered / Unconfirmed
GUEST, unregistred user!
pid:=project.FieldValues['pid']; //主表project的id号
projectjoiner1.Active:=true;
projectjoiner1.First;
while not projectjoiner1.Eof do
begin
if pid=projectjoiner1.fieldbyname('pid').asinteger then //在从表projectjoiner1中找到符合条件的记录
begin
pname:=trim(projectjoiner1.fieldbyname('name').AsString);
pweight:=trim(projectjoiner1.fieldbyname('weight').AsString);
strlink:=strlink+pname+'('+pweight+')';//把这些记录拼合成字符串
end;
projectjoiner1.Next;
end;
update project //接着更新主表
set 项目承担人=:strlink
where pid=:pid
这个过程我想用存储过程实现,可是不知道怎么写,谁能帮帮忙。
 
Interbase写法:
create procedure proc(PID INTEGER)
AS
DECLARE VARIABLE Name STRING;
DECLARE VARIABLE Weight STRING;
DECLARE VARIABLE Strlink STRING;
BEGIN
SELECT NAME,WEIGHT FROM ProjectJoiner1
WHERE PID=:PID
INTO :NAME,:WEIGHT;
Strlink=TRIM(Name) || "(" || TRIM(Weight) || ")" ;
UPDATE PROJECT
SET 项目承担人=:strlink
WHERE PID=:PID;
END;
 
create procedure proc
(@PID INT)
AS
DECLARE @Name varchar(50)
DECLARE @Weight varchar(50)
DECLARE @Strlink varchar(1000)
declare mycursor cursor for
SELECT NAME,WEIGHT FROM ProjectJoiner1
WHERE PID=@PID
open mycursor
fetch next mycursor INTO @NAME,@WEIGHT
while (@@fetch_status=0)
begin
set @Strlink=@strlink+@name+'('+@weight+')'
fetch next mycursor INTO @NAME,@WEIGHT
end
close mycusor
deallacte mycursor
UPDATE PROJECT
SET 项目承担人=@strlink
WHERE PID=@PID

 
谢谢两位了我用的数据库是sql server2000
根据pengjinlongex 所写我改写成下面这个样子可还是不行,不能更换新project表
create procedure get_projectjoiner
(@pid int)
AS
DECLARE @name varchar(50)
DECLARE @weight varchar(50)
DECLARE @strlink varchar(1000)
declare mycursor cursor for
SELECT name,weight FROM projectjoiner
WHERE pid=@pid
open mycursor
while (@@fetch_status=0) begin
fetch next from mycursor into @name,@weight
if(@@fetch_status<>0) break
set @strlink=@strlink+@name+'('+@weight+')'
end
close mycursor
deallocate mycursor
UPDATE PROJECT
SET 项目承担人=@strlink
WHERE pid=@pid
GO
 
>>while (@@fetch_status=0) begin
>>fetch next from mycursor into @name,@weight --这句应写在后面
>>if(@@fetch_status<>0) break --画蛇添足
>> set @strlink=@strlink+@name+'('+@weight+')'
>>end

建议在while语句前加上:
set @strlink = ''

表不能更新的原因可能是@strlink的长度不够.
 
基本上是可以了,不过name和weight都得去空格,用trim还不行,还有什么函数吗?
 
/*没那么复杂,这只是思路,你可以朝着这方面想*/
Create Proc good(@Pid char(10),@table char(10))
As
Declare @Pname Char(20)
Declare @PWeight Char(30)
Declare @PstrLink Varchar(50)
Begin
select pid,@Pname=name,@PWeight=weight From @table where Pid=@pid
Set @PstrLink=@PstrLink+@Pname+@PWeight
Update @table set 项目承担人=@Pstrlink Where Pid=@pid
end;
/*@table 是表名,@Pid是关键字段*/
/*调用是只需 */
good '123',hello
 
我没用过SQL 2000, 但我知道SQL 7中是不能象楼上那样写。
trim在SQL中是分成了两个函数:ltrim()、rtrim(),分别是去左边的空格和去右边的空格。
 
ok,问题已经解决,不过新的问题又出现了,速度太慢。。。。。。。
 
后退
顶部