English 中文(简体)
页: 1 习惯领域用于创纪录的一套方法
原标题:SQL Server -- Elegant way to use custom fields for record set

I m 寻找从某种角度选择领域的适当方法,在这种观点中,第二个支持表界定了应当选择的领域。

这一观点包含我的所有雇员数据。 限制表载有外地名称,其中提到外地名称,并注明了选择外地的界限。

观点实例:

Name   | Age | Weight
Peter  | 38  | 180
Martha | 25  | 115

限制表例:

Field  | Enabled
Name   | 1
Age    | 0
Weight | 1

...... 在进行询问后,应退还以下数据:

Name   | Weight
Peter  | 180
Martha | 115

关于如何处理这一问题的任何想法?

感谢!

最佳回答
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 ) 
问题回答

没有活力 页: 1

我建议使用客户的制约因素数据来隐瞒数据,在数据库中不这样做。





相关问题
How to write this T-SQL WHERE condition?

I ve got two tables: TableA Col1 Col2 TableB Col3 Col4 I want to join them together: SELECT * from TableA join TableB ON (...) Now, in place of ... I need to write an expression ...

Customer and Order Sql Statement

TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primary key of CustomerID. The ...

Recommended way of querying multiple Versioned tables

Have a win 2003 box with MSSQL 2005 running on it. There is a database which is populated every morning with new/modified SalesOrder made the previous day. The database has several tables: SalesOrder, ...

update duplicate record

I have a table with the following fields Id Name IsPublic i need to write a sql query that updates IsPublic to false where name has a duplicate. Only one of the duplicates should have IsPublic = ...

Define variable to use with IN operator (T-SQL)

I have a Transact-SQL query that uses the IN operator. Something like this: select * from myTable where myColumn in (1,2,3,4) Is there a way to define a variable to hold the entire list "(1,2,3,4)"? ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签