Home > SQL Server Tips > Database Development > Undocumented stored procedure: Return string type for a column with sp_gettypestring
SQL Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

DATABASE DEVELOPMENT

Undocumented stored procedure: Return string type for a column with sp_gettypestring


Serdar Yegulalp
07.05.2005
Rating: -3.50- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


SQL Server 2000 has a number of useful albeit undocumented (and therefore unsupported) stored procedures. Among them is the sp_gettypestring stored procedure, which can be used to return the type string for a column in a table. If you're trying to use a program to determine what a column is -- either in the context of a stored procedure or another piece of code -- this is one way to do it.

sp_gettypestring uses this syntax:

sp_gettypestring <tabid>, <colid>, <typestring>

<tabid> and <colid> are integers that refer to a table and column ID respectively. <typestring> is the output parameter (nvarchar(255)). Because you can't pass a column or table name to sp_gettypestring, you may need to derive this information programmatically.

Here's an example of how to do this:

USE MyDatabase

GO

DECLARE @intTableID int, @strTypeString nvarchar(255)

SELECT @intTableID = object_id('MyTable')

EXEC sp_gettypestring @intTableID, 1, @strTypeString output

SELECT @strTypeString

GO

In this example, sp_gettypestring is used on a database named MyDatabase, with a table named MyTable. The "1" in the EXEC sp_gettypestring parameter list means "Obtain the type string for the first column in the table." If you wanted the fourth column, you'd use "4," and so on.

The SELECT @intTableID statement uses the T-SQL object_id function to determine what the table ID number is for MyTable. If you already know the table ID, you can pass this manually, but it's usually painless enough to derive it in this fashion.

Bear in mind that because this is an undocumented procedure, there's no guarantee it will be supported in future versions of SQL Server or even in future updates of SQL Server 2000. For now, however, it's a convenient way to derive this sort of information without too much extra work.

About the author: Serdar Yegulalp is editor of the Windows Power Users Newsletter. Check it out for the latest advice and musings on the world of Windows network administrators -- and please share your thoughts as well!


More information from SearchSQLServer.com

  • Tip: A simple stored procedure to list database objects by selected type(s)
  • Chapter Download: Stored procedure primer
  • Ask the Experts: How to use results in one stored procedure from another


  • Rate this Tip
    To rate tips, you must be a member of SearchSQLServer.com.
    Register now to start rating these tips. Log in if you are already a member.


    Submit a Tip




    Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



    RELATED CONTENT
    SQL Server Stored Procedures
    SQL Server Mailbag: CALs, witnesses and unwanted changes
    SQL Server Mailbag: Stored procedures, triggers and SSRS reports
    Top tips and tricks for SQL Server database development
    Top 10 SQL Server development tips of 2008
    SQL Server trigger vs. stored procedure to receive data notification
    SQL Server errors, failures and other problems fixed from the trenches
    SQL Server and data manipulation in T-SQL
    How to use SQL Server 2008 hierarchyid data type
    SQL Server stored procedures tutorial: Write, tune and get examples
    Check SQL Server database and log file size with this stored procedure

    Database Development
    Using DELETE and TRUNCATE TABLE statements to delete data in SQL Server
    Speed up reports in SQL Server Reporting Services with caching
    Data Transformation Services vs. SSIS: The key differences
    Working with IntelliSense in SQL Server 2008 Management Studio
    Top tips and tricks for SQL Server database development
    Managing the development lifecycle with Visual Studio Team System 2008
    Processing XML files with SQL Server functions
    A first look at Visual Studio Team System 2008 Database Edition
    How to create a SQL inner join and outer join: Basics to get started
    New datetime data types in SQL Server 2008 offer flexibility

    RELATED GLOSSARY TERMS
    Terms from Whatis.com − the technology online dictionary
    library  (SearchSQLServer.com)
    trigger  (SearchSQLServer.com)

    RELATED RESOURCES
    2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
    Search Bitpipe.com for the latest white papers and business webcasts
    Whatis.com, the online computer dictionary

    DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



    SQL Server Development - .NET, C#, T-SQL, Visual Basic
    HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
    About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
    SEARCH 
    TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

    TechTarget Corporate Web Site  |  Media Kits  |  Site Map




    All Rights Reserved, Copyright 2005 - 2009, TechTarget | Read our Privacy Policy
      TechTarget - The IT Media ROI Experts