\Program files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\your_database_name.ldf>')
FOR ATTACH
GO
可以选择Windows或SQLServer验证,可以点击【测试连接】测试一下连接是否正常。
选择或附加一个数据库后,点击确定,会获取指定数据库中的所有表,选择要生成表结构的表(默认全选),点击【确定】生成:
获取指定数据库中的所有表:
SELECT * FROM sys.tables
1.在Word中插入表格
数据库、表选择好了之后,剩下的工作就要在Word中处理了。
【约束条件】、【默认值】、【备注】三列忽略,
在剩下的列中,【字段名】、【名称】(也取的字段名)、【数据类型】、【空、非空】比较好取得,只有一个【主键】颇费了一番功夫。
代码
--常用系统表
--SELECT * FROM sys.all_columns
--SELECT * FROM sys.tables
--SELECT * FROM sys.objects
--SELECT * FROM sys.key_constraints
--SELECT * FROM sys.types
--SELECT * FROM sys.indexes
--SELECT * FROM sys.default_constraints
--SELECT * FROM sys.all_objects
--SELECT * FROM sys.schemas
--常用系统视图
--SELECT * FROM sys.all_objects
--SELECT * FROM sys.databases
--SELECT * FROM sys.servers
--默认值测试
--SELECT col.column_id,col.name,d.definition
--FROM sys.columns AS col
--INNER JOIN sys.default_constraints AS d ON col.column_id = d.parent_column_id
IF EXISTS(SELECT * FROM sys.objects WHERE type = 'P' and name = 'sp_生成数据结构')
BEGIN
DROP PROCEDURE dbo.sp_生成数据结构
END
GO
CREATE PROCEDURE dbo.sp_生成数据结构
(
@TableNames SelectTables READONLY
)
AS
SET XACT_ABORT ON
BEGIN TRANSACTION
CREATE TABLE #TableNames
(
ID INT IDENTITY(1, 1),
TableName VARCHAR(200)
)
INSERT INTO #TableNames(TableName)
SELECT TableName
FROM @TableNames
DECLARE @table_owner sysname = null,
@table_qualifier sysname = null,
@table_id int,
-- quotename() returns up to 258 chars
@full_table_name nvarchar(517) -- 258 + 1 + 258
DECLARE @i INT, @Count INT, @Name VARCHAR(200)
SET @i = 1
SET @Count = (SELECT COUNT(*) FROM #TableNames)
WHILE @i <= @Count
BEGIN
SET @Name = (SELECT TableName FROM #TableNames WHERE ID = @i)
SET @table_owner =
(
SELECT s.name
FROM sys.schemas AS s
INNER JOIN sys.tables AS t ON s.schema_id = t.schema_id
WHERE t.name = @Name
)
if @table_owner is null
begin -- If unqualified table name
select @full_table_name = quotename(@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(@Name)
end
end
select @table_id = object_id(@full_table_name)
SELECT col.name AS 字段名,
col.name AS 名称,
CASE WHEN col.name IN
(
select
COLUMN_NAME = c.name
from
sys.indexes i,
sys.all_columns c,
sys.all_objects o
where
o.object_id = @table_id and
o.object_id = c.object_id and
o.object_id = i.object_id and
i.is_primary_key = 1 and
(c.name = index_col (@full_table_name, i.index_id, 1) or
c.name = index_col (@full_table_name, i.index_id, 2) or
c.name = index_col (@full_table_name, i.index_id, 3) or
c.name = index_col (@full_table_name, i.index_id, 4) or
c.name = index_col (@full_table_name, i.index_id, 5) or
c.name = index_col (@full_table_name, i.index_id, 6) or
c.name = index_col (@full_table_name, i.index_id, 7) or
c.name = index_col (@full_table_name, i.index_id, 8) or
c.name = index_col (@full_table_name, i.index_id, 9) or
c.name = index_col (@full_table_name, i.index_id, 10) or
c.name = index_col (@full_table_name, i.index_id, 11) or
c.name = index_col (@full_table_name, i.index_id, 12) or
c.name = index_col (@full_table_name, i.index_id, 13) or
c.name = index_col (@full_table_name, i.index_id, 14) or
c.name = index_col (@full_table_name, i.index_id, 15) or
c.name = index_col (@full_table_name, i.index_id, 16))
) THEN '√'
WHEN col.name NOT IN
(
select
COLUMN_NAME = convert(sysname,c.name)
from
sys.indexes i,
sys.all_columns c,
sys.all_objects o
where
o.object_id = @table_id and
o.object_id = c.object_id and
o.object_id = i.object_id and
i.is_primary_key = 1 and
(c.name = index_col (quotename(@full_table_name), i.index_id, 1) or
c.name = index_col (quotename(@full_table_name), i.index_id, 2) or
c.name = index_col (quotename(@full_table_name), i.index_id, 3) or
c.name = index_col (quoten