kgojiwong
K歌之王章鱼哥
2017-12-28 07:14

sqlserver,定义一个变量,但返回NULL,'SET @item_id' 导致出错,但不知为何

10
  • select
  • sql

DECLARE @user_id bigint
DECLARE @item_ids varchar(1000)
SET @user_id = 7
SET @item_ids = N'536563,562379'
DECLARE @i INT;
DECLARE @count INT;
DECLARE @price BIGINT;
DECLARE @total_price BIGINT;
DECLARE @temp_price BIGINT;
SET @total_price = 1;
SET @i = 0;
SET @count = (SELECT COUNT(VALUE) FROM string_split(@item_ids,',') );
WHILE (@i < @count)
BEGIN
DECLARE @item_id BIGINT;
SET @item_id = CAST(
(
SELECT ID FROM
(
SELECT CAST(VALUE AS BIGINT) AS ID,ROW_NUMBER() OVER(ORDER BY VALUE) AS RowNum
FROM string_split(@item_ids,',')) AS Idx
WHERE RowNum = @i
)
AS BIGINT
) ;
SET @price = (SELECT price FROM tb_item WHERE id = @item_id)
SET @total_price = @total_price + @price
SET @i = @i + 1
END
SELECT @total_price AS TTP

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答

为你推荐

换一换