Solved

updating sql table where other tables match

Posted on 2012-12-26
11
130 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
  • 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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38721553
update or delete + insert the rows?
0
 
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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 142

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL Query Syntax Error 9 34
using & in TSQL 18 21
Sql to find top3 for each record 7 18
SQL Maintenance Plan 3 17
     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now