October 25, 2013 at 1:02 pm
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)
October 28, 2013 at 4:27 am
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;-)
October 28, 2013 at 7:05 pm
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 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
October 29, 2013 at 10:46 am
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
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply