2016-04-11 20 views
1

ben bu sorguyu kullanın ama işe yaramazsa:kullanın fonksiyonu

create FUNCTION getUserCreditPayment 
(
    @resellerCode int, 
    @userCode int, 
    @startDate datetime, 
    @endDate datetime 
) 
RETURNS TABLE AS 
RETURN(
    WITH Directories AS 
    (
     CASE 
      WHEN (@userCode != 0) 
      THEN 
       (SELECT * 
       FROM UserCredit 
       WHERE userCode = @userCode 
        AND date >= @startDate AND date < @endDate 
       UNION ALL 
       SELECT code, date, price* - 1, 
         userCode, userCodeDes, customerUserName, type 
       FROM UserPayment 
       WHERE userCode = @userCode 
        AND date >= @startDate AND date < @endDate) 
      ELSE 
       (SELECT * 
       FROM UserCredit 
       WHERE userCode = @userCode 
        AND date >= @startDate AND date < @endDate 
       UNION ALL 
       SELECT code, date, price* -1, 
         userCode, userCodeDes, customerUserName, type 
       FROM UserPayment 
       WHERE date >= @startDate AND date < @endDate) 
     END 
    ) 
    SELECT * 
    FROM Directories 
) 

cevap

1

ile deneyin bir örnektir

olduğunu
create FUNCTION getUserCreditPayment 
(
    @id int 
) 
RETURNS --this is the table which you will return,Populate it with same schema 
@test table 
(
id int, 
name varchar(max) 
) 
as 
Begin 
if (@userCode != 0) 
begin 
insert into @test 
SELECT * FROM UserCredit where [email protected] and date>[email protected] and date<@endDate 
      UNION ALL SELECT code,date,price*-1,userCode,userCodeDes,customerUserName,type from UserPayment 
      where [email protected] and date>[email protected] and date<@endDate 
end 
else 
insert into @test 
SELECT * FROM UserCredit where [email protected] and date>[email protected] and date<@endDate 
      UNION ALL SELECT code,date,price*-1,userCode,userCodeDes,customerUserName,type from UserPayment 
      where [email protected] and date>[email protected] and date<@endD 

Return 
end 
+0

Bu yanıt BEGIN ve END ile "eski" sözdizimini kullanır ve bu nedenle çok kötü performans gösterecektir ... Çok daha iyi performans gösteren "yeni" inkar edilebilen (ad-hoc) sözdizimiyle aynı değere ulaşamadığınız nadir durumlar var! – Shnugo

+0

İpucu için teşekkürler, güncellenmiş, bazı örnekleri de paylaşır mısınız – TheGameiswar

+0

Merhaba, üzgünüm, yeterince açık değildi. "Çoklu-açıklama-fonksiyonu" olarak çok daha iyi performans gösteren "tek beyan-TVF" demek istedim. Naveen Kumars'ın cevabına bak. Sadece 'RETURNS TABLE AS ...' var ve daha sonra "çıplak" bir sorgu takip ediyor. Hayır "INSERT" no "@ değişkeni" hayır IF-ELSE "... – Shnugo

2

Tablosu Case ve eğer Gerek Yok, hem komut sadece Fark kullanarak OR koşulu

ele alınabilir kullanici_kodu çek Eğer mutiple koşullarını kontrol ve eklemek istediğiniz takdirde

Eğer function..Here değerli Çoklu tablo için gitmeli, bu

create FUNCTION getUserCreditPayment 
(
    @resellerCode int, 
    @userCode int, 
    @startDate datetime, 
    @endDate datetime 
) 
RETURNS TABLE AS 
RETURN(
    WITH Directories AS 
    (

     SELECT * FROM UserCredit where [email protected] and date>[email protected] and date<@endDate 
      UNION ALL SELECT code,date,price*-1,userCode,userCodeDes,customerUserName,type from UserPayment 
      where ([email protected] OR @userCode = 0) and date>[email protected] and date<@endDate 


    ) 
    SELECT * FROM Directories 
) 
+0

Özellikle * inline * TVF sözdizimini (BEGIN ve END olmadan) kullandığınız için, tarafımdan daha fazlası – Shnugo