查明列是否有 varchar 20

发布于 2024-12-14 20:09:03 字数 371 浏览 1 评论 0原文

目标:
查明列 Firstname 是否包含 varchar(20)。如果为 true,则更改表等。

问题:
我无法确定列 Firstname 内部是否有 varchar 20 (如果存在) 一切都发生在 SQL Server 2008 R2

表 Staff
柱子:

Firstname varchar(20)  
Lastname varchar(100)
if Exists()   // Find out if column Firstname has varchar(20)
begin 

   // Alter table and its specific column

end 

Goal:
Find out if column Firstname has varchar(20). If true, alter the table etc.

Problem:
I have problem to find out if column Firstname has varchar 20 inside of (if Exists)
Everything take place in SQL server 2008 R2

table Staff
Column:

Firstname varchar(20)  
Lastname varchar(100)
if Exists()   // Find out if column Firstname has varchar(20)
begin 

   // Alter table and its specific column

end 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尴尬癌患者 2024-12-21 20:09:03

COLUMNS 信息模式视图非常适合此目的。

SELECT * FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = [table] AND COLUMN_NAME = [column]
AND DATA_TYPE = 'varchar' AND CHARACTER_MAXIMUM_LENGTH = 20

The COLUMNS information schema view is ideal for this.

SELECT * FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = [table] AND COLUMN_NAME = [column]
AND DATA_TYPE = 'varchar' AND CHARACTER_MAXIMUM_LENGTH = 20
稍尽春風 2024-12-21 20:09:03
IF EXISTS
    (SELECT *
    FROM    sys.columns c
    WHERE   c.object_id = OBJECT_ID('MySchema.MyTable')
    AND c.name = 'MyColumn'
    AND c.system_type_id = 167 --167 = varchar SELECT * FROM sys.types WHERE name = 'varchar'
    AND c.max_length = 20)
BEGIN
    ALTER TABLE MySchema.MyTable
    ALTER COLUMN MyColumn VARCHAR(25) NULL; --or NOT NULL and/or DEFAULT ...
END
IF EXISTS
    (SELECT *
    FROM    sys.columns c
    WHERE   c.object_id = OBJECT_ID('MySchema.MyTable')
    AND c.name = 'MyColumn'
    AND c.system_type_id = 167 --167 = varchar SELECT * FROM sys.types WHERE name = 'varchar'
    AND c.max_length = 20)
BEGIN
    ALTER TABLE MySchema.MyTable
    ALTER COLUMN MyColumn VARCHAR(25) NULL; --or NOT NULL and/or DEFAULT ...
END
德意的啸 2024-12-21 20:09:03

如果使用 sys.columns - 请记住 max_length 列实际上是最大大小(以字节为单位) - 而不是列的实际声明大小。

请参阅此处了解更多信息:MSDN 上的 sys.columns

这适用于char/varchar(其中大小为 n 字节),但对于 nchar 和 nvarchar,字节存储不同,其中 n 是字符串的长度。

nchar 是 2 n 字节,因此您需要执行 max_length/2 来获取实际声明的大小 (1-4000)

nvarchar 也是 2 n em> 字节,因此您需要执行 (max_length/2) 来获取实际声明的大小。尽管 nvarchar 还为每列使用额外的 2 个字节,但这不包含在 sys.columns.max_length 列中。

另请记住:任何 nvarchar 或 nchar(max) 值都会返回 -1

示例代码,选择所有表的所有列并显示类型详细信息:-

SELECT '[' + schema_name(t.[schema_id]) + '].[' + t.name + ']' AS TableName
            , '[' + c.name + ']' AS ColumnName
            , '[' + ty.name + ']' AS DataType
            , max_length_value = CASE WHEN c.max_length > -1
                AND (ty.name = 'nvarchar' 
                OR ty.name = 'nchar') THEN c.max_length/2 
            ELSE c.max_length END
            , max_length_bytes = c.max_length
            , c.is_nullable
        FROM sys.columns AS c
            INNER JOIN sys.tables AS t ON t.object_id = c.object_id
            INNER JOIN sys.types AS ty ON ty.system_type_id = c.system_type_id
                AND ty.user_type_id = c.user_type_id

