由于业务需要,我需要在存储过程中由传入的ID值,先读取一个表中的数据,然后将读取到的几个字段的值作为新记录插入到另一个表中。比如订单记录中有客户的资料,我将此资料作为新客户资料插入到客户表中。
简单的说:就是如何在存储过程中读取一个表中的数据,并取得这些数据后进行一系列的处理。
最好有详细的代码,我在存储过程方面是新手。。。
CREATE PROCEDURE insertSelectedResult
@StudentID int
AS
BEGIN
SET NOCOUNT ON;
insert dbo.T_ClientInfo(ID, ClientName)
select StudentID AS ID, StudentName as ClientName
from dbo.T_Student
where StudentID=@StudentID
END
GO
两张表:
T_ClientInfo:
T_Student:
1、说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用)
法一:select * into b from a where 1<>1
法二:select top 0 * into b from a
2、说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)
insert into b(a, b, c) select d,e,f from b;
3、说明:跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用)
insert into b(a, b, c) select d,e,f from b in ‘具体数据库’ where 条件
例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where..
CREATE PROCEDURE UP_order_SameCustomer
@oid int, @mid int output
as
begin
INSERT INTO [member]( [memberName],[jacketLength],[chest],[waist],[bottomHips],[shoulderWidth],[neckWidth],[sleeveLength],[muscle],[cuffOpenning],[bp],[np],[fc],[bc],[sfw],[sbw],[pantsOutseam],[seat],[hipsHight],[thigh],[bottom],[crotchLength] )
select [customerName] as memberName,[jacketLength],[chest],[waist],[bottomHips],[shoulderWidth],[neckWidth],[sleeveLength],[muscle],[cuffOpenning],[bp],[np],[fc],[bc],[sfw],[sbw],[pantsOutseam],[seat],[hipsHight],[thigh],[bottom],[crotchLength]
from [order]
where [oid]=@oid
SET @mid = @@IDENTITY
update [order] set [memberId]=@mid
where [oid]=@oid
end