 |
|
สอบถามปัญหาคำสั่ง SQL ในการเพิ่มข้อมูลเข้าสู่ตาราง 2 ตาราง ในคำสั่ง SQL คำสั่งเดียว |
|
 |
|
|
 |
 |
|
declare @name varchar(50),@lastname varchar(50),@mobile varchar(100),@email varchar(50)
Declare customer Cursor For
select name,lastname,mobile,email from customer
Open customer
Fetch Next From customer into @name,@lastname,@mobile,@email
While @@Fetch_Status = 0
Begin
BEGIN
insert into table1(id,name,lastname) select (select isnull(max(id),0)+1 from table1),@name,@lastname
insert into table2(id,mobile,emaul) select (select isnull(max(id),0)+1 from table2),@mobile,@email
END
Fetch Next From customer into @name,@lastname,@mobile,@email
End
Close customer
Deallocate customer
สร้างตัวแปรมารับค่า ข้อมูลที่ select ออกมา คือ declare @name varchar(50)
แล้ว วนลูป จากนั้นก็เอาข้อมูลที่ได้ ไป Insert ลง Table1 , table2 ที่ต้องการ
ลองดูครับ
|
 |
 |
 |
 |
Date :
2012-05-10 10:38:44 |
By :
thep |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|