SQL ID from table posts same ID to all other coldfusion entries per transaction

倖福魔咒の 提交于 2019-12-13 13:24:52

问题


How do I trigger my ID from my Transaction table to be the same on all entries to each table per session? I am trying to insert a lot of information to many tables but trying to keep all tables linked together by TransactionID and am struggling on how it creates the TransactionID with the first entry then grabbing that entry and using it on all other table entries.

(This is my insert to create the Transaction. This should automatically create an ID in my Transaction table since ID is my primary key in the transaction table and IsIdentity is yes and increment of one)

<cfquery datasource="Titlesbymail" name="InsertEntry">
 INSERT INTO dbo.Transaction (Type, OwnerType)
 VALUES (
    <cfqueryparam value='NonLeased' cfsqltype='cf_sql_varchar' />
   , <cfqueryparam value='Owner' cfsqltype='cf_sql_varchar' />
 )
</cfquery>

This then creates the transaction table:
ID: 1
Type: NonLeased
OwnerType: Owner

I am trying to figure out how I can keep that same transaction ID to be inserted with my next entries to my other 6 Tables (if the table exists)

<cfquery datasource="Titlesbymail" name="CustomerInsertEntry">
 INSERT INTO dbo.Customer (TransactionID, ID, FirstName, LastName)
 VALUES (
    <cfqueryparam value= **'(ID from Transaction Table)'** cfsqltype='cf_sql_int' />
   , <cfqueryparam value='1' cfsqltype='cf_sql_int' />
   , <cfqueryparam value='#session.checkout.info.firstname_1#' cfsqltype='cf_sql_varchar' />
   , <cfqueryparam value='#session.checkout.info.lastname_1#' cfsqltype='cf_sql_varchar' />
 )
</cfquery>

This is completely new to me and have done lots of research I just keep coming across sql commands of triggers and all but have no idea how that applies with my coldfusion set up like this.


回答1:


When using cfquery you can use the results attribute to return information based on the query that was run. One part of this information is GENERATEDKEY. You can then assign this GENERATEDKEY to a variable and use it later in your code.

So first you need to add the result attribute to your first cfquery.

<cfquery datasource="Titlesbymail" name="InsertEntry" result="transactionResult">
 INSERT INTO dbo.Transaction (Type, OwnerType)
 VALUES (
    <cfqueryparam value='NonLeased' cfsqltype='cf_sql_varchar' />
   , <cfqueryparam value='Owner' cfsqltype='cf_sql_varchar' />
 )
</cfquery>

Then assign a variable to use the GENERATEDKEY from the result (or use the result variable directly)

i.e. <cfset theID = transactionResult.GENERATEDKEY>

Then you use this variable anywhere else in your code.

<cfquery datasource="Titlesbymail" name="CustomerInsertEntry">
 INSERT INTO dbo.Customer (TransactionID, ID, FirstName, LastName)
 VALUES (
    <cfqueryparam value='#theID#' cfsqltype='cf_sql_integer' />
   , <cfqueryparam value='1' cfsqltype='cf_sql_integer' />
   , <cfqueryparam value='#session.checkout.info.firstname_1#' cfsqltype='cf_sql_varchar' />
   , <cfqueryparam value='#session.checkout.info.lastname_1#' cfsqltype='cf_sql_varchar' />
 )
</cfquery>


来源:https://stackoverflow.com/questions/29866864/sql-id-from-table-posts-same-id-to-all-other-coldfusion-entries-per-transaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!