来自 MSDN 的 nchar 和 nvarchar :-

nchar[(n)]
固定长度的 Unicode 字符串数据。 n 定义字符串长度,并且必须是 1 到 4,000 之间的值。存储大小是n字节的两倍。当排序规则代码页使用双字节字符时,存储大小仍为n字节。根据字符串的不同,n 个字节的存储大小可能小于为 n 指定的值。 nchar 的 ISO 同义词是National char 和National Character。

nvarchar[(n|ma​​x)]
可变长度 Unicode 字符串数据。 n 定义字符串长度,可以是 1 到 4,000 之间的值。 max表示最大存储大小为2^31-1字节(2GB)。存储大小(以字节为单位)是输入数据实际长度的两倍+2字节。 nvarchar 的 ISO 同义词是National char Varing 和NationalCharacter Varing。

If using sys.columns - remember that the max_length column is actually the maximum size in bytes - not the actual declared size of a column.

See here for more info: sys.columns on MSDN

This works fine for char/varchar (where the size is n bytes), but for nchar and nvarchar the byte storage is different, where n is the length of the string.

nchar is 2 n bytes, so you will need to do max_length/2 to get the actual declared size (1-4000)

nvarchar is also 2 n bytes, so you will need to do (max_length/2) to get the actual declared size. Although nvarchar also uses an additional 2 bytes for every column, this is not included in the sys.columns.max_length column.

Also Remember: Any nvarchar or nchar(max) values return -1

Example code, selects all columns for all tables and displays type details:-

SELECT '[' + schema_name(t.[schema_id]) + '].[' + t.name + ']' AS TableName
            , '[' + c.name + ']' AS ColumnName
            , '[' + ty.name + ']' AS DataType
            , max_length_value = CASE WHEN c.max_length > -1
                AND (ty.name = 'nvarchar' 
                OR ty.name = 'nchar') THEN c.max_length/2 
            ELSE c.max_length END
            , max_length_bytes = c.max_length
            , c.is_nullable
        FROM sys.columns AS c
            INNER JOIN sys.tables AS t ON t.object_id = c.object_id
            INNER JOIN sys.types AS ty ON ty.system_type_id = c.system_type_id
                AND ty.user_type_id = c.user_type_id

nchar and nvarchar from MSDN :-

nchar[(n)]
Fixed-length Unicode string data. n defines the string length and must be a value from 1 through 4,000. The storage size is two times n bytes. When the collation code page uses double-byte characters, the storage size is still n bytes. Depending on the string, the storage size of n bytes can be less than the value specified for n. The ISO synonyms for nchar are national char and national character.

nvarchar[(n|max)]
Variable-length Unicode string data. n defines the string length and can be a value from 1 through 4,000. max indicates that the maximum storage size is 2^31-1 bytes (2 GB). The storage size, in bytes, is two times the actual length of data entered + 2 bytes. The ISO synonyms for nvarchar are national char varying and national character varying.

誰認得朕 2024-12-21 20:09:03

您应该能够使用类似于此查询的内容来查找您的列是否为 varchar(20)。这将从信息模式中的视图中进行选择,并按特定于列和表的条件进行过滤。

select t.name 'Table'
    , c.name 'Column'
    , c.max_length
from sys.columns c
inner join sys.tables t on
    t.object_id = c.object_id
inner join sys.types y on
    y.system_type_id = c.system_type_id
where c.name = 'FirstName'
    and t.name = 'MyTable'
    and y.name = 'varchar'
    and c.max_length = 20

You should be able to use something similar to this query to find if your column is varchar(20). This will select from views in the information schema and filter by column- and table-specific criteria.

select t.name 'Table'
    , c.name 'Column'
    , c.max_length
from sys.columns c
inner join sys.tables t on
    t.object_id = c.object_id
inner join sys.types y on
    y.system_type_id = c.system_type_id
where c.name = 'FirstName'
    and t.name = 'MyTable'
    and y.name = 'varchar'
    and c.max_length = 20
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文