DECLARE @TABLE TABLE
( field varchar(70),
[enabled] int)
DECLARE @SELECT VARCHAR(MAX)
SET @SELECT =
INSERT INTO @TABLE VALUES ( Name ,1)
INSERT INTO @TABLE VALUES ( Age ,0)
INSERT INTO @TABLE VALUES ( Weight ,1)
SELECT @SELECT = @SELECT + field + ,
FROM @TABLE
WHERE [enabled] = 1
SET @SELECT = LEFT(@SELECT,LEN(@SELECT)-1) --trim last ,
SET @SELECT = SELECT + @SELECT + FROM MyView
PRINT @SELECT
--EXEC @SELECT
执行
SELECT Name, Weight FROM MyView
抓住充满活力的诽谤者。
采用这样的表格或观点并非微不足道,但首先可以将其归纳为一种储存的程序,从而形成一个连接的服务器机库。
if exists (select * from master..sysservers where srvname = loopback )
exec sp_dropserver loopback
go
exec sp_addlinkedserver @server = N loopback ,
@srvproduct = N ,
@provider = N SQLOLEDB ,
@datasrc = @@servername
go
然后,你可以使用下列辛迪加:
select * FROM openquery(loopback, exec yourSproc )