Solved

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

Posted on 2008-06-24
2
192 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
2 Comments
 
LVL 2

Accepted Solution

by:
dbanttari earned 50 total points
Comment Utility
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
Comment Utility
Worked like a charm. Thanks for your quick help.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

743 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

16 Experts available now in Live!

Get 1:1 Help Now