괄호와 모든 것을 사용하여 SQL Server 열 정의를 어떻게 얻을 수 있습니까?
-
05-07-2019 - |
문제
정보 유형을 정보를 얻을 수있는 스마트 방법이 필요합니다. 문제는 숫자와 같이 이해해야하는 '추가'필드입니다._
정밀도와 숫자_
규모.
분명히, 나는 정수의 열을 무시할 수 있지만 (10의 정밀도와 스케일 0), 숫자와 같은 다른 유형이 있습니다. 따라서 테이블을 구문 분석하기 위해 많은 코드를 작성하지 않고 열 정의에서 일종의 필드 속기를 얻는 방법에 대한 아이디어가 있습니까?
나는 다음과 같은 것을 얻을 수 있기를 원합니다 : int, dateTime, 돈, 숫자 ** (10,2) **
해결책
다음은 업데이트 (Ripoff!)입니다 galacticcowboy의 대답 일부 문제를 해결하고 모든 문제를 업데이트하려면 SQL Server 2008R2 데이터 유형에 대한 업데이트 :
select data_type +
case
when data_type like '%text' or data_type in ('image', 'sql_variant' ,'xml')
then ''
when data_type in ('float')
then '(' + cast(coalesce(numeric_precision, 18) as varchar(11)) + ')'
when data_type in ('datetime2', 'datetimeoffset', 'time')
then '(' + cast(coalesce(datetime_precision, 7) as varchar(11)) + ')'
when data_type in ('decimal', 'numeric')
then '(' + cast(coalesce(numeric_precision, 18) as varchar(11)) + ',' + cast(coalesce(numeric_scale, 0) as varchar(11)) + ')'
when (data_type like '%binary' or data_type like '%char') and character_maximum_length = -1
then '(max)'
when character_maximum_length is not null
then '(' + cast(character_maximum_length as varchar(11)) + ')'
else ''
end as CONDENSED_TYPE
, *
from information_schema.columns
order by table_schema, table_name, ordinal_position
다른 팁
select column_type = data_type +
case
when data_type like '%text' then ''
when data_type like '%char' and character_maximum_length = -1 then '(max)'
when character_maximum_length is not null then '(' + convert(varchar(10), character_maximum_length) + ')'
when data_type = 'numeric' then '(' + convert(varchar(10), isnull(numeric_precision, 18)) + ', ' +
convert(varchar(10), isnull(numeric_scale, 0)) + ')'
else ''
end
,*
from information_schema.columns
SMO 스크립팅은 스크립트 세대를 처리해야합니다. 이것이 MS가 스크립트 세대를 위해 SQL Management Studio에서 사용하는 것이라고 생각합니다.
http://msdn.microsoft.com/en-us/library/ms162153.aspx
@yourcomment- I need a smart way to get the data types out of INFORMATION_SCHEMA.COLUMNS in a way that could be used in a CREATE TABLE statement
이것이 당신이 요청한 것입니다. 이에 따라 정보 스키마보기 결과를 구문 분석해야합니다.
SMO를 사용하는 경우 속성에 액세스하여 정밀도와 스케일을 모두 얻을 수 있습니다. 열 객체
column.property [ "NumericsCale"]. 값
column.property [ "numericprecision"]. 값