Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Selective Insert using Wildcard Expand / Collapse
Author
Message
Posted Friday, October 25, 2013 1:02 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 3:48 PM
Points: 270, Visits: 714
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)

Post #1508574
Posted Monday, October 28, 2013 4:27 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, August 19, 2014 12:26 AM
Points: 2,840, Visits: 3,963
try this

INSERT INTO Company2.dbo.CUSTVEND
SELECT *
,CASE WHEN DIVISION='DIV1' THEN 'DIV2' END
FROM Company1.dbo.CUSTVEND c1
WHERE not exists (SELECT 1 FROM Company2.dbo.CUSTVEND c2
where c2. ACCTNO = c1. ACCTNO and c1.SUBC= c2.SUBC )



-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done
Post #1508793
Posted Monday, October 28, 2013 7:05 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 10:58 PM
Points: 3,418, Visits: 5,333
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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1509163
Posted Tuesday, October 29, 2013 10:46 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, April 22, 2014 10:38 AM
Points: 103, Visits: 448
If this is a one time deal, why use *? If you already know the columns, you can simply add a case statement to the field you want to hard code.

Another approach would be to use select * into your target table, then use an update on that table:

  UPDATE myTable, SET myField = 'MY HARD CODED VALUE' WHERE myField = myCondition


Post #1509468
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse