CREATE PROCEDURE uspCalcMedian
(
@tablename VARCHAR(50),
@columnname VARCHAR(50),
@result SQL_VARIANT OUTPUT
)
AS
BEGIN
DECLARE @sqlstmt VARCHAR(200)
DECLARE @midCount INT
SET NOCOUNT ON
SET @sqlstmt = 'insert #tempmedian select ' @columnname
' from ' @tablename ' order by 1 asc '
CREATE TABLE #tempmedian (col SQL_VARIANT)
EXEC (@sqlstmt)
DECLARE c_med CURSOR SCROLL FOR SELECT * FROM #tempmedian
SELECT @midCount = ROUND(COUNT(*) * 0.5,0 ) FROM #tempmedian
OPEN c_med
FETCH ABSOLUTE @midCount FROM c_med INTo @result
CLOSE c_med
DEALLOCATE c_med
DROP TABLE #tempmedian
END
(
@tablename VARCHAR(50),
@columnname VARCHAR(50),
@result SQL_VARIANT OUTPUT
)
AS
BEGIN
DECLARE @sqlstmt VARCHAR(200)
DECLARE @midCount INT
SET NOCOUNT ON
SET @sqlstmt = 'insert #tempmedian select ' @columnname
' from ' @tablename ' order by 1 asc '
CREATE TABLE #tempmedian (col SQL_VARIANT)
EXEC (@sqlstmt)
DECLARE c_med CURSOR SCROLL FOR SELECT * FROM #tempmedian
SELECT @midCount = ROUND(COUNT(*) * 0.5,0 ) FROM #tempmedian
OPEN c_med
FETCH ABSOLUTE @midCount FROM c_med INTo @result
CLOSE c_med
DEALLOCATE c_med
DROP TABLE #tempmedian
END