有没有办法显示创建表的查询?例如,有一个名为CLIENT的表,我想查看用于创建此表的查询。我怎样才能做到这一点?如何查找在SQL Server 2008中创建表的查询
0
A
回答
1
右键单击该表中SSMS并选择脚本表>作为创建
4
- 右键单击表
- 脚本表为
- 创造
- 新的查询编辑器窗口
编辑:该死的你必须快速在这里为简单的代表!哈哈。
1
如果你想从一些脚本做那么这里是
declare @table varchar(100)
set @table = 'client_table' -- set table name here
declare @sql table(s varchar(1000), id int identity)
-- create statement
insert into @sql(s) values ('create table [' + @table + '] (')
-- column list
insert into @sql(s)
select
' ['+column_name+'] ' +
data_type + coalesce('('+cast(character_maximum_length as varchar)+')','') + ' ' +
case when exists (
select id from syscolumns
where object_name(id)[email protected]
and name=column_name
and columnproperty(id,name,'IsIdentity') = 1
) then
'IDENTITY(' +
cast(ident_seed(@table) as varchar) + ',' +
cast(ident_incr(@table) as varchar) + ')'
else ''
end + ' ' +
(case when IS_NULLABLE = 'No' then 'NOT ' else '' end) + 'NULL ' +
coalesce('DEFAULT '+COLUMN_DEFAULT,'') + ','
from information_schema.columns where table_name = @table
order by ordinal_position
-- primary key
declare @pkname varchar(100)
select @pkname = constraint_name from information_schema.table_constraints
where table_name = @table and constraint_type='PRIMARY KEY'
if (@pkname is not null) begin
insert into @sql(s) values(' PRIMARY KEY (')
insert into @sql(s)
select ' ['+COLUMN_NAME+'],' from information_schema.key_column_usage
where constraint_name = @pkname
order by ordinal_position
-- remove trailing comma
update @sql set s=left(s,len(s)-1) where [email protected]@identity
insert into @sql(s) values (' )')
end
else begin
-- remove trailing comma
update @sql set s=left(s,len(s)-1) where [email protected]@identity
end
-- closing bracket
insert into @sql(s) values(')')
-- result!
select s from @sql order by id
这会给你的输出像
create table [client_table] (
[colA] varchar(250) NOT NULL DEFAULT (''),
[colB] int NOT NULL DEFAULT(0)
)
相关问题
- 1. 如何在SQL Server 2008中查找性能最差的查询?
- 2. 如何创建Sql Server 2008交叉表动态查询?
- 3. SQL Server 2008 R2表查询
- 4. 查询创建SQL Server中的新表2008年
- 5. 如何创建在SQL Server 2008 R2查询
- 6. SQL Server 2008查询
- 7. Sql Server 2008查询
- 8. SQL Server 2008中的“交叉表”查询
- 9. SQL Server 2008/64Bit - 如何创建包括Excel的查询
- 10. 查找在SQL Server 2008
- 11. SQL查询在SQL Server 2008中
- 12. SQL Server 2008 Express的查询
- 13. SQL Server 2008中的复杂SQL查询
- 14. 循环在SQL查询的SQL Server 2008
- 15. 访问查询与SQL Server 2008查询
- 16. “创建如果不存在”和“创建表”像sql server查询
- 17. 你如何在SQL Server一整套在SQL Server查询2008
- 18. 使用SQL Server CE创建表查询
- 19. SQL Server 2008查找时间
- 20. SQL Server 2008查找用户
- 21. 查询优化SQL Server 2008
- 22. SQL Server 2008/Reporting Services查询
- 23. SQL Server 2008 XML查询
- 24. 查询从SQL Server 2008
- 25. SQL Server 2008像查询
- 26. SQL Server 2008分层查询
- 27. Sql server 2008查询php
- 28. 查询使用SQL Server 2008
- 29. 优化SQL Server 2008查询
- 30. SQL Server 2008 - 搜索查询
+1为您编辑::) – Shiva
感谢湿婆!在成为水蛭几年之后才刚刚开始。 –