Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2008
»
SQL Server Newbies
»
Stuck on new fault with my update
27 posts, Page 1 of 3
1
2
3
»
»»
Stuck on new fault with my update
Rate Topic
Display Mode
Topic Options
Author
Message
alan_lynch
alan_lynch
Posted Wednesday, March 06, 2013 6:57 PM
Valued Member
Group: General Forum Members
Last Login: Thursday, May 16, 2013 11:38 PM
Points: 50,
Visits: 88
Hi Professionals I am running the following query as advised previously which updates the source table based on a column from the reference table matching...
BEGIN TRANSACTION Inner1;
GO
UPDATE dbsource SET software_name_raw = dbref.software_name_amended
FROM dbo.BigTable dbsource
INNER JOIN (
SELECT software_name_raw,software_name_amended
FROM RefTable
GROUP BY software_name_raw,software_name_amended
) dbref
ON dbref.software_name_raw = dbsource.software_name_raw
go
COMMIT TRANSACTION Inner1;
I have run into a problem which is. If they dont match I need to update the reference tables 2 columns with the new unmatched record to reference something like this
ELSE INSERT INTO RefTable(software_name_raw,software_name_amended)
Values BigTable(software_name_raw,’Needs Updating’)
How can or can this be amended easily.
Post #1427718
brad.mason5
brad.mason5
Posted Wednesday, March 06, 2013 7:32 PM
Right there with Babe
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 8:49 AM
Points: 744,
Visits: 572
I would create temp table or CTE and then use that to do insert update. The "ID" field is your PK of the table.
IF OBJECT_ID('tempdb..#Source','u') IS NOT NULL
DROP TABLE #Source
SELECT dbsource.ID
,dbsource.software_name_raw
,dbref.software_name_amended
,ToInsert = CASE WHEN dbref.software_name_raw IS NULL
THEN 1
ELSE 0
END
INTO #Source
FROM dbo.BigTable dbsource
LEFT JOIN (
SELECT software_name_raw,software_name_amended
FROM RefTable
GROUP BY software_name_raw,software_name_amended
) dbref
ON dbref.software_name_raw = dbsource.software_name_raw
UPDATE BigTable
SET software_name_raw = src.software_name_amended
FROM #Source src
JOIN dbo.BigTable
ON BigTable.ID = src.ID
WHERE src.ToInsert = 0
INSERT INTO dbo.RefTable
(software_name_raw
,software_name_amended
)
SELECT software_name_raw
,'Needs Updating'
FROM #Source
WHERE ToInsert = 1
Post #1427722
Lynn Pettis
Lynn Pettis
Posted Wednesday, March 06, 2013 7:45 PM
SSC-Insane
Group: General Forum Members
Last Login: Today @ 12:10 PM
Points: 21,607,
Visits: 27,438
Do you know what would really help, besides direct access to your system? The DDL for the table(s), some sample data for the tables, the expected results of the query you are working on based on the sample data, and all of this in a readily consumable (meaning cut/paste/run in SSMS) format.
It is really hard to provide good answers based on just some code that apparently doesn't really work.
Lynn Pettis
For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here
or
when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here
and
here
Managing Transaction Logs
SQL Musings from the Desert
Fountain Valley SQL
(My Mirror Blog)
Post #1427725
alan_lynch
alan_lynch
Posted Wednesday, March 06, 2013 8:34 PM
Valued Member
Group: General Forum Members
Last Login: Thursday, May 16, 2013 11:38 PM
Points: 50,
Visits: 88
Hi Brad.
The software_name_raw on the RefTable is the Primary Key
Does that help
Post #1427731
Lynn Pettis
Lynn Pettis
Posted Wednesday, March 06, 2013 8:40 PM
SSC-Insane
Group: General Forum Members
Last Login: Today @ 12:10 PM
Points: 21,607,
Visits: 27,438
alan_lynch (3/6/2013)
Hi Brad.
The software_name_raw on the RefTable is the Primary Key
Does that help
For better answers to your questions, read this:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
.
Lynn Pettis
For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here
or
when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here
and
here
Managing Transaction Logs
SQL Musings from the Desert
Fountain Valley SQL
(My Mirror Blog)
Post #1427733
alan_lynch
alan_lynch
Posted Wednesday, March 06, 2013 8:56 PM
Valued Member
Group: General Forum Members
Last Login: Thursday, May 16, 2013 11:38 PM
Points: 50,
Visits: 88
Sorry Lynn
The BigTable.software_name_raw contains
SOFTWARE_NAME_RAW
Microsoft Office 2003 Professional Edition, Office 2003 Professional
Microsoft Office 2003 Professional Enterprise Edition, Office 2003 Professional
Microsoft 2003 Office Professional, Office 2003 Professional
and the RefTable contains a further column
SOFTWARE_NAME_RAW,SOFTWARE_NAME_AMENDED
Microsoft Office 2003 Professional Edition, Office 2003 Professional
Microsoft Office 2003 Professional Enterprise Edition, Office 2003 Professional
Microsoft 2003 Office Professional, Office 2003 Professional
So if the two columns match then update the BigTable with the software_name_amended column from the RefTable
If there is no initial match then I want to insert into the RefTable a new found reference
EG Microsoft 2003 PRO, 'Needs Updating'
Hope this helps
Post #1427736
Lynn Pettis
Lynn Pettis
Posted Wednesday, March 06, 2013 9:02 PM
SSC-Insane
Group: General Forum Members
Last Login: Today @ 12:10 PM
Points: 21,607,
Visits: 27,438
alan_lynch (3/6/2013)
Sorry Lynn
The BigTable.software_name_raw contains
SOFTWARE_NAME_RAW
Microsoft Office 2003 Professional Edition, Office 2003 Professional
Microsoft Office 2003 Professional Enterprise Edition, Office 2003 Professional
Microsoft 2003 Office Professional, Office 2003 Professional
and the RefTable contains a further column
SOFTWARE_NAME_RAW,SOFTWARE_NAME_AMENDED
Microsoft Office 2003 Professional Edition, Office 2003 Professional
Microsoft Office 2003 Professional Enterprise Edition, Office 2003 Professional
Microsoft 2003 Office Professional, Office 2003 Professional
So if the two columns match then update the BigTable with the software_name_amended column from the RefTable
If there is no initial match then I want to insert into the RefTable a new found reference
EG Microsoft 2003 PRO, 'Needs Updating'
Hope this helps
Nope. I don't see any DDL for the table(s), nor insert statements with sample (not real) data, nor even what the expected results are based on the sample data.
Guess I'll move along and see if there are others who I may be able help.
Lynn Pettis
For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here
or
when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here
and
here
Managing Transaction Logs
SQL Musings from the Desert
Fountain Valley SQL
(My Mirror Blog)
Post #1427738
alan_lynch
alan_lynch
Posted Wednesday, March 06, 2013 9:09 PM
Valued Member
Group: General Forum Members
Last Login: Thursday, May 16, 2013 11:38 PM
Points: 50,
Visits: 88
Ok Lynn if thats what you want to do and move on and help someone else then thats fine.
I am new to SqlServer so I dont know how to do the insert statement properly, thats why I asked this in my initial enquiry asking if data can be inserted into the reference table if it does not exist during my update.
I will await Brad's reply he seems to know what I mean
Post #1427740
Lynn Pettis
Lynn Pettis
Posted Wednesday, March 06, 2013 9:12 PM
SSC-Insane
Group: General Forum Members
Last Login: Today @ 12:10 PM
Points: 21,607,
Visits: 27,438
alan_lynch (3/6/2013)
Ok Lynn if thats what you want to do and move on and help someone else then thats fine.
I am new to SqlServer so I dont know how to do the insert statement properly, thats why I asked this in my initial enquiry asking if data can be inserted into the reference table if it does not exist during my update.
I will await Brad's reply he seems to know what I mean
Take the time to read the article I gave you the link to, it will help with everything I asked you to provide.
Lynn Pettis
For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here
or
when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here
and
here
Managing Transaction Logs
SQL Musings from the Desert
Fountain Valley SQL
(My Mirror Blog)
Post #1427741
joeroshan
joeroshan
Posted Wednesday, March 06, 2013 9:18 PM
Mr or Mrs. 500
Group: General Forum Members
Last Login: Yesterday @ 4:12 AM
Points: 573,
Visits: 1,165
Alan,
What you need to do is right click the Both tables in SSMS, select Script table as Create , paste the code in the forum. Otherwise whatever suggestions we give will be based on assumptions, that can give you wrong results.
Lynn is willing to help you and he is one of very active members here.
For your question, If software_name_raw is pk for your RefTable, I donot see a point in using group by in your update statement.
Edit (Reffered Lynn with wrong pronoun. Because we only see their valuable posts , rarely know them in person. Corrected with apologies)
-- Roshan Joe
*******************************************
Jeff Moden -Forum Etiquette: How to post data/code on a forum to get the best help
Custom cleanup script for backups
Post #1427744
« Prev Topic
|
Next Topic »
27 posts, Page 1 of 3
1
2
3
»
»»
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.