12 Mart 2009, PERŞEMBE
Kontrol edin eğer bir sıra varsa, aksi Ekle
Bir tablodaki bir satır güncelleyen T-SQL saklı bir yordam yazmak istiyorum. Eğer bu satır mevcut değilse, takın. Tüm bu adımları bir işlem tarafından sarılmış.
Bu olmalı bu yüzden rezervasyon sistemi içinatom ve güvenilir. Eğer hareket işlendi eğer doğru döndürmeli ve uçuş rezervasyonu.
EdiyorumYeni T-SQL içinve @@rowcount
nasıl kullanılacağı konusunda emin değilim. Bu şimdiye kadar yazdım. Doğru yolda mıyım? Sizin için kolay bir sorun olduğuna eminim. Teşekkürler
-- BEGIN TRANSACTION (HOW TO DO?)
UPDATE Bookings
SET TicketsBooked = TicketsBooked @TicketsToBook
WHERE FlightId = @Id AND TicketsMax < (TicketsBooked @TicketsToBook)
-- Here I need to insert only if the row doesn't exists.
-- If the row exists but the condition TicketsMax is violated, I must not insert
-- the row and return FALSE
IF @@ROWCOUNT = 0
BEGIN
INSERT INTO Bookings ... (omitted)
END
-- END TRANSACTION (HOW TO DO?)
-- Return TRUE (How to do?)
CEVAP
13 Mart 2009, Cuma
MERGE komut bir göz atın. Bir açıklamada*, *6UPDATE
& DELETE
yapabilirsiniz.
Burada MERGE
kullanarak üzerinde çalışan bir uygulama
- Uçuş bir güncelleme yapmadan önce tam olup olmadığını kontrol eder, ekleme yapar başka.
if exists(select 1 from INFORMATION_SCHEMA.TABLES T
where T.TABLE_NAME = 'Bookings')
begin
drop table Bookings
end
GO
create table Bookings(
FlightID int identity(1, 1) primary key,
TicketsMax int not null,
TicketsBooked int not null
)
GO
insert Bookings(TicketsMax, TicketsBooked) select 1, 0
insert Bookings(TicketsMax, TicketsBooked) select 2, 2
insert Bookings(TicketsMax, TicketsBooked) select 3, 1
GO
select * from Bookings
Ve sonra ...
declare @FlightID int = 1
declare @TicketsToBook int = 2
--; This should add a new record
merge Bookings as T
using (select @FlightID as FlightID, @TicketsToBook as TicketsToBook) as S
on T.FlightID = S.FlightID
and T.TicketsMax > (T.TicketsBooked S.TicketsToBook)
when matched then
update set T.TicketsBooked = T.TicketsBooked S.TicketsToBook
when not matched then
insert (TicketsMax, TicketsBooked)
values(S.TicketsToBook, S.TicketsToBook);
select * from Bookings
Bunu Paylaş:
Objective-C - eğer bir yöntem varsa ko...
kontrol edin eğer bir WordPress kullan...
temp tablo olmadığını kontrol edin eğe...
Eğer özellik özniteliği varsa kontrol ...
Eğer birden çok dize başka bir dize va...