 |
|
IF ELSE ใน Stored Procedures ต้องเขียนยังไง รบกวนดูให้หน่อยจ้า |
|
 |
|
|
 |
 |
|
ต้องการให้ sql สร้างเงื่อนไขว่า
1. ถ้าใน ฟิลด์ เงินคืน (tableFund) มีค่า = 0 ในฟิลด์เงินเหลือ ต้องมีค่า เท่ากับเงินที่เติมก่อนหน้านี้
2. ถ้าใน ฟิลด์ เงินคืน (tableFund) มีค่า > 0 ในฟิลด์เงินเหลือ ต้องมีค่า = 0
ยังงงๆ เรื่องการเขียน IF ELSE ใน sql อยู่ ต้องแก้ไขยังไงให้เขียนได้ถูกต้องค่ะ
รบกวนด้วย ขอบคุณล่วงหน้ามากๆค่ะ
Code (ASP)
IF 0=rf.fund
SELECT 0=rf.fund as 'เงินคืน',p.money as 'เงินเหลือ'
From tablePerson p inner join tableFund rf on p.ID=rf.ID
WHERE
CONVERT(nvarchar(10),rf.mDate,126)='2012-05-04'
AND CONVERT(datetime,(Convert(nvarchar(10),rf.mDate,114)),114) BETWEEN '06:00' and '18:00'
ORDER by p.ID asc
END
ELSE IF 0>rf.fund
SELECT 0=rf.fund as 'เงินคืน',p.money as 'เงินเหลือ'
From tablePerson p inner join tableFund rf on p.ID=rf.ID
WHERE
CONVERT(nvarchar(10),rf.mDate,126)='2012-05-04'
AND CONVERT(datetime,(Convert(nvarchar(10),rf.mDate,114)),114) BETWEEN '06:00' and '18:00'
ORDER by p.ID asc
END
Tag : .NET, Ms SQL Server 2005, Ms SQL Server 2008, Web (ASP.NET), C#, VS 2010 (.NET 4.x)
|
|
 |
 |
 |
 |
Date :
2012-07-07 17:08:47 |
By :
porn_toon |
View :
1270 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้พวก CASE WHEN ก็ได้ครับ ถ้ายังงง
แต่ดูใน Code แล้วก็ไม่น่าจะผิดน่ะครับ
ตัวอย่าง
DECLARE @compareprice money, @cost money
EXECUTE Production.uspGetList '%Bikes%', 700,
@compareprice OUT,
@cost OUTPUT
IF @cost <= @compareprice
BEGIN
PRINT 'These products can be purchased for less than
$'+RTRIM(CAST(@compareprice AS varchar(20)))+'.'
END
ELSE
PRINT 'The prices for all products in this category exceed
$'+ RTRIM(CAST(@compareprice AS varchar(20)))+'.'
|
 |
 |
 |
 |
Date :
2012-07-08 07:36:15 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากคะ จะลองทำดูนะคะ 
|
 |
 |
 |
 |
Date :
2012-07-08 14:35:31 |
By :
porn_toon |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2012-07-08 16:16:18 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|