UNION error

  • GĀ² (11/6/2008)


    It was my understanding that varchar(max) was meant to be a replacement to the text data type because of the issue that this question raises as well as other like:

    Being able to use varchar(max) as a local variable (can't with text)

    Being able to use most string functions with it

    It can still hold 2Gb of data like text can, so what would this break?

    Please correct me if I'm wrong. These discussions are great!

    Greg

    Yes, all of the above is true. That is why I have changed my usage when we dropped SQL Server 2000 support. As to breakage, the application could be using READTEXT, WRITETEXT, UPDATETEXT, TEXTPTR, etc. See the BOL for details.


    [font="Arial Narrow"](PHB) I think we should build an SQL database. (Dilbert) What color do you want that database? (PHB) I think mauve has the most RAM.[/font]

  • JohnG (11/6/2008)


    GĀ² (11/6/2008)


    It was my understanding that varchar(max) was meant to be a replacement to the text data type because of the issue that this question raises as well as other like:

    Being able to use varchar(max) as a local variable (can't with text)

    Being able to use most string functions with it

    It can still hold 2Gb of data like text can, so what would this break?

    Please correct me if I'm wrong. These discussions are great!

    Greg

    Yes, all of the above is true. That is why I have changed my usage when we dropped SQL Server 2000 support. As to breakage, the application could be using READTEXT, WRITETEXT, UPDATETEXT, TEXTPTR, etc. See the BOL for details.

    That's a good point. I hadn't thought of the text specific functions.

    Thanks,

    Greg

  • GĀ² (11/6/2008)


    That's a good point. I hadn't thought of the text specific functions.

    Thanks,

    Greg

    Now you know why just performing an "ALTER TABLE" to change the data type could not be considered a "workaround" to the specific query problem. The entire application could fail.


    [font="Arial Narrow"](PHB) I think we should build an SQL database. (Dilbert) What color do you want that database? (PHB) I think mauve has the most RAM.[/font]

  • DOCTOR FRANKENSTEIN

    OPEN UP !

  • Not mentioned as an answer, but here is a workaround using varchar(max) (or nvarchar(max)); build views over the two tables and cast the text/ntext column as a varchar(max)/nvarchar(max) datatype in the select statement.

    There is your workaround, and it doesn't affect the underlying table or procedures that work with the text/ntext column.

  • Now, this is a workaround I can live with. Good idea.

    But I am still miffed at not having my points. (grin).

  • Several people have complained that changing the text column to varchar(max) is bad because you are changing the data, which is right, so I'd just do the conversion in line..select id, convert(varchar(max),somestuff) as 'somestuff' from tableA

    union

    select id, convert(varchar(max)somestuff) as 'somestuff' from tableB

    I'd be wary of changing the UNION to UNION ALL since although, at present, the assumption is that there are no duplicates, I would assume this can't be guaranteed, since otherwise the query would have specified a UNION ALL in the first place!

    Of couse, if this is an ad hoc query, rather than production code, than anything that works (and isn't ridiculously poor in performance) is acceptable! šŸ™‚

    Derek

  • In MHO, making a change to the underlying table structure is NOT a workaround. (period!) Therefore, the only "workaround" solution is to change the select statement. šŸ˜‰


    Regards,

    Joe Burdette
    hanesbrands.com

  • Even varchar() has limited size to 4000.

  • J (11/6/2008)


    Sorry - you were wrong

    No. YOU ARE wrong.

    It does not matter if there are duplicates or not, because the question specifically mentioned that there were no duplicates.

    So "UNION ALL" is entirely satisfactory.

    And modifying the structure of tables is hardly a "workaround". There are consequences to doing this.

    Now, gimme my points! And double them for amends! Or else !

    I'm a hundred percent in agreement with this reply. Plus the varchar(max) option doesn't exists in SQL 2000, which is where most SQL Server databases still are.

    The answer is wrong, completely wrong!

    Tom

    Tom

  • Tom.Thomson (1/26/2009)


    J (11/6/2008)


    Sorry - you were wrong

    No. YOU ARE wrong.

    It does not matter if there are duplicates or not, because the question specifically mentioned that there were no duplicates.

    So "UNION ALL" is entirely satisfactory.

    And modifying the structure of tables is hardly a "workaround". There are consequences to doing this.

    Now, gimme my points! And double them for amends! Or else !

    I'm a hundred percent in agreement with this reply. Plus the varchar(max) option doesn't exists in SQL 2000, which is where most SQL Server databases still are.

    The answer is wrong, completely wrong!

    Tom

    Unfortunately, the question specifically stated SQL Server 2005, as shown in the following quote from the question:

    You have a default SQL2005 Standard.installation with 2 tables:

    So this comes down to reading the WHOLE question before answering.

  • Joe Burdette (11/6/2008)


    In MHO, making a change to the underlying table structure is NOT a workaround. (period!) Therefore, the only "workaround" solution is to change the select statement. šŸ˜‰

    No need to change the structure of the tables. I posted a workaround that would use a view over the tables. Derek Dongray also posted a solution whar you cast the text columns as varchar(max) in the select statements directly.

    The best solution, however, would have been to build the tables in SQL Server 2005 using the varchar(max) to begin with.

  • I am not getting the second option to change UNION to UNION all

    Deepak Kumar Sharma

  • Happy wif the question and the answer...

    What you don't know won't hurt you but what you know will make you plan to know better
  • Lynn Pettis (1/26/2009)


    Tom.Thomson (1/26/2009)


    J (11/6/2008)


    Sorry - you were wrong

    No. YOU ARE wrong.

    It does not matter if there are duplicates or not, because the question specifically mentioned that there were no duplicates.

    So "UNION ALL" is entirely satisfactory.

    And modifying the structure of tables is hardly a "workaround". There are consequences to doing this.

    Now, gimme my points! And double them for amends! Or else !

    I'm a hundred percent in agreement with this reply. Plus the varchar(max) option doesn't exists in SQL 2000, which is where most SQL Server databases still are.

    The answer is wrong, completely wrong!

    Tom

    Unfortunately, the question specifically stated SQL Server 2005, as shown in the following quote from the question:

    You have a default SQL2005 Standard.installation with 2 tables:

    So this comes down to reading the WHOLE question before answering.

    Mea maxima culpa! I should read more carefully, my addition about sql 2000 was totally irrelevant.

    BUT: union all is still entirely satisfactory, since there are known to be no duplicates (part of the probl;em, statement) - and union instead of union all will perform less well (since it has to do the unneeded check for duplicates). AND making modifications to table structure is NOT a good workaround, there are potentially many side effects. I'm not sure if it was you who originally made those comments or someone else, but that was what I was agreeing with.

    Tom

    Tom

Viewing 15 posts - 16 through 29 (of 29 total)

You must be logged in to reply to this topic. Login to reply