-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17.FlowControl.sql
More file actions
70 lines (63 loc) · 2.31 KB
/
17.FlowControl.sql
File metadata and controls
70 lines (63 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use DBTEST
-- Conditional branching structure ----------------------------------------------------------
select * from BankCard
select * from CardExchange
-- 1. For card number '6225547854125656'
-- Process withdrawal of 5000, check balance first
-- Display "Withdrawal successful" or "Insufficient balance"
declare @balance money
select @balance =
(select CardMoney from BankCard where CardNo = '6225547854125656')
if @balance >= 5000 -- Allow withdrawal
begin
update BankCard set CardMoney = CardMoney - 5000
where CardNo = '6225547854125656'
insert into CardExchange([CardNo],[MoneyInBank],[MoneyOutBank],[ExchangeTime])
values('6225547854125656',0,5000,GETDATE())
print 'Withdrawal successful'
end
else -- Reject withdrawal
begin
print 'Insufficient balance'
end
-- 2. Query card info, convert status codes 1,2,3,4 to text
-- Also classify users as "Regular" (balance <300,000) or "VIP" (balance >=300,000)
-- Display card number, ID, name, balance, user type and card status
select CardNo as CardNumber, AccountCode as ID, RealName as Name, CardMoney as Balance,
case -- Range comparison simplifies logic
when CardMoney >= 300000 then 'VIP'
else 'Regular'
end UserType,
case CardState -- Equality comparison simplifies logic
when 1 then 'Normal'
when 2 then 'Lost'
when 3 then 'Frozen'
when 4 then 'Closed'
else 'Abnormal'
end Status
from BankCard
inner join AccountInfo on BankCard.AccountId = AccountInfo.AccountId
-- Loop structure (WHILE) --------------------------------------------------------------
-- 1. Print numbers 1-10
declare @i int = 1 -- Initialize counter
while @i <= 10
begin
print @i
set @i = @i+1
end
-- 2. Print multiplication table (Chinese style)
declare @i int = 1
while @i <= 9 -- Outer loop for multiplicand
begin
declare @str varchar(1000) = ''
declare @j int = 1
while @j <= @i -- Inner loop for multiplier
begin
set @str = @str + cast(@i as varchar(1)) + '*' + cast(@j as varchar(1))
+ '=' + cast(@i*@j as varchar(3)) + char(9) -- Tab separator
set @j = @j + 1
end
set @i = @i + 1
print @str
end
-- Common special characters: TAB CHAR(9), LineFeed CHAR(10), CarriageReturn CHAR(13)