#sql-server #excel
#sql-сервер #excel
Вопрос:
Ответ №1:
Я взял некоторый код из определения SP_HELP
for reference, и вот кое-что, что может вам помочь, но вам все равно придется копировать данные в Excel вручную :
select
'Table_name' = ao.name,
'Column_name' = ac.name,
'Type' = type_name(user_type_id),
'Computed' = case when ColumnProperty(ac.object_id, ac.name, 'IsComputed') = 0 then 'no' else 'yes' end,
'Length' = convert(int, max_length),
-- for prec/scale, only show for those types that have valid precision/scale
-- Search for type name ',', because 'datetime' is actually a substring of 'datetime2' and 'datetimeoffset'
'Prec' = case when charindex(type_name(system_type_id) ',', N'tinyint,smallint,decimal,int,bigint,real,money,float,numeric,smallmoney,date,time,datetime2,datetimeoffset,') > 0
then convert(char(5),ColumnProperty(ac.object_id, ac.name, 'precision'))
else ' ' end,
'Scale' = case when charindex(type_name(system_type_id) ',', N'tinyint,smallint,decimal,int,bigint,real,money,float,numeric,smallmoney,date,time,datetime2,datetimeoffset,') > 0
then convert(char(5),OdbcScale(system_type_id,scale))
else ' ' end,
'Nullable' = case when is_nullable = 0 then 'no' else 'yes' end,
'TrimTrailingBlanks' = case ColumnProperty(ac.object_id, ac.name, 'UsesAnsiTrim')
when 1 then 'no'
when 0 then 'yes'
else '(n/a)' end,
'FixedLenNullInSource' = case
when type_name(system_type_id) not in ('varbinary','varchar','binary','char')
then '(n/a)'
when is_nullable = 0 then 'no' else 'yes' end,
'Collation' = collation_name
from sys.all_columns ac
INNER JOIN sys.all_objects ao
ON ac.object_id = ao.object_id and ao.type = N'U'
ORDER BY ao.object_id, ac.column_id
Ответ №2:
DECLARE @OBJ_TBLNAME VARCHAR(100) = 'touchnet'
DECLARE @OUTTBL AS TABLE (NAME VARCHAR(200), DATATYPE VARCHAR(200), NULLABLE VARCHAR(10))
declare @mytbl as table(TABLE_NAME VARCHAR(500))
INSERT INTO @mytbl SELECT TABLE_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_CATALOG=@OBJ_TBLNAME GROUP BY TABLE_NAME
WHILE((SELECT COUNT(*) FROM @mytbl)>0)
BEGIN
DECLARE @TBLNAME VARCHAR(200)
SELECT TOP 1 @TBLNAME = TABLE_NAME FROM @mytbl GROUP BY TABLE_NAME
INSERT INTO @OUTTBL(NAME, DATATYPE, NULLABLE) SELECT @TBLNAME, '', ''
INSERT INTO @OUTTBL(NAME, DATATYPE, NULLABLE) SELECT 'Colum Name', 'Data Type', 'Type'
INSERT INTO @OUTTBL
select COLUMN_NAME AS NAME, CONVERT(varchar, DATA_TYPE) (case when ISNULL(CHARACTER_MAXIMUM_LENGTH,-1)=-1 then '' else '(' CONVERT(varchar, CHARACTER_MAXIMUM_LENGTH) ')' end) AS DATATYPE, (CASE when is_nullable = 'YES' then 'NOT NULL' else 'NULL' end) AS NULLABLE from INFORMATION_SCHEMA.COLUMNS
where TABLE_CATALOG=@OBJ_TBLNAME and TABLE_NAME=@TBLNAME
INSERT INTO @OUTTBL(NAME, DATATYPE, NULLABLE) SELECT '', '', ''
DELETE TOP(1) FROM @mytbl
END
SELECT * FROM @OUTTBL
Комментарии:
1. Это нормально для всех таблиц в БД. Что делать, если вы хотите получить только для одной таблицы?