如何在t-sql中编写select case语句?我的下面的代码不起作用,帮助 我只想要匹配大小写,插入语句开始将值插入表中。
SET @DayName = dbo.GetWeekDayNameOfDate(@SaleDate)
SELECT
CASE @DayName
WHEN 'Sunday' THEN
INSERT INTO Tmp_Data(ProductID,ProductName,Sunday,NetProfit)
VALUES(@ProductId,@ProductName,@Qty,@Profit);
WHEN 'Monday' THEN
INSERT INTO Tmp_Data(ProductID,ProductName,Monday,NetProfit)
VALUES(@ProductId,@ProductName,@Qty,@Profit);
WHEN 'Tuesday' THEN
INSERT INTO Tmp_Data(ProductID,ProductName,Tuesday,NetProfit)
VALUES(@ProductId,@ProductName,@Qty,@Profit);
WHEN 'Wednesday' THEN
INSERT INTO Tmp_Data(ProductID,ProductName,Wednesday,NetProfit)
VALUES(@ProductId,@ProductName,@Qty,@Profit);
WHEN 'Thursday' THEN
INSERT INTO Tmp_Data(ProductID,ProductName,Thursday,NetProfit)
VALUES(@ProductId,@ProductName,@Qty,@Profit);
WHEN 'Friday' THEN
INSERT INTO Tmp_Data(ProductID,ProductName,Friday,NetProfit)
VALUES(@ProductId,@ProductName,@Qty,@Profit);
WHEN 'Saturday' THEN
INSERT INTO Tmp_Data(ProductID,ProductName,Saturday,NetProfit)
VALUES(@ProductId,@ProductName,@Qty,@Profit);
END
答案 0 :(得分:8)
我同意@John Dewey的评论 - 你不应该在每个工作日都有一个专栏,而是一个工作日的专栏。假设您无法修复损坏的模式,接下来您需要了解CASE
是一个返回值的表达式,而不是像其它语言一样的流语句控件。这意味着你不能说CASE THEN something THEN go do something else
。
这是编写语句的一种方法,它将至少减少您正在尝试的一些冗余:
SET @DayName = dbo.GetWeekDayNameOfDate(@SaleDate);
INSERT dbo.Tmp_Data(ProductID,ProductName,NetProfit,
Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday)
SELECT @ProductId,@ProductName,@Profit,
CASE @DayName WHEN 'Sunday' THEN @Qty END,
CASE @DayName WHEN 'Monday' THEN @Qty END,
CASE @DayName WHEN 'Tuesday' THEN @Qty END,
CASE @DayName WHEN 'Wednesday' THEN @Qty END,
CASE @DayName WHEN 'Thursday' THEN @Qty END,
CASE @DayName WHEN 'Friday' THEN @Qty END,
CASE @DayName WHEN 'Saturday' THEN @Qty END;
您还可以在动态SQL中构建语句。不是我喜欢的方式,只是为了简洁而包含该选项。
SET @DayName = dbo.GetWeekDayNameOfDate(@SaleDate);
DECLARE @sql NVARCHAR(4000);
SET @sql = N'INSERT dbo.Tmp_Data(ProductID,ProductName,NetProfit,'
+ @DayName + ') SELECT '
+ CONVERT(VARCHAR(12), @ProductId) +','
+ '''' + @ProductName + ''','
+ CONVERT(VARCHAR(12), @Profit) + ','
+ CONVERT(VARCHAR(12), @Qty) + ';';
PRINT @sql;
-- EXEC sp_executesql @sql;
答案 1 :(得分:-4)
请试一试。
SET @DayName = dbo.GetWeekDayNameOfDate(@SaleDate)
SELECT
CASE WHEN @DayName = 'Sunday' THEN
INSERT INTO Tmp_Data(ProductID,ProductName,Sunday,NetProfit)
VALUES(@ProductId,@ProductName,@Qty,@Profit);
WHEN @DayName = 'Monday' THEN
INSERT INTO Tmp_Data(ProductID,ProductName,Monday,NetProfit)
VALUES(@ProductId,@ProductName,@Qty,@Profit);
WHEN @DayName= 'Tuesday' THEN
INSERT INTO Tmp_Data(ProductID,ProductName,Tuesday,NetProfit)
VALUES(@ProductId,@ProductName,@Qty,@Profit);
WHEN @DayName= 'Wednesday' THEN
INSERT INTO Tmp_Data(ProductID,ProductName,Wednesday,NetProfit)
VALUES(@ProductId,@ProductName,@Qty,@Profit);
WHEN @DayName= 'Thursday' THEN
INSERT INTO Tmp_Data(ProductID,ProductName,Thursday,NetProfit)
VALUES(@ProductId,@ProductName,@Qty,@Profit);
WHEN @DayName= 'Friday' THEN
INSERT INTO Tmp_Data(ProductID,ProductName,Friday,NetProfit)
VALUES(@ProductId,@ProductName,@Qty,@Profit);
WHEN @DayName= 'Saturday' THEN
INSERT INTO Tmp_Data(ProductID,ProductName,Saturday,NetProfit)
VALUES(@ProductId,@ProductName,@Qty,@Profit);
END