SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Selective Insert using Wildcard


Selective Insert using Wildcard

Author
Message
bpowers
bpowers
SSC-Addicted
SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)

Group: General Forum Members
Points: 487 Visits: 960
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)


Bhuvnesh
Bhuvnesh
SSCertifiable
SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)

Group: General Forum Members
Points: 5360 Visits: 4076
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;-)
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)

Group: General Forum Members
Points: 7425 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
souLTower
souLTower
Old Hand
Old Hand (361 reputation)Old Hand (361 reputation)Old Hand (361 reputation)Old Hand (361 reputation)Old Hand (361 reputation)Old Hand (361 reputation)Old Hand (361 reputation)Old Hand (361 reputation)

Group: General Forum Members
Points: 361 Visits: 559
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



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search