SQL Server SQLColumns: Date column is displayed as a varchar column

ffdz8vbo  于 2023-08-02  发布在  其他
关注(0)|答案(3)|浏览(100)

I want know data type for columns in my table. For this I called ODBC function SQLColumns . But for each column which has type "date" this function says that column is varchar, but it is not so. Information about data type I know through the field "DATA_TYPE" which return int code of type -9, but this code must be 91. What's the problem, please tell me.

P.S. I use for retrieving information about columns standard C++ code example which is on official page. P.P.S For other DBMS this function SQLColumns() works correctly

Thank you!

kadbb459

kadbb4591#

I would guess its dependent on the ODBC driver you are using. If it does not support (or request) 2008 or later TDS version, then the type DATE will correctly be returned as VARCHAR.

g6ll5ycj

g6ll5ycj2#

You could run a query to get the table column info:

SELECT tbl.name AS tblName,
    col.name AS fldName,
    typ.name AS Type,
    col.length AS length,
    col.isnullable AS isnullable,
    cmt.text AS DefaultVal
FROM sys.sysobjects tbl
INNER JOIN sys.syscolumns col
    ON col.id = tbl.id
INNER JOIN sys.systypes typ
    ON typ.xtype = col.xtype
LEFT JOIN sys.sysobjects cnst
    ON cnst.xtype = 'D'
    AND cnst.parent_obj = tbl.id
    AND col.cdefault = cnst.id
LEFT JOIN sys.syscomments cmt
    ON cmt.id = cnst.id
WHERE  (tbl.xtype = 'U' OR tbl.xtype = 'V')
    AND (tbl.name <> N'dtproperties') 
    AND (tbl.name <> N'sysconstraints') 
    AND (tbl.name <> N'syssegments') 
    AND (tbl.name = 'MyTestTable')
ORDER BY tbl.name, col.name

Output:

tblName     fldName     Type    length  isnullable  DefaultVal
MyTestTable action      varchar 10  0   NULL
MyTestTable actionby    varchar 100 0   NULL
MyTestTable actiondate  datetime    8   0   NULL
MyTestTable colname     varchar 50  0   NULL
MyTestTable formfieldname   varchar 100 0   NULL
MyTestTable newvalue    varchar -1  0   NULL
MyTestTable oldvalue    varchar -1  0   NULL
MyTestTable primarykey  varchar 100 0   NULL
MyTestTable sequence    int 4   0   NULL
tjrkku2a

tjrkku2a3#

When the type comes back as WVARCHAR(-9) , you have to look at the sqlTypeName as obtained by

SQLCHAR     *sqlTypeName        = NULL;
SQLINTEGER  sqlTypeNameLength   = 0;

sqlTypeName = (unsigned char *)(strSchemaName.GetBuffer(MAX_TEXT_FILE_LINE_LENGTH));

SQLBindCol(m_hSQL,6,SQL_C_CHAR,sqlTypeName,MAX_TEXT_FILE_LINE_LENGTH, &sqlTypeNameLength);

It will contain date , time , or datetime2 . Type WVARCHAR(-9) is not returned when the data column is a datetime .

相关问题