收藏几段SQL Server语句和存储过程
-- ======================================================
--列出SQL SERVER 所有表,字段名,主键,类型,长度,小数位数等信息
--在查询分析器里运行即可,可以生成一个表,导出到EXCEL中
-- ======================================================
select
()表名,
a.colorder 字段序号,
a.name 字段名,
() 标识,
((*)
) 主键,
b.name 类型,
a.length 占用字节数,
COLUMNPROPERTY(a.) 允许空,
a.colid = g.smallid
order
by a.id,a.colorder
列出SQL SERVER 所有表、字段定义,类型,长度,一个值等信息
并导出到Excel 中
-- ======================================================
-- Export all user tables definition and one sample value
-- jan-13-2003,Dr.Zhang
-- ======================================================
在查询分析器里运行:
set ANSI_NULLS off
GO
set NOCOUNT ON
GO
set LANGUAGE 'Simplified Chinese'
go
declare @tbl nvarchar(200),@fld nvarchar(200),@sql nvarchar(4000),@maxlen int,@sample nvarchar(40)
select d.name TableName,a.name FieldName,b.name TypeName,a.length Length,a.isnullable IS_NULL into #t
from syscolumns a, systypes b,sysobjects d
where a.xtype=b.xusertype and a.id=d.id and d.xtype='U'
1 read_cursor @sql,N(@sql,N'@sample varchar(30) OUTPUT',@sample OUTPUT
--for quickly
--set @sql=N'set @sample=convert(varchar(20),(select
top 1 ' @fld ' from '
@sql,Nread_cursor #t
GO
tablename
@sql=
print @sql
,@sql = @sql '[' name '],'
from
(select
case
' name ' '
' name '
'
' name '
'
' name '
'
' name '
(' '
' name '
(' '
[' @tablename proc_insert (@tablename
,@sqlstr2=@sqlstr2 name
' a.name '
'
' a.name '
' a.name '
(' a.name '
' a.name '
('
' a.name '
'
' a.name '
' a.name '
' a.name '
' a.name '
(' a.name '
' a.name '
'
' a.name '
(' a.name '
' a.name '
' a.name '
('
' a.name '
' a.name '
' a.name '
' a.name '
'
' a.name '
(' a.name '
a (@sqlstr1,len(@sqlstr1)-3) ')'' from ' @tablename
-- print @sqlstr
exec( @sqlstr)
set nocount off
end