CREATE FUNCTION LAG
(
-- Add the parameters for the function here
@table VARCHAR(20), --表名
@p1 char, --列名
@p2 char --条件值
)
RETURNS TABLE
AS
RETURN
(
-- Add the SELECT statement with parameter references here
SELECT @p1 FROM dbo.@table WHERE @[email protected]
)
GO
[email protected],[email protected]@table
[email protected],@table,
比如:SELECT @p1 FROM dbo.@table WHERE @[email protected]
select ID from dbo.Users where [email protected]
则是可以.
但这样的话,就不能做到自定义函数通用
请教大师们,有没有办法实现 SELECT @p1 FROM dbo.@table WHERE @[email protected]
------解决方案--------------------
- SQL code
exec 'SELECT [email protected]+' FROM [email protected]+' WHERE [email protected][email protected]+''
------解决方案--------------------
------解决方案--------------------
- SQL code
CREATE PROC p_testasSELECT GETDATE() agoCREATE VIEW v_testAS SELECT * FROM OPENROWSET('SQLOLEDB.1','server=FI6\SQL2008;uid=sa;pwd=123','exec test11.dbo.p_test')goSELECT * FROM V_TEST/*a2009-09-01 11:08:30.197*/CREATE FUNCTION FN_TEST()RETURNS TABLE AS RETURN( SELECT * FROM OPENROWSET('SQLOLEDB.1','server=FI6\SQL2008;uid=sa;pwd=123','exec test11.dbo.p_test'))goSELECT * FROM FN_TEST()/*a2009-09-01 11:08:30.197*/本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/htl258/archive/2009/09/10/4539388.aspx