• BLandry474 (10/20/2016)


    Here is my brilliant idea (that I am sure someone else already thought of...)

    We have a situation where we regularly get tables from another department and we have to check every text-related column to see if the users actually "over-typed" something. What I mean is that they were looking away from the keyboard and typed more than 255 chars. without realizing it - saved the record - and never knew they had done this.

    So the boss asked if I could write a SProc to detect this kind of thing. E.g. look at the columns, and see if they were "full" (for lack of a better term). So the first thing I did was to run:

    EXEC sp_columns @table_name = 'WHATEVER_THE_TABLE_IS' and this gives me a nice display of ALL the internal makeup of the table - BUT!!! - I don't need all of it. I just need a few columns like LENGTH and PRECISION.

    So here is the simple (I hope) question...

    Can I develop a query that would allow me to specify the columns out of what I presume is Master? Again, I don't need all of it - just a few.

    Thanks in advance for any help you can offer.

    BLandry474 (10/21/2016)


    My thanks to all who responded, but please, in the future, read the question - I DID NOT say "Users are entering data", what I said was "We get tables" - meaning these are tables exported from other systems where the limits are not the same as SQL Server. So a few of you got off on a tangent answering a question I never asked.

    To those who presented good, workable solutions, many thanks - it's most helpful. To the others, I honor your excellent knowledge; you're way ahead of me - but deep knowledge is kind of useless if you're answering a question that was in fact, never asked.

    No offense intended here to anyone, but as the wise man says; "intelligence is useless without common sense."

    Heh... no offense but why didn't you just ask how you could get the same information as what sp_columns gives you but in a SELECT instead of going 'round the horn with information that only served to confuse those in a hurry to help and then bawling people out for trying to solve the root problem, which is actually a bad app that you can't fix anyway? 😉

    Getting to the root of your problem...

    BLandry474 (10/21/2016)


    Alex,

    Here is the basic code I used. This produces a nice temp table of information and of course, you can edit the query if you don't want all the columns. I am using this in a larger SProc that will allow my user to add the table he wants to compare as a parameter. Works well so far! Obviously, from this basic code, you could tinker it to meet your own needs.

    Create table TempTable

    (TABLE_QUALIFIER varchar(40),

    TABLE_OWNER varchar(20),

    TABLE_NAME varchar(40),

    COLUMN_NAME varchar(40),

    DATA_TYPE int,

    TYPE_NAME varchar(20),

    PREC int, LENGTH int,

    SCALE int, RADIX int,

    NULLABLE char(4),

    REMARKS varchar(128),

    COLUMN_DEF varchar(40),

    SQL_DATA_TYPE int,

    SQL_DATETIME_SUB int,

    CHAR_OCTET_LENGTH int,

    ORDINAL_POSITION int,

    IS_NULLABLE char(4),

    SS_DATA_TYPE int)

    Set nocount on

    Insert TempTable

    Exec sp_columns @table_name = 'YOUR TABLE NAME'

    Select * From TempTable

    Drop table TempTable

    ... that's nice...

    There are easier ways though. There are a whole bunch of system tables in the "sys." schema of every database that contain such information and much more. They're in "Books Online" and you should take the time to look some of them up and as well as some of the meta-data functions. It'll make any future such projects a whole lot easier.

    Here's one example...

    --===== Uncomment the "ANDs" to isolate an individual table.

    SELECT SchemaName = OBJECT_SCHEMA_NAME(object_id)

    ,ObjectName = OBJECT_NAME(object_id)

    ,ColumnName = name

    ,* --change this to whatever column names you need

    FROM sys.columns

    WHERE OBJECTPROPERTY(object_id,'IsTable') = 1

    AND OBJECTPROPERTY(object_id,'IsMSShipped') = 0

    --AND OBJECT_SCHEMA_NAME(object_id) = 'dbo' --or whatever

    --AND OBJECT_NAME(object_id) = 'WHATEVER_THE_TABLE_IS' --obviously, you'll need to change this.

    ORDER BY SchemaName,ObjectName,ColumnName

    ;

    Perhaps an easier thing would be in the INFORMATION_SCHEMA views, which you can also read about in "Books Online". Here's an example.

    SELECT * --Change the "*" to whatever column names you want

    FROM INFORMATION_SCHEMA.COLUMNS

    WHERE TABLE_SCHEMA = 'dbo' -- or whatever

    AND TABLE_NAME = 'WHATEVER_THE_TABLE_IS' --obviously, you'll need to change this

    ;

    If you don't know what "Books Online" is, you can download it from the following URL and then you can get help just by pressing the {f1} key.

    [font="Arial Black"]Books Online Download for Local Installation[/font]

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)