Solved

tsql update table on select

Posted on 2012-12-29
4
476 Views
Last Modified: 2012-12-30
i have a table statistics :

with fiELD day, CURRDATE REQPORT  TOTALS

I WANT TO UPDATE TOTALS EVERY ON HOURRS USING SQL AGENT

I HAVE A VIEW SELECT

I WANT TO UPDATE THE VALUE OF TOTALS FROM THIS VIEWS

UPDATE STATISTICS SET TOTALS = ( )
WHERE CURRDATE = XX AND REQPORT =

FROM ( SELECT CURRATE,REQPORT,TOTALS FROM MYVIEW )
0
Comment
Question by:afifosh
  • 2
4 Comments
 
LVL 12

Expert Comment

by:Habib Pourfard
Comment Utility
UPDATE  [STATISTICS]
SET     TOTALS = T.Totals
FROM    (SELECT CURRATE, REQPORT, TOTALS FROM MYVIEW) T
WHERE   CURRDATE = XX
        AND REQPORT = T.REQPORT

Open in new window

0
 
LVL 9

Expert Comment

by:sognoct
Comment Utility
update t1 
set totals =t2.totals
from STATISTICS t1
INNER JOIN (SELECT CURRATE, REQPORT, TOTALS FROM MYVIEW)  t2 ON t1.CURRDATE = t2.currate AND t1.report = t2.reqport

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Or simply:
UPDATE  s
SET     TOTALS = v.TOTALS
FROM    [STATISTICS] s
        INNER JOIN MYVIEW v ON s.REQPORT = v.REQPORT
WHERE   s.CURRDATE = XX 

Open in new window

0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
Comment Utility
If you also need to join to CURRDATE do it this way:
UPDATE  s
SET     TOTALS = v.TOTALS
FROM    [STATISTICS] s
        INNER JOIN MYVIEW v ON s.REQPORT = v.REQPORT AND s.CURRDATE = v.CURRDATE
WHERE   s.CURRDATE = XX 

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
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 shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

771 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

11 Experts available now in Live!

Get 1:1 Help Now