Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 sum() insert help.

Author  Topic 

shelbyoh
Starting Member

14 Posts

Posted - 2010-04-13 : 15:00:38
Is it possible to insert a unique id with sum()


I have a 2 sproc

first sproc go something like this.

 

declare @p_Project_ID int, @Indentity1 int
insert into Projects(Project_ID, ProjectName, ProjectNumber)
--@@Identity(Project_id);store it in local
values(52, 'MEDical start1', '5465464546541')
set @p_Project_ID = @@IDENTITY
insert into PersonnelSummary (Date, Cleveland_Resident, DailyWork, Employee_Name, Gender, Minority, Position, Present, SubContractor, Project_ID, TradeContractor)
values (04/02/2010, 1, 'woking', 'testing', 'male', 'yes', 'woking', 1, 'shelyb', @@IDENTITY, 'shelby')
insert into Narrative (Date, ContructionManager, Location, MonitorSummary, MIM, MIF, MAF, MAM, Project_ID)
Values (04/02/2010, 'tomas', 'cmc', 'testing1', 4,5,4,5, @p_Project_ID)

second

insert into dbo.Manpower(Minority_Employees, Female_Employees, Total_Employees, Cleveland_Residents)
Select SUM(Narrative.MIM),Sum(Narrative.MIF), SUM(Narrative.MAF + Narrative.MAM + Narrative.MIF + Narrative.MIM), SUM(PersonnelSummary.Cleveland_Resident)
from Narrative, PersonnelSummary,



which displays in show table page. The problem I have a Project_ID int not null column in all the tables.

The first one insert the Project_ID in all the tables it works perfectly except when it the same id i get an error.

I need for it to place the Project_ID from the Narrative.Project_ID into the Manpower.Manpower_ID

Please if you can not give me an example don't respond to this plea for help.

I work off example.

which displays in show table page. The problem I have a Project_ID int not null column in all the tables.



The first one insert the Project_ID in all the tables it works perfectly except when it the same id i get an error.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-13 : 15:02:04
Please post the error as well as sample data to explain your issue more clearly.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-14 : 00:02:24
If I understand your question correctly, the problem what you're facing is because you're aggregating (taking the SUM()) while inserting to manpower. Then you wont be able to return id value also along with it unless you apply an aggregation function over or GROUP BY id. But unless we know what you're trying to achieve here we will not be able to suggest you a solution. May be you could explain us with some sample output.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2010-04-14 : 08:45:28
Start with changing @@IDENTITY to SCOPE_IDENTITY()

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page
   

- Advertisement -