Solved

updating sql table where other tables match

Posted on 2012-12-26
11
136 Views
Last Modified: 2013-01-01
hi guys,

I have the following statement:

This creates a new table putting together pieces of 2 tables where 2 conditions exist.

Now I that the table exists... the second time I run this I just want to update the values.
Can someone please help me change this query from a create new table... to update the existing table.

Appreciate it
thanks


SELECT 
	dbo.PositionsDB.Account_ID, dbo.PositionsDB.Position_ID, dbo.AccountsDB.Account_Number, 
	dbo.AccountsDB.Account_Description,	dbo.PositionsDB.COB_Date, dbo.PositionsDB.Qty_Net, 
	dbo.PositionsDB.Exchange, dbo.PositionsDB.Product, dbo.PositionsDB.Contract_Date, 
	dbo.PositionsDB.Option_Type, dbo.PositionsDB.Strike,
	dbo.PositionsDB.Settlement_Price
INTO dbo.DailyPositions_JEFF
FROM dbo.PositionsDB
INNER JOIN dbo.AccountsDB
ON dbo.PositionsDB.Account_ID = dbo.AccountsDB.Account_ID
WHERE dbo.AccountsDB.Client_ID = '418bb7b8-0180-4a5a-99f6-ce52e60e694f'
AND [COB_Date] = '20121219'

Open in new window

0
Comment
Question by:solarissf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
11 Comments
 
LVL 11

Accepted Solution

by:
David Kroll earned 350 total points
ID: 38721538
IF NOT EXISTS
   (  SELECT [name]
      FROM sys.tables
      WHERE [name] = 'DailyPositions_JEFF'
   )
   BEGIN
SELECT
      dbo.PositionsDB.Account_ID, dbo.PositionsDB.Position_ID, dbo.AccountsDB.Account_Number,
      dbo.AccountsDB.Account_Description,      dbo.PositionsDB.COB_Date, dbo.PositionsDB.Qty_Net,
      dbo.PositionsDB.Exchange, dbo.PositionsDB.Product, dbo.PositionsDB.Contract_Date,
      dbo.PositionsDB.Option_Type, dbo.PositionsDB.Strike,
      dbo.PositionsDB.Settlement_Price
INTO dbo.DailyPositions_JEFF
FROM dbo.PositionsDB
INNER JOIN dbo.AccountsDB
ON dbo.PositionsDB.Account_ID = dbo.AccountsDB.Account_ID
WHERE dbo.AccountsDB.Client_ID = '418bb7b8-0180-4a5a-99f6-ce52e60e694f'
AND [COB_Date] = '20121219'
END
ELSE
BEGIN
--update statements here
END
0
 

Author Comment

by:solarissf
ID: 38721542
as I'm am completed new to this... the section where you have
--update statements here

Can you show me 1 example of updating 1 column with my inner join statements

I'm stuck there as well

thanks soo much
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38721553
update or delete + insert the rows?
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 11

Expert Comment

by:David Kroll
ID: 38721558
UPDATE dbo.DailyPositions_JEFF
SET
Settlement_Price = 0
FROM dbo.DailyPositions_JEFF
INNER JOIN dbo.AccountsDB
ON dbo.DailyPositions_JEFF.Account_ID = dbo.AccountsDB.Account_ID
ANDdbo.AccountsDB.Client_ID = '418bb7b8-0180-4a5a-99f6-ce52e60e694f'
AND [COB_Date] = '20121219'
0
 

Author Comment

by:solarissf
ID: 38721573
i tried this... still not working... at least for the 1 column

	UPDATE dbo.DailyPositions_JEFF
	SET
	Settlement_Price = dbo.PositionsDB.Settlement_Price
	FROM dbo.DailyPositions_JEFF
	INNER JOIN dbo.AccountsDB 
	ON dbo.DailyPositions_JEFF.Account_ID = dbo.AccountsDB.Account_ID 

Open in new window

0
 
LVL 11

Expert Comment

by:David Kroll
ID: 38721585
UPDATE dbo.DailyPositions_JEFF
      SET
      Settlement_Price = dbo.PositionsDB.Settlement_Price
      FROM dbo.DailyPositions_JEFF
      INNER JOIN dbo.PositionsDB
      ON dbo.DailyPositions_JEFF.Account_ID = dbo.PositionsDB.Account_ID
0
 

Author Comment

by:solarissf
ID: 38721767
i'm losing my mind here.

I'm not sure if I found source or not.

Lets assume that the table columns are there but there is not data in dbo.DailyPositions_JEFF.

I am trying to populate these columns
      dbo.PositionsDB.Account_ID, dbo.PositionsDB.Position_ID,      dbo.AccountsDB.Account_Number,
      dbo.AccountsDB.Account_Description,      dbo.PositionsDB.COB_Date, dbo.PositionsDB.Qty_Net,
      dbo.PositionsDB.Exchange, dbo.PositionsDB.Product, dbo.PositionsDB.Contract_Date,
      dbo.PositionsDB.Option_Type, dbo.PositionsDB.Strike,
      dbo.PositionsDB.Settlement_Price


.  So using your examples I just have to UPdate the columns.  This is the part I'm stuck on .
to make things a bit more confusing... and maybe the source of the problem
Account_ID is a unique identifier from AccountsDB
and Position_ID is a unique identifier from PositionsDB

is this why your update statement is not working... am I supposed to be updating all of them a tthe same time instead of each column?
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 150 total points
ID: 38721799
0
 

Author Comment

by:solarissf
ID: 38721807
thanks , i'll check it out
0
 

Assisted Solution

by:solarissf
solarissf earned 0 total points
ID: 38724671
so the reason I couldnt get any results is because my code was incorrect.

I should have been using INSERT INTO since I started with a blank table. .. duhhh, my fault
That combined with your help has this issue solved.

Thanks!!!
0
 

Author Closing Comment

by:solarissf
ID: 38734232
added to solution
0

Featured Post

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question