选择除标识列之外的所有列

[英]Selecting All the columns except IDENTITY Column


I am trying to display all the columns available in the table EXCEPT IDENTITY COLUMN. Is it possible?

我正在尝试显示除身份列之外的表格中的所有列。是可能的吗?

Pseudo code would be something like this.

伪代码是这样的。

SELECT * FROM Tab1 WHERE IDENTIY = NULL

or

SELECT * FROM Tab1 WHERE IDENTIY <> 'YES'

This is obviously invalid. But I want to achieve something like this. Is it possible??

这显然是无效的。但我想实现这样的目标。是否有可能?

PS. I am doing this through SQL server connection from VBScript, and I dont want to hard code the column names( Obviously for flexibility).

PS.我是通过VBScript的SQL server连接完成的,我不想硬编码列名(显然是为了灵活性)。

Here's the VbScript snippet.

这是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 stores all the column names (Except 1st one (which is an identity Column))

STR_Cols存储所有列名(除了第一列(它是标识列))

Which i am later using to perform the down streaming task. This is working absolutely fine only one thing. This would work only if the 1st column is IDENTIY column, if the table is altered, the code will have to be changed. I want the code to be as flexible and robust as possible.

我稍后将使用它来执行下行流任务。这完全没问题,只有一件事。只有当第一列是标识列时,这才会有效,如果表被修改,那么代码就必须被修改。我希望代码尽可能灵活和健壮。

6 个解决方案

#1


3  

try like this:

试试这样:

sys.columns=Contains All Columns regarding to Particular Database

sys。columns=包含与特定数据库相关的所有列

sys.tables=Contains All Tables regarding to Particular Database

sys。表=包含与特定数据库相关的所有表

sys.identity_columns=Contains All identity Columns regarding to Particular Database

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

So Result Can be Generated By joins with is_identity=1 and Exclude Identity column with Not IN with all columns to particular table

因此,可以通过与is_identity=1的连接生成结果,并排除与特定表的所有列不在一起的标识列

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'

#2


2  

Try this. Use Sys.columns table to get the column list without identity column. Then use Dynamic SQL to generate column list and execute the query

试试这个。使用系统。列表以获取无标识列的列列表。然后使用动态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

#3


1  

Try this

试试这个

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

#4


0  

Don't Specify the column name which are not needed. instead of specifying * specify the column name that are needed.

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

SELECT Col1, Col3, Col6, Col8 FROM Tab1   

Here Col1, Col3, Col6, Col8 are selected and Col2, Col4, Col5 and Col7 are excluded from select query.

这里选择Col1、Col3、Col6、Col8,并将Col2、Col4、Col5和Col7排除在select查询之外。

#5


0  

You will get Identity columns by executing following query :

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

    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

#6


0  

here is a solution same as what Pradeep suggested :

这里有一个和Pradeep建议的一样的解决方案:

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)

check value of STR_Cols

检查STR_Cols的价值

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2014/11/17/72025292ebfbcb612511e728898e3fcc.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告