Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-06-24
2
Medium Priority
?
203 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 200 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

926 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