create proc proc_test @id int as begin tran insert into tb select *from ta where id=@id if @@error=0 update tc set status='Y' else rollbackcommit
sqlserver的FOR 循环:(1+2+3....+100)declare @row int declare @total int select @row=1 while @row<=100 begin select @total=isnull(@total,0)+@row select @row=@row+1 end print @total
譬如:如果还要判断ta中的A字段为 4 的时候,才插入,这个条件怎么加。
insert into tb select *from ta where id=@id and a=4
create proc proc_test @id int as begin tran insert into tb select *from ta where id=@id and a=4 if @@error=0 update tc set status='Y' else rollbackcommit
Create proc P_Test @ID Int As Declare @Result Int Begin Transaction Insert Into TestA select * from TestB Where ID= @ID Set @Result = @@error If @Result > 0 RollBack Else Commit Update TableC Set State = 1 Go
有条件就用if,要循环就用while.
我的意思,现在还要对从表ta中取出来的 字段a进行判断,如果a=4 则进行相关操作,不能于4又进行另外一种操作。如果直接insert into tb select *from ta where id=@id 这样就直接 插入到tb中去了,那么我想根据ta中a字段的值进行相关的操作就无法做了
insert into tb select *from ta where id=@id and a=4 --这种你就把a=4以外的所有值都排除去了,我的意思要根据a不同的值进行不同的操作所以 直接insert into这种方式行不通
直接用一个insert into 。select语句来实现第一步操作不就可以了吗?
老大,你看下上面的回复啊 我要根据 select出来的值,去判断进行其他操作啊 如果直接insert into 。select * from ta 取不出来 ta中的值,这样就一股脑的全insert 进去u了。
insert into B select * from A where id='1'不用循环
@id int
as
begin tran
insert into tb
select *from ta where id=@id
if @@error=0
update tc set status='Y'
else
rollbackcommit
declare @total int
select @row=1
while @row<=100
begin
select @total=isnull(@total,0)+@row
select @row=@row+1
end
print @total
譬如:如果还要判断ta中的A字段为 4 的时候,才插入,这个条件怎么加。
select *from ta where id=@id and a=4
@id int
as
begin tran
insert into tb
select *from ta where id=@id and a=4
if @@error=0
update tc set status='Y'
else
rollbackcommit
@ID Int
As
Declare @Result Int
Begin Transaction
Insert Into TestA
select * from TestB Where ID= @ID
Set @Result = @@error
If @Result > 0
RollBack
Else
Commit
Update TableC Set State = 1
Go
我的意思,现在还要对从表ta中取出来的 字段a进行判断,如果a=4 则进行相关操作,不能于4又进行另外一种操作。如果直接insert into tb
select *from ta where id=@id
这样就直接 插入到tb中去了,那么我想根据ta中a字段的值进行相关的操作就无法做了
insert into tb
select *from ta where id=@id and a=4
--这种你就把a=4以外的所有值都排除去了,我的意思要根据a不同的值进行不同的操作所以 直接insert into这种方式行不通
我要根据 select出来的值,去判断进行其他操作啊
如果直接insert into 。select * from ta 取不出来 ta中的值,这样就一股脑的全insert 进去u了。