选择除IDENTITY列以外的所有列

时间:2014-11-17 06:15:48

标签: sql sql-server vbscript

我正在尝试在表格 EXCEPT IDENTITY COLUMN 中显示所有列。 有可能吗?

伪代码就是这样的。

SELECT * FROM Tab1 WHERE IDENTIY = NULL

SELECT * FROM Tab1 WHERE IDENTIY <> 'YES'

这显然无效。但我希望实现这样的目标。 有可能??

PS。我通过VBScript的SQL服务器连接来做这个,我不想硬编码列名(显然是为了灵活性)。

这是VbScript代码段。

SQL_Cols = "select Column_name from Information_schema.columns where Table_name = 'Tab1'"
Recordset.open SQL_Cols,ConnString
p = recordset.getrows()
STR_Cols = ""
for i=1 to Ubound(p,2)   ' from 1 to UBound since in p(0,0) name of identity column is stored   
    if i = Ubound(p,2) Then
        STR_Cols  = STR_Cols  &"["&p(0,i)&"]" 
    else
        STR_Cols  = STR_Cols  &"["&p(0,i)&"],"
    end if
NEXT
Recordset.close

STR_Cols 存储所有列名称(第一个除外(这是一个标识列))

我后来用于执行向下流任务。 这只是一件好事。这仅在第1列是IDENTIY列时才有效,如果表被更改,则必须更改代码。我希望代码尽可能灵活和健壮。

7 个答案:

答案 0 :(得分:4)

尝试这样:

sys.columns =包含有关特定数据库的所有列

sys.tables =包含有关特定数据库的所有表

sys.identity_columns =包含有关特定数据库的所有标识列

所以结果可以由joins生成is_identity=1生成,排除标识列生成Not IN所有列到特定表

select c.name from sys.columns c
join sys.tables AS t
on t.object_id=c.object_id
where c.name not in (select name from sys.identity_columns where is_identity=1)
and  t.name='MyTableName'

答案 1 :(得分:2)

试试这个。使用Sys.columns表获取没有identity column的列列表。然后使用Dynamic SQL生成列列表并执行查询

declare @ collist varchar(max)='',@ sql nvarchar(max)
select @collist += ','+name from sys.columns where object_name(object_id)='Tab1' and is_identity <> 1
select @collist = right(@collist,len(@collist)-1)
set @sql ='select '+@collist+ ' from Tab1'
exec sp_executesql @sql

答案 2 :(得分:1)

试试这个

SELECT * INTO #TempTable FROM YourTable

/* Drop the IDENTITY cloumn */
ALTER TABLE #TempTable DROP COLUMN ID_COLUMN

/* SELECT New result and drop temp table */
SELECT * FROM #TempTable 
DROP TABLE #TempTable

答案 3 :(得分:0)

不要指定不需要的列名。而不是指定*指定所需的列名。

SELECT Col1, Col3, Col6, Col8 FROM Tab1   

此处Col1, Col3, Col6, Col8 are selected and Col2, Col4, Col5 and Col7从选择查询中排除。

答案 4 :(得分:0)

您将通过执行以下查询来获取标识列:

    SELECT
    clmns.name
    FROM
    sys.tables AS tbl
    INNER JOIN sys.all_columns AS clmns ON clmns.object_id=tbl.object_id
    LEFT OUTER JOIN sys.indexes AS ik ON ik.object_id = clmns.object_id and 1=ik.is_primary_key
    LEFT OUTER JOIN sys.index_columns AS cik ON cik.index_id = ik.index_id and cik.column_id = clmns.column_id and cik.object_id = clmns.object_id and 0 = cik.is_included_column
    where tbl.name = N'Tab1' AND is_identity = 1

答案 5 :(得分:0)

这是一个与普拉迪普建议相同的解决方案:

SQL_Cols = "declare @collist varchar(max)=''; select @collist += ', ' + name from sys.columns where object_name(object_id) = 'Tab1' and is_identity <> 1; select @collist = right(@collist,(case when len(@collist) > 2 then len(@collist)-2 else @collist end)); select @collist as [ColumnNames];"
Recordset.open SQL_Cols,ConnString
p = recordset.getrows()
STR_Cols = p(0,0)

检查STR_Cols

的值

答案 6 :(得分:0)

尝试以下查询:我发现此查询正常运行。这将选择没有标识= 1的列中的数据

DECLARE @Temp NVARCHAR(MAX); 
DECLARE @SQL NVARCHAR(MAX);

SET @Temp = '';
SELECT @Temp = @Temp + COLUMN_NAME + ', ' FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME ='Person' AND  columnProperty(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') != 1 

SET @SQL = 'SELECT ' + SUBSTRING(@Temp, 0, LEN(@Temp)) +' FROM [Person]';
EXECUTE SP_EXECUTESQL @SQL;

您可以从sys.columns表中获取列名详细信息

尝试以下查询:

SELECT * FROM SYS.COLUMNS 
WHERE object_id = OBJECT_ID('dbo.TableName') 
AND [Name] <> 'ColumnName'

DECLARE @sql as VARCHAR(8000)
SET @sql = 'SELECT '

SELECT @sql += [Name] + ', ' FROM SYS.COLUMNS 
WHERE object_id = OBJECT_ID('dbo.TableName') 
AND [Name] <> 'ColumnName'

SELECT @sql += ' FROM Dbo.TableName'

EXEC(@sql)