[英]SQL counting total rows from temporary table in a table valued function
如果@total
等於1,我需要在@t
插入的@total
。
我怎樣才能做到這一點?
ALTER FUNCTION [dbo].[myfunction]
(
@ID int = NULL,
@years int = NULL,
,@total BIT = 0
)
RETURNS @t TABLE (
RowNum int,
ID int,
years int,
)
AS
BEGIN
INSERT INTO @t
SELECT
ROW_NUMBER() OVER(ORDER BY years) AS RowNum,
ID,
years,
FROM dbo.mytable
WHERE ..
RETURN
END
結果的外觀應為:
Total RowNum ID year
-------------------------------------
3 1 101 2014
3 2 102 2015
3 3 103 2016
謝謝!
RETURNS @t TABLE (
Total int,
RowNum int,
ID int,
years int,
)
...
INSERT INTO @t
SELECT
NULL,
ROW_NUMBER() OVER(ORDER BY years) AS RowNum,
ID,
years,
FROM dbo.mytable
WHERE ..
...
IF(@total=1) BEGIN
DECLARE @Count INT
SELECT @Count=COUNT(*) FROM @t
UPDATE @t SET Total=@Count
END
RETURN
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.