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.
| Author |
Topic |
|
helixpoint
Constraint Violating Yak Guru
291 Posts |
Posted - 2007-03-16 : 09:34:55
|
| here is the scoop.I have a table of projects. I have a second table of ProjectPage. The ProjectPage table is tied to the projects table by a project_id field. What I need to do is add records to the pages table. In that ProjectPage table there is a prioriry id field. So when I do an insert. I need to first see if there are any pages that are tied to the project_id.. If so I have to find the highest priority value for the project_id and add one and then insert the record into the pages table. My code is not working. The Null Value seems priority value seems to create issues too.ALTER PROCEDURE dbo.InsertPage ( @project_ID as int, @page_title AS VARCHAR(100), @page_template_ID int, @content_area AS VARCHAR(2000), @main_image AS VARCHAR(100), @second_image AS VARCHAR(100), @modified_by_ID int )ASINSERT INTO ProjectPage (priority, project_ID, page_title, date_modified, modified_by_ID, page_template_ID, content_area, main_image, second_image)SELECT MAX(priority) + 1, @project_ID, @page_title, GETDATE(), @modified_by_ID, @page_template_ID, @content_area, @main_image, @second_imageFROM ProjectPage AS ProjectPage_1GROUP BY project_IDHAVING (project_ID = @project_ID)ProjectPage Tablepage_ID int Uncheckedproject_ID int Uncheckedpage_title nvarchar(50) Checkeddate_modified smalldatetime Checkedmodified_by_ID int Uncheckedpriority int Checkedpage_template_ID int Uncheckedcontent_area nvarchar(MAX) Checkedmain_image nvarchar(50) Checkedsecond_image nvarchar(50) CheckedProject Tableproject_ID int Uncheckedproject_Desc nvarchar(100) Checkedproject_priority int Uncheckeduserid int CheckedlastModified datetime Checked |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-16 : 09:49:47
|
[code]SELECT MAX(priority) + 1, @project_ID, @page_title, GETDATE(), @modified_by_ID, @page_template_ID, @content_area, @main_image, @second_imageFROM ProjectPage AS ProjectPage_1GROUP BY project_IDHAVING (project_ID = @project_ID)where project_ID = @project_ID[/code] KH |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-16 : 09:55:16
|
| [code]ALTER PROCEDURE dbo.InsertPage ( @Project_ID INT, @Page_Title VARCHAR(100), @Page_Template_ID INT, @Content_Area VARCHAR(2000), @Main_Image VARCHAR(100), @Second_Image VARCHAR(100), @Modified_By_ID INT)ASSET NOCOUNT ONDECLARE @Priority INTSELECT @Priority = MAX(Priority) + 1FROM ProjectPageWHERE Project_ID = @Project_IDIF @Priority IS NULL SET @Priority = 1 -- Change this to any value for lowest initial priorityINSERT ProjectPage ( Priority, Project_ID, Page_Title, Date_Modified, Modified_By_ID, Page_Template_ID, Content_Area, Main_Image, Second_Image )VALUES ( @Priority, @Project_ID, @Page_title, CURRENT_TIMESTAMP, @Modified_By_ID, @Page_Template_ID, @Content_Area, @Main_Image, @Second_Image )[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-16 : 09:55:52
|
| khtan, what if this insert is a new project?Peter LarssonHelsingborg, Sweden |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-16 : 09:59:05
|
quote: Originally posted by Peso khtan, what if this insert is a new project?Peter LarssonHelsingborg, Sweden
Yes. Didn't thought of that  KH |
 |
|
|
helixpoint
Constraint Violating Yak Guru
291 Posts |
Posted - 2007-03-16 : 12:20:09
|
| Any clues??? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-16 : 12:36:04
|
what clues ? Have you tried Peter's code ? KH |
 |
|
|
|
|
|
|
|