|
-
Apr 22nd, 2008, 02:48 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] [2005] Help with SQL Update Command
I want to update the Status of the NASE_Records table if the MemberNum in that table does not Exist in the SAS_Download table.
I don't think my sub query WHERE clause is right but I can't really test it cause this is live data and I don't want to screw up my table.
Code:
UPDATE NASE_Records
SET Status = 'I'
WHERE (NOT EXISTS
(SELECT *
FROM SAS_Download
WHERE (SAS_Download.MemberNum = NASE_Records.MemberNum)))
Any help would be great, thanks!
Tuber
"I don't know the rules"
-
Apr 22nd, 2008, 02:52 PM
#2
Re: [2005] Help with SQL Update Command
Update NASE_Records Set
Status = 'I'
Where MemberNum Not In (Select MemberNum From SAS_Download)
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Apr 22nd, 2008, 02:52 PM
#3
Re: [2005] Help with SQL Update Command
It should work, but isn't very efficient.
Try:
Code:
UPDATE NASE_Records
SET Status = 'I'
WHERE MemberNum NOT IN (SELECT MemberNum
FROM SAS_Download)
(VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.
-
Apr 22nd, 2008, 03:01 PM
#4
Thread Starter
Hyperactive Member
Re: [2005] Help with SQL Update Command
Oh, okay, yeah that does look alot better, thanks Gary, and Tom!!!!!!
Tuber
"I don't know the rules"
-
Apr 22nd, 2008, 03:05 PM
#5
Thread Starter
Hyperactive Member
Re: [2005] Help with SQL Update Command
Oh and thanks for the quick response!
Tuber
"I don't know the rules"
-
Apr 22nd, 2008, 03:06 PM
#6
Re: [2005] Help with SQL Update Command
If this is MS SQL - then you can use this.
Code:
Update NASE_Records Set Status = 'I'
From NASE_Records NS
Left Join SAS_Download SD on SD.MemberNum=NS.MemberNum
Where SD.MemberNum is null
This is truly efficient - as it's using a simple join.
And the even nicer thing about this type of UPDATE/FROM is that you can test it by changing it to a SELECT
Code:
Select *
From NASE_Records NS
Left Join SAS_Download SD on SD.MemberNum=NS.MemberNum
Where SD.MemberNum is null
This will show you - in advance - what the population of rows is that will be updated.
-
Apr 22nd, 2008, 03:13 PM
#7
Thread Starter
Hyperactive Member
Re: [RESOLVED] [2005] Help with SQL Update Command
This is more efficient huh because you're not looping through the whole table?
Tuber
"I don't know the rules"
-
Apr 22nd, 2008, 03:19 PM
#8
Re: [RESOLVED] [2005] Help with SQL Update Command
It would depend on the index. If there's not one on the MemberNum column, then the whole table would need to be scanned to get the rows included in the join. If there's one on that column in both tables, then the indexes can just be compared and it's much quicker.
(VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.
-
Apr 22nd, 2008, 03:20 PM
#9
Re: [RESOLVED] [2005] Help with SQL Update Command
 Originally Posted by tuber
This is more efficient huh because you're not looping through the whole table?
If member num is a PK on both tables the query-optimizer is going to see the simple-JOIN and the even simpler-WHERE/NULL and get those rows back as quickly as possible.
There is a good chance that the query-optimizer will discover that all three variations of this query are the same - but that's only going to happen in MS SQL, where that logic has 50 years of maturity.
I also prefer my version as it's easier to read - in my opinion 
You can do a BEGIN TRAN/ROLLBACK around all three of these and look at the execution plan in Mgt Studio and report back to us exactly which one is better...
-
Apr 22nd, 2008, 03:22 PM
#10
Re: [RESOLVED] [2005] Help with SQL Update Command
...Like Tom said
-
Apr 22nd, 2008, 03:23 PM
#11
Thread Starter
Hyperactive Member
Re: [RESOLVED] [2005] Help with SQL Update Command
It's not a primary key though, and we're not indexing this table, although we should because it's pretty massive.
The primary key is just a unique incremented int
I'm not very knowledgeable of SQL.
I'm using SQL 2003
Tuber
"I don't know the rules"
-
Apr 22nd, 2008, 03:24 PM
#12
Re: [RESOLVED] [2005] Help with SQL Update Command
You should make it an ALTERNATE KEY - that's an easy process. Although INSERT's will take longer...
btw - SQL 2000 and SQL 2005 are versions - 2003 is not a version of MS SQL.
-
Apr 22nd, 2008, 03:26 PM
#13
Re: [RESOLVED] [2005] Help with SQL Update Command
Well, run it twice in Query Analyzer and it'll cache the query plan and be pretty quick regardless. The join would be much faster, though, as using NOT IN with a subquery is one of the most inefficient things you can do and I apologize for showing it to you.
It's just so damn easy to type in, though ...
(VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.
-
Apr 22nd, 2008, 03:27 PM
#14
Re: [RESOLVED] [2005] Help with SQL Update Command
I wasn't expecting a report of the timing of the query - I was more interested on seeing the OP's actual execution plan info - like table scan vs index - how many rows were eaten up in each step. Some of that can be pretty influential on how the query will run.
-
Apr 22nd, 2008, 03:30 PM
#15
Thread Starter
Hyperactive Member
Re: [RESOLVED] [2005] Help with SQL Update Command
Yeah I'm retarded, we're running SBS 2003 and SQL 2000
I have learned much today!
Tuber
"I don't know the rules"
-
Apr 22nd, 2008, 03:33 PM
#16
Thread Starter
Hyperactive Member
Re: [RESOLVED] [2005] Help with SQL Update Command
And I knew nothing of this SQL Query Analyzer until 4 minutes ago.
Tuber
"I don't know the rules"
-
Apr 22nd, 2008, 03:35 PM
#17
Re: [RESOLVED] [2005] Help with SQL Update Command
 Originally Posted by tuber
The primary key is just a unique incremented int
Not like I want to harp on this point - and I realize this was not a thread about db design
...but with MS SQL there are some real benefits to not using a unique incremented int key. That's a common thing that ACCESS developers have gotten used to - but with MS SQL having a more natural key is sometimes better.
If MEMBERNUM is supposed to be unique anyway...
And you have lots of WHERE MEMBERNUM=xyz type queries...
Then MEMBERNUM should be the PK.
-
Apr 22nd, 2008, 03:40 PM
#18
Thread Starter
Hyperactive Member
Re: [RESOLVED] [2005] Help with SQL Update Command
I completely agree, and I'm not sure why it was set up the way it was.
One other question:
Why does this not show up in the SQL Designer?
"The SQL Designer does not support the Optional FROM cla etc...
Code:
UPDATE NASE_Records
SET Status = 'I'
FROM NASE_Records NS LEFT JOIN
SAS_Download SD ON SD.MemberNum = NS.MemberNum
WHERE SD.MemberNum IS NULL
Tuber
"I don't know the rules"
-
Apr 22nd, 2008, 03:48 PM
#19
Re: [RESOLVED] [2005] Help with SQL Update Command
I don't use SQL DESIGNER...
And now that you tell me it doesn't support UPDATE/FROM I won't!
UPDATE/FROM is definitely a unique kind of syntax - I'm not sure how many sql-engines allow it.
We use it extensively here because you can do use the JOIN'd table on the right side of the SET xyz= statements - so you can do some very complex updates without a series of sub-queries or building temp tables in advance.
[edit] I just googled a bit and it might be because the Designer is using ODBC ? [/edit]
-
Apr 22nd, 2008, 03:53 PM
#20
Thread Starter
Hyperactive Member
Re: [RESOLVED] [2005] Help with SQL Update Command
Ah okay. Well I'm going to run this off a DTS package after the office closes here. So I hope it works. Thanks again for all the help and the many clarifications.
Tuber
"I don't know the rules"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|