怎样才能取得SQL SERVER 的所有表的字段值。(20分)

  • 主题发起人 主题发起人 蛐蛐
  • 开始时间 开始时间

蛐蛐

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样才能取得SQL SERVER 的所有表的字段值,字段名称 字段类型
如果是DECIMAL 有几位小位等。
 
Pb好象有这个功能,用delphi实现可能要写点程序。
 
什么pb好象,如果不怕麻烦的话,自己从sysobject先取出表名,然后...,反正很麻烦就是了
你最好到ms下个sql2k的中文联机手机来自己看
每个数据库中的系统表

这些表为每个数据库存储数据库级系统信息。

syscolumns sysindexkeys
syscomments sysmembers
sysconstraints sysobjects
sysdepends syspermissions
sysfilegroups sysprotects
sysfiles sysreferences
sysforeignkeys systypes
sysfulltextcatalogs sysusers
sysindexes
 
对啊,这些东西在系统表里面都有记录的
 
去系统表中看一下都有的
 
对于sql server 应该学会用sql server的配套软件SQL 事件探察器
估计你的长进会不小
 
分太少!!!
以下存储过程可以解决你的问题(只需执行sp_AllColumns);
CREATE PROCEDURE sp_AllColumns
AS
DECLARE @full_table_name nvarchar(769)
DECLARE @table_id int
Declare @Table_Name nvarchar(384)
Declare @table_owner nvarchar(384)
Declare @table_Qualifier sysname
Declare @Column_Name nVarchar(384)
Declare @ODBCVer int
Select @table_Name=null,@table_Owner=Null,@Table_Qualifier=Null,@Column_Name=Null,@ODBCVer=2

if @ODBCVer <> 3
select @ODBCVer = 2
if @column_name is null /* If column name not supplied, match all */
select @column_name = '%'
if @table_qualifier is not null
begin
if db_name() <> @table_qualifier
begin /* If qualifier doesn't match current database */
raiserror (15250, -1,-1)
return
end
end
if @table_name is null
begin /* If table name not supplied, match all */
select @table_name = '%'
end
if @table_owner is null
begin /* If unqualified table name */
SELECT @full_table_name = quotename(@table_name)
end
else
begin /* Qualified table name */
if @table_owner = ''
begin /* If empty owner name */
SELECT @full_table_name = quotename(@table_owner)
end
else
begin
SELECT @full_table_name = quotename(@table_owner) +
'.' + quotename(@table_name)
end
end

/* Get Object ID */
SELECT @table_id = object_id(@full_table_name)
if ((isnull(charindex('%', @full_table_name),0) = 0) and
(isnull(charindex('[', @table_name),0) = 0) and
(isnull(charindex('[', @table_owner),0) = 0) and
(isnull(charindex('_', @full_table_name),0) = 0) and
@table_id <> 0)
begin
/* this block is for the case where there is no pattern
matching required for the table name */
SELECT
TABLE_QUALIFIER = convert(sysname,DB_NAME()),
TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
TABLE_NAME = convert(sysname,o.name),
COLUMN_NAME = convert(sysname,c.name),
d.DATA_TYPE,
convert (sysname,case
when t.xusertype > 255 then t.name
else d.TYPE_NAME
end) TYPE_NAME,
convert(int,case
when d.DATA_TYPE in (6,7) then d.data_precision /* FLOAT/REAL */
else OdbcPrec(c.xtype,c.length,c.xprec)
end) "PRECISION",
convert(int,case
when type_name(d.ss_dtype) IN ('numeric','decimal') then /* decimal/numeric types */
OdbcPrec(c.xtype,c.length,c.xprec)+2
else
isnull(d.length, c.length)
end) LENGTH,
SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
d.RADIX,
NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
REMARKS = convert(varchar(254),null), /* Remarks are NULL */
COLUMN_DEF = text,
d.SQL_DATA_TYPE,
d.SQL_DATETIME_SUB,
CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
ORDINAL_POSITION = convert(int,c.colid),
IS_NULLABLE = convert(varchar(254),
substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3)),
SS_DATA_TYPE = c.type
FROM
sysobjects o,
master.dbo.spt_datatype_info d,
systypes t,
syscolumns c
LEFT OUTER JOIN syscomments m on c.cdefault = m.id
AND m.colid = 1
WHERE
o.id = @table_id
AND c.id = o.id
AND t.xtype = d.ss_dtype
AND c.length = isnull(d.fixlen, c.length)
AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
AND o.type <> 'P'
AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
AND c.xusertype = t.xusertype
AND c.name like @column_name
ORDER BY 17
end
else
begin
/* this block is for the case where there IS pattern
matching done on the table name */
if @table_owner is null /* If owner not supplied, match all */
select @table_owner = '%'
SELECT
TABLE_QUALIFIER = convert(sysname,DB_NAME()),
TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
TABLE_NAME = convert(sysname,o.name),
COLUMN_NAME = convert(sysname,c.name),
d.DATA_TYPE,
convert (sysname,case
when t.xusertype > 255 then t.name
else d.TYPE_NAME
end) TYPE_NAME,
convert(int,case
when d.DATA_TYPE in (6,7) then d.data_precision /* FLOAT/REAL */
else OdbcPrec(c.xtype,c.length,c.xprec)
end) "PRECISION",
convert(int,case
when type_name(d.ss_dtype) IN ('numeric','decimal') then /* decimal/numeric types */
OdbcPrec(c.xtype,c.length,c.xprec)+2
else
isnull(d.length, c.length)
end) LENGTH,
SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
d.RADIX,
NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
REMARKS = convert(varchar(254),null), /* Remarks are NULL */
COLUMN_DEF = text,
d.SQL_DATA_TYPE,
d.SQL_DATETIME_SUB,
CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
ORDINAL_POSITION = convert(int,c.colid),
IS_NULLABLE = convert(varchar(254),
rtrim(substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3))),
SS_DATA_TYPE = c.type
FROM
sysobjects o,
master.dbo.spt_datatype_info d,
systypes t,
syscolumns c
LEFT OUTER JOIN syscomments m on c.cdefault = m.id
AND m.colid = 1
WHERE user_name(o.uid) like @table_owner
AND o.id = c.id
AND t.xtype = d.ss_dtype
AND c.length = isnull(d.fixlen, c.length)
AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
AND o.type <> 'P'
AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
AND c.xusertype = t.xusertype
AND c.name like @column_name
ORDER BY 2, 3, 17
end
 
大哥,我没有多少分的。我每天都搞UNIX 下面一套 MRP ,
现在 我看到 /
 
不好意思,
感觉到好陌生的。
分太少,真的对不起。谢谢大家。
 
SQL Server 表在一个特定的时段是否有一个
唯一的序列号?
 
没有表述清楚,是表的记录,
关于"怎样才能取得SQL SERVER 的所有表的字段值。 "这个问题,
已经解决了,是采取的 acbcwy 的方法。
谢谢大家,谢谢 acbcwy。
 
我说下我问这两个问题的意图吧:
一 HINTS 是必不可少的,如果我将字段所对应的HINDS 都放一个表里,
写个公用的东东,SQL 一下,就可以了。
第二个问题则是:如果在一个SQL 中,每条记录都有一个唯一的内部序号,
在查询时,是不是要省很多的事。
 
多人接受答案了。
 
后退
顶部