select
syscolumns.name as [Column],
syscolumns.xusertype as [Type],
sysobjects.xtype as [Objtype]
from
sysobjects
inner join
syscolumns on sysobjects.id = syscolumns.id
where sysobjects.xtype = 'u'
and sysobjects.name = 'MyTableName'
order by syscolumns.name
select
syscolumns.name,
syscolumns.colid
from
sysobjects, syscolumns
where
sysobjects.id = syscolumns.id and
sysobjects.xtype = 'u' and
sysobjects.name = '<YOUR_TABLE>'
order by syscolumns.colid
CREATE PROCEDURE [dbo].[Usp_GetColumnName]
@TableName varchar(50)
AS
BEGIN
BEGIN
SET NOCOUNT ON
IF (@TableName IS NOT NULL)
select ORDINAL_POSITION OrderPosition,COLUMN_NAME ColumnName from information_schema.columns
where table_name =@TableName
order by ORDINAL_POSITION
END
END
SELECT st.NAME, sc.NAME, sc.system_type_id
FROM sys.tables st
INNER JOIN sys.columns sc ON st.object_id = sc.object_id
WHERE st.name LIKE '%Tablename%'
Select
ObjectName = Object_Name(Object_ID)
,T.Name
,C.*
,T.*
From
Sys.All_Columns C
Inner Join Sys.Types T On T.User_Type_Id = C.User_Type_Id
Where [Object_ID] = Object_ID('Sys.Server_Permissions')
--Order By Name Asc
Select * From Sys.Types将产生该类型的user_type_id = ID。这在数据库中是唯一的。对于系统数据类型:user_type_id = system_type_id。
declare @Result varchar(max)='
'
select @Result=@Result+''+ColumnName+'
'
from
(
select
replace(col.name, ' ', '_') ColumnName,
column_id ColumnId
from sys.columns col
join sys.types typ on
col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id
where object_id = object_id('tblPracticeTestSections')
) t
order by ColumnId
print @Result
输出
column1
column2
column3
column4
要使用与c#类相同的代码打印表及其列名,请使用以下代码:
declare @TableName sysname = '<EnterTableName>'
declare @Result varchar(max) = 'public class ' + @TableName + '
{'
select @Result = @Result + '
public static string ' + ColumnName + ' { get { return "'+ColumnName+'"; } }
'
from
(
select
replace(col.name, ' ', '_') ColumnName,
column_id ColumnId
from sys.columns col
join sys.types typ on
col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id
where object_id = object_id(@TableName)
) t
order by ColumnId
set @Result = @Result + '
}'
print @Result
输出:
public class tblPracticeTestSections
{
public static string column1 { get { return "column1"; } }
public static string column2{ get { return "column2"; } }
public static string column3{ get { return "column3"; } }
public static string column4{ get { return "column4"; } }
}