Solved

Using SQL Insert and want some fields to be inserted with zero balance

Posted on 2008-06-24
2
197 Views
Last Modified: 2010-04-21
How would I get all 'SUM' (sumcurf, sumcurfe, sumcurfea etc.) fields to be inserted with zero balances?
I know  I could run a update after the table is rebuilt to zero out these fields but why not do it as you rebuild table.



truncate table dbo.tbl_ps_combine2

insert INTO dbo.tbl_ps_combine2

select [Functional Reporting], ExpenseLevel3, costelement, OffAcname, [Cost Ctr], per, obcur, LEVEL3, LEVEL4, LEVEL5, LEVEL6, LEVEL7, LEVEL8, Alias,
                      abbreviate, acctdesc, , sumcurfean, sumcurfeanc, sumprev3f, sumprev3fe, sumprev3fea, sumprev3fean, sumprev3feanc,
                       sumytdf, sumytdfe, sumytdfea, sumytdfean, sumytdfeanc, objcurrsum, counter
FROM         dbo.tbl_ps_combine1  
WHERE (per = @pmonth) AND (([Functional Reporting] = @pfunction) OR (@pfunction IS NULL))
AND ((LEVEL3 = @plevel3)  OR (@plevel3 IS NULL)) AND ((LEVEL4 = @plevel4) OR (@plevel4 IS NULL)) AND ((LEVEL5 = @plevel5) OR (@plevel5 IS NULL))
AND ((LEVEL6 = @plevel6) OR (@plevel6 IS NULL)) AND ((LEVEL7 = @plevel7) OR (@plevel7 IS NULL)) AND ((LEVEL8 = @plevel8) OR (@plevel8 IS NULL))  

0
Comment
Question by:thayduck
[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
2 Comments
 
LVL 2

Accepted Solution

by:
dbanttari earned 50 total points
ID: 21860847
You have to use a field list in your INSERT clause, then put literals in the SELECT:

insert INTO dbo.tbl_ps_combine2 ([Functional Reporting], ExpenseLevel3, costelement, OffAcname, [Cost Ctr], per, obcur, LEVEL3, LEVEL4, LEVEL5, LEVEL6, LEVEL7, LEVEL8, Alias,
                      abbreviate, acctdesc, , sumcurfean, sumcurfeanc, sumprev3f, sumprev3fe, sumprev3fea, sumprev3fean, sumprev3feanc,
                       sumytdf, sumytdfe, sumytdfea, sumytdfean, sumytdfeanc, objcurrsum, counter, sumcurf, sumcurfe, sumcurfea)

select [Functional Reporting], ExpenseLevel3, costelement, OffAcname, [Cost Ctr], per, obcur, LEVEL3, LEVEL4, LEVEL5, LEVEL6, LEVEL7, LEVEL8, Alias,
                      abbreviate, acctdesc, , sumcurfean, sumcurfeanc, sumprev3f, sumprev3fe, sumprev3fea, sumprev3fean, sumprev3feanc,
                       sumytdf, sumytdfe, sumytdfea, sumytdfean, sumytdfeanc, objcurrsum, counter
,0 ,0 ,0
FROM         dbo.tbl_ps_combine1  
WHERE (per = @pmonth) AND (([Functional Reporting] = @pfunction) OR (@pfunction IS NULL))
AND ((LEVEL3 = @plevel3)  OR (@plevel3 IS NULL)) AND ((LEVEL4 = @plevel4) OR (@plevel4 IS NULL)) AND ((LEVEL5 = @plevel5) OR (@plevel5 IS NULL))
AND ((LEVEL6 = @plevel6) OR (@plevel6 IS NULL)) AND ((LEVEL7 = @plevel7) OR (@plevel7 IS NULL)) AND ((LEVEL8 = @plevel8) OR (@plevel8 IS NULL))  
0
 

Author Closing Comment

by:thayduck
ID: 31470373
Worked like a charm. Thanks for your quick help.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

734 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