declare @maxLength intselect  @maxLength =(case when sc.xtype<>'231'then  sc.lengthelse sc.length/2  end )from syscolumns as sc where  sc.id=object_id('MortgageMachinery') and sc.name='Manufacturer'print @maxLength

解决方案 »

  1.   

    length是NVARCHAR类型的??那要转换一下啊
      

  2.   

    nvarchar 类型是字符串型的数据,除2的意义何在?
      

  3.   

    sc.xtype<>'231'
    --〉
    sc.xtype<>231不过应该没什么影响。
      

  4.   

    我刚才测试了,不是length是NVARCHAR类型的原因,,感觉是判断那里不正确,,处理不了,,请各位指点一下,,
      

  5.   

    declare @maxLength intselect @maxLength =(case when NOT (sc.xtype=231)then ISNULL(sc.length,0)else ISNULL(sc.length/2,0) end )from syscolumns as sc where sc.id=object_id('MortgageMachinery') and sc.name='Manufacturer'print @maxLength
      

  6.   

    是字符串型的数据,除2的意义何在? 如果是nvarchar 类,输入的汉字最大长度就只为数据库中长度的一半,,否则就因数据过长插入不成功,,
      

  7.   

    declare @maxLength intselect @maxLength =(case when NOT (sc.xtype=231)then ISNULL(sc.length,0)else ISNULL(sc.length/2,0) end )from syscolumns as sc where sc.id=object_id('MortgageMachinery') and sc.name='Manufacturer'print @maxLength
    结果还是不正确,,
      

  8.   

    GO
    /****** 对象:  Table [dbo].[MortgageMachinery]    脚本日期: 09/29/2011 10:50:58 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[MortgageMachinery](
    [AutoID] [int] IDENTITY(1,1) NOT NULL,
    [MortgageMachineryID] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
    [Mortgager] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
    [MortgagerNumber] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
    [Address] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
    [MortgageName] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
    [Type] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
    [Amount] [int] NULL,
    [Unit] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
    [InvoiceMoney] [decimal](18, 0) NULL,
    [MortgageMoney] [decimal](18, 0) NULL,
    [MortgageLimitTime] [int] NULL,
    [EvaluateMoney] [decimal](18, 0) NULL,
    [MortgageRate] [decimal](18, 0) NULL,
    [InvoiceNo] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
    [InvoiceDate] [datetime] NULL,
    [Manufacturer] [nvarchar](30) COLLATE Chinese_PRC_CI_AS NULL,
    [DeviceBrand] [nvarchar](30) COLLATE Chinese_PRC_CI_AS NULL,
    [SumPrice] [nvarchar](30) COLLATE Chinese_PRC_CI_AS NULL,
    [Re] [nvarchar](300) COLLATE Chinese_PRC_CI_AS NULL,
    [custID] [varchar](20) COLLATE Chinese_PRC_CI_AS NULL,
    [Valuation] [decimal](18, 0) NULL,
     CONSTRAINT [pk_MortgageMachinery_autoID] PRIMARY KEY CLUSTERED 
    (
    [AutoID] ASC
    )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
    ) ON [PRIMARY]GO
    SET ANSI_PADDING OFF
      

  9.   

    你直接这么写,看看输出什么
    select sc.xtype,
    case when sc.xtype<>'231' then sc.length else sc.length/2 end 
    from syscolumns as sc
      

  10.   

    length不是个字段?len(sc.xtype) / 2
      

  11.   

    上面是我的表结构,如果nvarchar(30),我就想取15,,其它型类的长度不做处理(eg:varchar(30)就取30),,
      

  12.   

    qianjin036a,,呵呵,,你挺幽默,,谢谢大家的回贴,,我只是想取出表结构中的括号中的值长度,,
    如果nvarchar(30),我就想取15,,其它型类的长度不做处理(eg:varchar(30)就取30, ),,
      

  13.   

    declare @maxLength intselect @maxLength =(case when sc.xtype=231 then ISNULL(sc.length/4,0)
    else ISNULL(sc.length,0) end )from syscolumns as sc where sc.id=object_id('MortgageMachinery') and sc.name='Manufacturer'print @maxLength
      

  14.   

    可是....结果难道不对么:
    select convert(varchar(20),name)name,xtype,length from syscolumns where id=OBJECT_ID('MortgageMachinery')
    /*
    name                 xtype length
    -------------------- ----- ------
    AutoID               56    4
    MortgageMachineryID  167   50
    Mortgager            167   50
    MortgagerNumber      167   50
    Address              167   50
    MortgageName         167   50
    Type                 167   50
    Amount               56    4
    Unit                 167   50
    InvoiceMoney         106   9
    MortgageMoney        106   9
    MortgageLimitTime    56    4
    EvaluateMoney        106   9
    MortgageRate         106   9
    InvoiceNo            167   50
    InvoiceDate          61    8
    Manufacturer         231   60
    DeviceBrand          231   60
    SumPrice             231   60
    Re               231   600
    custID               167   20
    Valuation            106   9(22 行受影响)
    */
    select (case when sc.xtype<>231 then sc.length else sc.length/2 end )as maxLength
    from syscolumns as sc where sc.id=object_id('MortgageMachinery') and sc.name='Manufacturer'
    /*
    maxLength
    -----------
    30(1 行受影响)*/
    go
      

  15.   

    这个好办啊,你写错语句了,不是这么写的。按照下面的写:
    declare @maxLength int
    select
    case when sc.xtype='231'then sc.length/2 else sc.length end 
    from syscolumns as sc where sc.id=object_id('MortgageMachinery') 
    print @maxLength 
      

  16.   

    nvarchar类型是占用2个字节的,如果你定义的是nvarchar(30),实际上在数据库中存储的长度是60,你通过/2就可以获得你当时定义的这个长度30,明白了么?