• bpowers (10/25/2013)


    We are populating a new database (Company2) with the vendor data that is currently in the Company1 database, using the wildcard to pickup all the fields. However, there is one field (Division) that we would like to hard code in the insert statement. We thought about including it using a case statement, but we are not sure if that is the best approach. Any feedback would be greatly appreciated.

    INSERT INTO Company2.dbo.CUSTVEND

    SELECT *

    ,CASE WHEN DIVISION='DIV1' THEN 'DIV2' END

    FROM Company1.dbo.CUSTVEND c1

    WHERE c1.ACCTNO+c1.SUBC not in(SELECT DISTINCT ACCTNO+SUBC

    FROM Company2.dbo.CUSTVEND)

    First of all, SELECT * means "all columns" and not "all columns except DIVISION." It looks to me like you're going to get a mismatch on the number of columns when you try to run the INSERT.

    Secondly, there is never a reason to use DISTINCT in a sub-select where you're using IN or NOT IN. It just slows the query down.

    Finally, what happens WHEN DIVISION <> 'DIV1'? Is your intent there to INSERT a NULL for the column?


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St