从没有计算列的SQL表中获取所有列

时间:2013-07-18 08:08:21

标签: sql-server-2008 calculated-columns

我正在运行SQL Server 2008并且必须加载没有计算列的表,例如

  

从MY_TABLE中选择(所有没有计算列的列)

这可能吗?如果我需要从加载中取消选择计算列,可能还有什么其他方法?

1 个答案:

答案 0 :(得分:2)

如何使用syscolumns和动态sql中的iscomputed列?

iscomputed (int)

Flag indicating whether the column is computed: 

0 = Noncomputed.

1 = Computed.

这样的东西
CREATE TABLE TADA(
  A INT,
  B INT,
  C AS (A+B)
 );
INSERT INTO TADA (A,B) VALUES (1,2);

DECLARE @TableName VARCHAR(500) = 'TADA'
DECLARE @ColumnNames VARCHAR(MAX)


SELECT @ColumnNames = COALESCE(@ColumnNames + ',','') + col.name
FROM sysobjects obj 
INNER JOIN syscolumns col ON obj.id = col.id 
WHERE obj.name = @TableName
AND col.iscomputed = 0

EXEC ('SELECT ' + @ColumnNames + ' FROM ' + @TableName)

SQL Fiddle DEMO