0
sql insert question
New Here
,
/t5/coldfusion-discussions/sql-insert-question/td-p/855218
Dec 11, 2008
Dec 11, 2008
Copy link to clipboard
Copied
I am trying to do a simple insert :
<cfquery name="qryInsert" datasource="dbName">
insert into table
(primaryID,
partNumber,
txnDate
quantity,
lineItem)
values(
'#primaryID#',
'#form.partNumer#',
'#form.txnDate#',
'#form.quantity#".
'#form.lineItem#')
</cfquery>
What I want/need to do is to check to see if the lineItem is aleady in the table. If it is there, then that record will not be inserted. If the line item is not in the table, then insert the record.
What is the propery sql syntax to check for this, and where would it go, inside this query/insert ?
<cfquery name="qryInsert" datasource="dbName">
insert into table
(primaryID,
partNumber,
txnDate
quantity,
lineItem)
values(
'#primaryID#',
'#form.partNumer#',
'#form.txnDate#',
'#form.quantity#".
'#form.lineItem#')
</cfquery>
What I want/need to do is to check to see if the lineItem is aleady in the table. If it is there, then that record will not be inserted. If the line item is not in the table, then insert the record.
What is the propery sql syntax to check for this, and where would it go, inside this query/insert ?
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
LEGEND
,
/t5/coldfusion-discussions/sql-insert-question/m-p/855219#M78861
Dec 11, 2008
Dec 11, 2008
Copy link to clipboard
Copied
New York Guy wrote:
> What is the propery sql syntax to check for this, and where would it go,
> inside this query/insert ?
>
You have two choices, you could put a unique index on the lineItem field
in the database, then the database will throw an error if any duplicate
lineItem value is attempted to be inserted. A <cftry><cfcatch> can
easily be used to capture this exception and handle it appropiatly.
OR
You can query the database with a separate select query and then make an
if comparison to see whether to insert the record or not depending on if
a record is returned in the first query.
> What is the propery sql syntax to check for this, and where would it go,
> inside this query/insert ?
>
You have two choices, you could put a unique index on the lineItem field
in the database, then the database will throw an error if any duplicate
lineItem value is attempted to be inserted. A <cftry><cfcatch> can
easily be used to capture this exception and handle it appropiatly.
OR
You can query the database with a separate select query and then make an
if comparison to see whether to insert the record or not depending on if
a record is returned in the first query.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Advisor
,
LATEST
/t5/coldfusion-discussions/sql-insert-question/m-p/855220#M78862
Dec 11, 2008
Dec 11, 2008
Copy link to clipboard
Copied
You might wrap your SQL logic in a stored procedure that is
responsible for checking for the existence of record before running
the insert.
What database (version and vendor) are you using?
What database (version and vendor) are you using?
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

