Jeenyusx10
asked on
How to modify an INSERT command within Visual Studio
I would like to retrieve an auto-Increment PK while while inserting a new record. I have tried manipulating the standard tabladapter.insert() command, I have tried adding a new stored procedure as well as a new insert command.. I just learned the Identity_Scope () is used for this purpose..
However, I do not know where to place this bit within existing commands or how to create a new command that incorporates this...
i will not ask any extra questions within this, but I am no expert at this and am learning by doing.
However, I do not know where to place this bit within existing commands or how to create a new command that incorporates this...
i will not ask any extra questions within this, but I am no expert at this and am learning by doing.
If you have not already created I think need to create a sequence and insert the nextval. Information on sequences is linked to below:
http://download.oracle.com/docs/cd/A97630_01/server.920/a96540/statements_615a.htm#2067095
http://download.oracle.com/docs/cd/A97630_01/server.920/a96540/statements_615a.htm#2067095
ASKER
@rajmraji:
Ok I tried that in the query builder for a new SP..
It says Unable to parse query text.. Are you familiar with Visual Studio?
@tahula2:
The PK I am trying to receive is Auto-increment.. So as the insert happens, the PK is made automatically, and I need to retrieve this value.
Ok I tried that in the query builder for a new SP..
It says Unable to parse query text.. Are you familiar with Visual Studio?
@tahula2:
The PK I am trying to receive is Auto-increment.. So as the insert happens, the PK is made automatically, and I need to retrieve this value.
From the question you closed below:
https://www.experts-exchange.com/questions/26441989/Finding-Max-Value-in-an-Int-SQL-Field.html
In your project, double click on your dataset's Designer.vb file, find the adapter's Insert Commmand, like this:
Me._adapter.InsertCommand. CommandTex t = "INSERT INTO `LookupTable` (`rectype`, `KeyValue`, `Datavalue`) VALUES (?, ?, ?)"
Add the following code:
; SELECT SCOPE_IDENTITY()
to the end of the command, like this:
Me._adapter.InsertCommand. CommandTex t = "INSERT INTO `LookupTable` (`rectype`, `KeyValue`, `Datavalue`) VALUES (?, ?, ?); SELECT SCOPE_IDENTITY()"
Then when you insert the record, do this:
Dim ID as Integer =CInt(AJobTableAdapter.Ins ert(Supp_N umLabel2.T ext.ToStri ng, _
Start_DateDateTimePicker.V alue.ToStr ing, _
Property_AddressTextBox.Te xt.ToStrin g, _
Property_CityTextBox.Text. ToString, _
Property_StateTextBox.Text .ToString, _
Property_ZipCodeTextBox.Te xt.ToStrin g, _
Property_DescTextBox.Text. ToString, _
Reo_NumberTextBox.Text.ToS tring, _
Loan_Orig_NumTextBox.Text. ToString, _
Work_ScopeTextBox.Text.ToS tring, _
NotesTextBox.Text.ToString ))
https://www.experts-exchange.com/questions/26441989/Finding-Max-Value-in-an-Int-SQL-Field.html
In your project, double click on your dataset's Designer.vb file, find the adapter's Insert Commmand, like this:
Me._adapter.InsertCommand.
Add the following code:
; SELECT SCOPE_IDENTITY()
to the end of the command, like this:
Me._adapter.InsertCommand.
Then when you insert the record, do this:
Dim ID as Integer =CInt(AJobTableAdapter.Ins
Start_DateDateTimePicker.V
Property_AddressTextBox.Te
Property_CityTextBox.Text.
Property_StateTextBox.Text
Property_ZipCodeTextBox.Te
Property_DescTextBox.Text.
Reo_NumberTextBox.Text.ToS
Loan_Orig_NumTextBox.Text.
Work_ScopeTextBox.Text.ToS
NotesTextBox.Text.ToString
ASKER
It looks fantastic. Ill have to test in the morning. No need to go in depth, but is this possible by using the visual query builder?
Sure, you can, as long as you see the Insert Into command, add the Select SCOPE_IDENTITY() to the end of the command prefixed with a semi-colon ";".
ASKER
Here's what my code looks like...
And I have no idea why It looks so bulky.. Should I literally place SELECT SCOPE_IDENTITY() at the end?
And I have no idea why It looks so bulky.. Should I literally place SELECT SCOPE_IDENTITY() at the end?
Me._adapter.InsertCommand.CommandText = "INSERT INTO [Job] ([Supp_Num], [Start_Date], [Property_Address], [Property_City]," & _
" [Property_State], [Property_ZipCode], [Property_Desc], [Reo_Number], [Loan_Orig" & _
"_Num], [Work_Scope], [Notes]) VALUES (@Supp_Num, @Start_Date, @Property_Address," & _
" @Property_City, @Property_State, @Property_ZipCode, @Property_Desc, @Reo_Number" & _
", @Loan_Orig_Num, @Work_Scope, @Notes);" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "SELECT Property_Num, Supp_Num, Start_Da" & _
"te, Property_Address, Property_City, Property_State, Property_ZipCode, Property_" & _
"Desc, Reo_Number, Loan_Orig_Num, Work_Scope, Notes FROM Job WHERE (Property_Num " & _
"= SCOPE_IDENTITY())"
ASKER
I added it to the end and it still only returns 1 for return value
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Still returns 1...
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It works! Finally. I was actually reading a page just like the one you sent me.. but it wasn't specific enough to tell me where to make the change.. and then I had to reload the dataset because I screwed something up and it produced over 100 errors and each End tag for every function was sqiggly lined..
Anyway, it is returning the appropriate values... Thank you, so..so...so much
Seems like everyone hits and runs these days looking for quick points.. So I really appreciate you stickin through with me
Anyway, it is returning the appropriate values... Thank you, so..so...so much
Seems like everyone hits and runs these days looking for quick points.. So I really appreciate you stickin through with me
Glad I helped. I too learned something in the process, even though I have not had much experience in SQL for about 3 years. :)
select scope_identity(), which will result in the inserted identity being returned as a single row, single column table