Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

One form Populating Multiple SQL Tables

Posted on 2008-11-18
11
Medium Priority
?
259 Views
Last Modified: 2013-12-24
I have a Transaction form that needs to populate the Transaction and TransactionDetail Tables in SQL Server 2005. The tables look like the following (Obviously trimmed down the field lists):

Transaction:
UNID - Primary Key - Autonumber
FName
LName

TransactionDetail
UNID - Primary Key - Autonumber
T_UNID - Transaction Table UNID
FuelCard

What I want to do is populate both tables. I need to get the Transaction Table UNID and place it into the TransactionDetail tables T_UNID field. I guess I could do this with a select to and then populate the id with another insert into the TransactionDetail table. Just wasn't sure if there was a better way. I know that when your in access you can create forms with the use of a subform and populate multiple tables and the id would appear correctly in the TransactionDetail table.
0
Comment
Question by:Michael Williams
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 7

Assisted Solution

by:black0ps
black0ps earned 400 total points
ID: 22988384
INSERT INTO FirstTable (COLUMNS) VALUES (#VALUES#);
SELECT @@Identity AS FirstTableUNID;
INSERT INTO SecondTable (FirstTableID, OtherColumns) VALUES (FirstTableUNID, #OtherColumns#);

You insert the first table information, select the unique id created and use that in the second table insert.
0
 
LVL 25

Accepted Solution

by:
DBAduck - Ben Miller earned 800 total points
ID: 22989682
You would need to use the identity value.

DECLARE @idvalue int
INSERT INTO firsttable (columns) values (values)
SELECT @idvalue = SCOPE_IDENTITY()
INSERT INTO secondtable (FirstTableID, columns) values (@idvalue, othervalues)

You need to pull the SCOPE_IDENTITY() value into a variable so that you can use it for the second table insert.
0
 
LVL 7

Expert Comment

by:black0ps
ID: 22990065
The way I did it wouldn't work?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 25

Expert Comment

by:DBAduck - Ben Miller
ID: 22990087
No, your statement does this:

SELECT @@identity as FirstTableID
which would return it as FirstTableID and not be in a variable for use in the second statement.  @@identity could be error prone and the correct way is to use SCOPE_IDENTITY() because it is scoped to the current session, where @@IDENTITY is a GLOBAL variable and could be affected by triggers, etc.

0
 

Author Comment

by:Michael Williams
ID: 22994251
dbaduck, that code worked perfectly.

My next question would be iwhere should I put this code ?

I'm creating a form via Dreamweaver and using coldfusion CFC's to insert the data. Would the best practice be to insert the data into a temp table and then process it on the SQL Server side via a stored procedure ? Or should I go ahead and process it at client side ?
0
 
LVL 25

Expert Comment

by:DBAduck - Ben Miller
ID: 23001351
Well, the real answer to this is that you would be best off to create a stored procedure and then call it from the Cold Fusion side of things with parameters.

Then the code could simply use the variables that are passed in and the code above would be just part of it.

Can that work for you?  Do you have to have inline inserts?
0
 

Author Comment

by:Michael Williams
ID: 23004126
That should be fine..

Is there  a way to reverse any inserts that happen ? Or would you just have to do a delete row ?
0
 
LVL 25

Expert Comment

by:DBAduck - Ben Miller
ID: 23004717
Well, if you wrap it in a transaction then you could rollback.

BEGIN TRAN

.... statements

IF condition to commit  AND @@TRANCOUNT = 1
   COMMIT
ELSE
   ROLLBACK -- this would completely rollback everything that was done since the BEGIN TRAN.

The reason you check @@trancount is to make sure that you only have one BEGIN TRAN.  If you have multiples and @@TRANCOUNT > 1 is that when you issue a COMMIT TRAN it decrements the @@TRANCOUNT by 1 so if you have it at 2 then you would have to issue 2 COMMITs and if you have more than 1 then you could have a bigger problem and you should resolve it before you decide to commit that many times.  Anyway, that is a bigger discussion, but thought you should be aware.
0
 
LVL 8

Assisted Solution

by:eszaq
eszaq earned 800 total points
ID: 23010021
ColdFusion has <cftransaction> tag
If you wrap your queries in it, black0ps's version with "SELECT @@Identity AS FirstTableUNID" should work just as fine.

Of course, you can use pure SQL solution - that would mean stored procedure. Generally it is considered to be safer.
0
 

Author Comment

by:Michael Williams
ID: 23028555
I was going to use the CODE-A, but decided to use CODE-B. With using a combination of CFTransaction and CFTry I'm about to make sure I can isert data into 2 tables. When I receive an error I can use <cftransaction action = "rollback"/>.  I've tested this code and it works perfectly. CODE-B will be placed into a CFComponent.

Used the following url for help with setting up my CF tags.
http://www.experts-exchange.com/Software/Server_Software/Web_Servers/ColdFusion/Q_23119839.html

Thanks for all of your help.
CODE-A
DECLARE @idvalue int
INSERT INTO [000-TEST] ([000_A], [000_B])
VALUES ('Damaged Goods', 'TEST');
SELECT @idvalue = SCOPE_IDENTITY()
SELECT [000_TESTID], [000_a], [000_B] FROM [000-TEST] WHERE [000_TESTID] = @idvalue;
 
CODE-B
<cfset commitIt = "Yes">
<cftransaction action = "begin">
  <cftry>
  <cfquery name = "Transaction" result="Transactionresult" dataSource = "#application.datasource#">
    INSERT INTO [000-TEST] ([000_A], [000_B])
    VALUES ('NEW', 'TETE');
  </cfquery>
  
  <cfquery name = "TransactionDetail" result="TransactionDetailresult" dataSource = "#application.datasource#">
    INSERT INTO [001-SECOND] ([001_TESTID], [001_B])
    VALUES (#Transactionresult.identitycol#, 'TEST');
  </cfquery>
  
  <cfcatch type = "DATABASE">
    <cftransaction action = "rollback"/>
    <cfset commitIt = "No">
    <cfoutput>
    	<cfdump var="#cfcatch#">
	</cfoutput>
  </cfcatch>
  </cftry>
 
  <cfif commitIt>
	<cfdump var="#Transactionresult#" />
  	<cfoutput>#Transactionresult.IDENTITYCOL#</cfoutput>
    <cftransaction action = "commit"/>
  </cfif>
</cftransaction>

Open in new window

0
 

Author Closing Comment

by:Michael Williams
ID: 31518003
I took pieces of each of the responses to identify my final coding solution.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

572 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