Wrox Programmer Forums
|
ASP Pro Code Clinic As of Oct 5, 2005, this forum is now locked. No posts have been deleted. Please use "Classic ASP Professional" at: http://p2p.wrox.com/forum.asp?FORUM_ID=56 for discussions similar to the old ASP Pro Code Clinic or one of the other many remaining ASP and ASP.NET forums here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP Pro Code Clinic section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 8th, 2004, 11:47 PM
Authorized User
 
Join Date: Apr 2004
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Default help with insert statement

I have the following two insert commands and I'm trying to figure out how I can take the primary key value that is created when the first insert occurs for "cmdAllocHdrInsert" and insert that value as a foreign key value when the second insert occurs for "cmdAlloDetlsInsert"

Any help is appreciated. Thanks,
-Dman100-

<%
if(Session("ddo") <> "") then cmdAllocHdrInsert__varddo = Session("ddo")
if(Session("reqname") <> "") then cmdAllocHdrInsert__varrequestor = Session("reqname")
if(Session("date") <> "") then cmdAllocHdrInsert__vardt = Session("date")
if(Session("AlloABR") <> "") then cmdAllocHdrInsert__vartype = Session("AlloABR")
if(Session("Allosubject") <> "") then cmdAllocHdrInsert__varsubject = Session("Allosubject")
if(Session("Allodescription") <> "") then cmdAllocHdrInsert__vardescription = Session("Allodescription")
if(Session("Alloreview") <> "") then cmdAllocHdrInsert__varreview = Session("Alloreview")
%>
<%
if(Session("yrA" & x) <> "") then cmdAlloDetlsInsert__varyr = Session("yrA" & x)
if(Session("strategyA" & x) <> "") then cmdAlloDetlsInsert__varstrategy = Session("strategyA" & x)
if(Session("BAA" & x) <> "") then cmdAlloDetlsInsert__varBA = Session("BAA" & x)
if(Session("BOA" & x) <> "") then cmdAlloDetlsInsert__varBO = Session("BOA" & x)
if(Session("origBAA" & x) <> "") then cmdAlloDetlsInsert__varorigBA = Session("origBAA" & x)
if(Session("IncrDecrA" & x) <> "") then cmdAlloDetlsInsert__varIncrDecr = Session("IncrDecrA" & x)
if(Session("origAllocation") <> "") then cmdAlloDetlsInsert__varorigallo = Session("origAllocation")
if(Session("reqAllocation") <> "") then cmdAlloDetlsInsert__varreqallo = Session("reqAllocation")
%>
<%
set cmdAllocHdrInsert = Server.CreateObject("ADODB.Command")
cmdAllocHdrInsert.ActiveConnection = MM_DBConn_STRING
cmdAllocHdrInsert.CommandText = "INSERT INTO dbo.DIM_ABR_MASTER_HDR (ABR_ddo, ABR_requestor, ABR_dt, ABR_type, ABR_subject, ABR_description, ABR_review) VALUES ('" + Replace(cmdAllocHdrInsert__varddo, "'", "''") + "','" + Replace(cmdAllocHdrInsert__varrequestor, "'", "''") + "','" + Replace(cmdAllocHdrInsert__vardt, "'", "''") + "','" + Replace(cmdAllocHdrInsert__vartype, "'", "''") + "','" + Replace(cmdAllocHdrInsert__varsubject, "'", "''") + "','" + Replace(cmdAllocHdrInsert__vardescription, "'", "''") + "','" + Replace(cmdAllocHdrInsert__varreview, "'", "''") + "') "
cmdAllocHdrInsert.CommandType = 1
cmdAllocHdrInsert.CommandTimeout = 0
cmdAllocHdrInsert.Prepared = true
cmdAllocHdrInsert.Execute()
%>
<%
set cmdAlloDetlsInsert = Server.CreateObject("ADODB.Command")
cmdAlloDetlsInsert.ActiveConnection = MM_DBConn_STRING
cmdAlloDetlsInsert.CommandText = "INSERT INTO dbo.DIM_ABR_TRNSACT_DETLS (ABR_TRNSACT_APPROP_YR, ABR_TRNSACT_STRATEGY, ABR_BUDG_ACCT, ABR_BUDG_OBJ, ABR_ORIG_BUDG_AMT, ABR_INCR_DECR, ABR_ORIG_ALLO, ABR_REQ_ALLO) VALUES ('" + Replace(cmdAlloDetlsInsert__varyr, "'", "''") + "','" + Replace(cmdAlloDetlsInsert__varstrategy, "'", "''") + "','" + Replace(cmdAlloDetlsInsert__varBA, "'", "''") + "','" + Replace(cmdAlloDetlsInsert__varBO, "'", "''") + "',convert(money,'" + Replace(cmdAlloDetlsInsert__varorigBA, "'", "''") + "'),convert(money,'" + Replace(cmdAlloDetlsInsert__varIncrDecr, "'", "''") + "'),convert(money,'" + Replace(cmdAlloDetlsInsert__varorigallo, "'", "''") + "'),convert(money,'" + Replace(cmdAlloDetlsInsert__varreqallo, "'", "''") + "')) "
cmdAlloDetlsInsert.CommandType = 1
cmdAlloDetlsInsert.CommandTimeout = 0
cmdAlloDetlsInsert.Prepared = true
cmdAlloDetlsInsert.Execute()
%>
<%
Response.Redirect("confirmation.asp")
%>

 
Old December 9th, 2004, 05:57 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

wow, allot of session variables. Session variables should be used very sparingly. Looking at your code IMO - submitting form variables (hidden or not) and querystrings should be thought about for a smarter use of resources.

When I insert a new record and need the new ID (primary key) for further inserts into other tables as a forign key, I run the following sql IMMEDIATLY after the insert:

sql = "SELECT top 1 id FROM tbleName ORDER BY id DESC;"

This RS will contain 1 record being your newly inserted id

Wind is your friend
Matt





Similar Threads
Thread Thread Starter Forum Replies Last Post
Complicated Insert Statement jackie25 SQL Server ASP 1 May 31st, 2006 09:08 AM
Problem With INSERT Statement len1591 ASP.NET 2.0 Basics 3 May 19th, 2006 12:14 PM
Insert Statement Iashia06 Access 0 April 7th, 2006 12:45 PM
Insert into statement not working. alisonchase Access ASP 8 January 16th, 2005 10:52 AM
Problems with my Insert Statement brendan82 Beginning VB 6 1 January 6th, 2004 08:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.