Groups | Blog | Home
all groups > sql server programming > march 2004 >

sql server programming : better way to make tran


Anith Sen
3/25/2004 5:18:48 PM
You have not provided any details on what you are doing here. If you are
just concerned about the elegance of the code, you can avoid the superfluous
BEGIN & END around single statements.

--
Anith

LiFo
3/25/2004 11:16:17 PM
hi i have made this transaktion not in the DB but just as a sql query

it is working fine but i was just wondering if it could bee done more
elegant ??









Begin tran

declare @la NVARCHAR(32)

declare @lockTable TABLE (ok bit,laastAf nvarchar(32))

select @la = laastaf from kunde

where nummer = "+kundeNummer

if @la is null

begin

begin

update kunde set laastAf='"+laastAf+"'

where nummer = "+kundeNummer

end

begin

insert into @lockTable values(1,'')

end

begin

select * from @lockTable

end

end else

begin

begin

insert into @lockTable values(0,@la)

end

begin

select * from @lockTable

end

end

commit tran

Anith Sen
3/26/2004 10:13:45 AM
[quoted text, click to view]

Your IF..ELSE block can be re-written as :

IF @la IS NULL
BEGIN
UPDATE kunde SET laastAf = ...
INSERT INTO @lockTable VALUES ( 1, '' )
END
ELSE
INSERT INTO @lockTable VALUES ( 0, @la )
SELECT * FROM @lockTable

Also, note that table variables are not affected by the transaction commits
& rollbacks.

--
Anith

LiFo
3/26/2004 4:35:12 PM
i am locking an row by inserting a name in the table
only if there the cell is null

if it is not null the i am returning an Temp table that has the cell value
and an bool value set to false

if it is null then i am updating the cell
and returning an Temp table that has an bool value set to true


u say superfluous begin end
but how do i then execute 3 statments inside one begin end ??

[quoted text, click to view]

AddThis Social Bookmark Button