How could I write CASE Clause in sql server 2005?
How could i write CASE Clause in sql server 2005 ? it errors Here is my code :
CASE @accesslevel
WHEN 'Order' THEN
INSERT INTO Permissions(UserAccountID,PrintOrder)
VALUES(@userid,1)
WHEN 'Cashier' THEN
INSERT INTO Permissions(UserAccountID,PrintInvoice,SaveAndClear)
VALUES(@userid,1,1)
WHEN 'Supervisor' THEN
INSERT INTO Permissions(UserAccountID,TableOperation,
ExchangeRate,SaleReport,Section,Category,
MenuItem,DeleteOrder,DeleteOneItem,MergeTable,
SplitTable,PrintInvoice,PrintOrder,CalculateChange,
SaveAndClea)
VALUES(@userid,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
WHEN 'Manager' THEN
INSERT INTO Permissions(UserAccountID,TableOperation,
ExchangeRate,SaleReport,Section,Category,
MenuItem,DeleteOrder,DeleteOneItem,MergeTable,
SplitTable,PrintInvoice,PrintOrder,CalculateChange,
SaveAndClear,DailyIncome)
VALUES(@userid,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
ELSE
INSERT INTO Permissions(UserAccountID,TableOperation,
ExchangeRate,TablePicture,SaleReport,DailyIncome,
Section,Category,MenuItem, UserAccount,UserPermission,
StaffManagement,DeleteOrder,DeleteOneItem,MergeTable,
SplitTable开发者_高级运维,PrintInvoice,PrintOrder,CalculateChange,
SaveAndClear)
VALUES(@userid,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
END CASE
I don't think you can use CASE
statement to execute some actions (inserts, updates...), it's used to calculate expressions. You can use IF..ELSE
statement:
IF @accesslevel = 'Order'
INSERT INTO Permissions(UserAccountID,PrintOrder)
VALUES(@userid,1)
ELSE IF @accesslevel = 'Cashier'
INSERT INTO Permissions(UserAccountID,PrintInvoice,SaveAndClear)
VALUES(@userid,1,1)
ELSE IF @accesslevel = 'Supervisor'
INSERT INTO Permissions(UserAccountID,TableOperation,
ExchangeRate,SaleReport,Section,Category,
MenuItem,DeleteOrder,DeleteOneItem,MergeTable,
SplitTable,PrintInvoice,PrintOrder,CalculateChange,
SaveAndClea)
VALUES(@userid,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
ELSE IF @accesslevel = 'Manager'
INSERT INTO Permissions(UserAccountID,TableOperation,
ExchangeRate,SaleReport,Section,Category,
MenuItem,DeleteOrder,DeleteOneItem,MergeTable,
SplitTable,PrintInvoice,PrintOrder,CalculateChange,
SaveAndClear,DailyIncome)
VALUES(@userid,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
ELSE
INSERT INTO Permissions(UserAccountID,TableOperation,
ExchangeRate,TablePicture,SaleReport,DailyIncome,
Section,Category,MenuItem, UserAccount,UserPermission,
StaffManagement,DeleteOrder,DeleteOneItem,MergeTable,
SplitTable,PrintInvoice,PrintOrder,CalculateChange,
SaveAndClear)
VALUES(@userid,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
CASE expressions are for use inside SQL Server Expressions, like:
SELECT CASE WHEN 1 = 1 THEN 'Yes' ELSE 'No' END
What you are trying to do above (conditionally executing statements or blocks of statements) should be done with IF
/ ELSE
(optionally using BEGIN
and END
to enclose multiple statements).
We often/usually say "Case Statement", but actually the correct term is "Case Expression", as they are used within expressions, within statements (msdn ref: http://msdn.microsoft.com/en-us/library/ms181765.aspx).
精彩评论