參考引用:自訂函數裏的select table名稱可否為變數?
--
=====================================
ALTER FUNCTION [dbo].[V_GettableXXX]
(
@T_tablename varchar(15) ,
@T_year varchar(4)
)
RETURNS table
AS
BEGIN
DECLARE @tableXXX varchar(25)
set @tableXXX=@T_tablename+@T_year
EXEC ('select * from ' + @tableXXX)
END
底下提供我測試的寫法。回傳值的部份我並未處理與測試,請各位自行嚐試囉!!
DECLARE @tablename AS NVARCHAR(50)
SET @tablename = 'aEnrichRPTDB.dbo.dimDate'
EXEC ('SELECT * FROM ' + @tablename)
沒有留言:
張貼留言