Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

with command object in VBScript return the identity of inserted row

Posted on 2014-04-04
7
Medium Priority
?
729 Views
Last Modified: 2014-04-28
the first column of tblBlah is the identity, BlahID.  I'm getting "Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another." I need to know how to do this using a command object in a VBScript if it's possible. Also, it needs to be bulletproof so none of this selecting the max(identity) after the fact. And .executeScalar() doesn't seem to work either.

      sql =       "insert into tblBlah ([This],[That],[TheOther]) "
      sql = sql & "OUTPUT INSERTED.BlahID into @BlahID "
      sql = sql & "select This, 'somevalue', '1234' from tblBlah where BlahID = '1223'"
            
      set cmd = CreateObject("adodb.command")
      with cmd
            .ActiveConnection = conn
            .CommandType = 1
            .CommandTimeout = 60 * 30
            .CommandText = sql
            .Parameters.Append(.CreateParameter("@BlahID", db_int, output_param))
      end with
      set rs = cmd.execute()
      new_order_id = rs("OrderID")
      set rs = nothing

P.S. Hitting MS SQL server 2008.
0
Comment
Question by:Rossamino
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39979230
please try this:
  sql =       "set nocount on insert into tblBlah ([This],[That],[TheOther]) "
      sql = sql & "OUTPUT INSERTED.BlahID into @BlahID "
      sql = sql & "select This, 'somevalue', '1234' from tblBlah where BlahID = '1223'" 

Open in new window

 
next point, to retrieve the parameter...
and the cmd.Execute, you shall be able to retriate it's value:

cmd.Parameteres(@BlahID").value
0
 

Author Comment

by:Rossamino
ID: 39979274
Updated:
"set nocount on . . . "
and
"new_order_id = cmd.Parameters("@OrderID").value"

Getting same error:
Script: insertRecordGetIdentity.vbs
Line: 347
Char: 4
Error: Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
Code: 800A0BB9
Source: ADODB.Command
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39979434
Somehow the creation of the variable @BlahID of type table seems to have got lost.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 65

Expert Comment

by:RobSampson
ID: 39979542
Hi, have a look at the solution here:
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_23479531.html#a21775795

I can't remember that far back, but it looks like SET NOCOUNT ON in the stored procedure itself, then create your parameter first, and set the parameter value second.

Rob.
0
 

Accepted Solution

by:
Rossamino earned 0 total points
ID: 39986452
After having tried all of the suggestions and all of the linked suggestions the only thing I found to work was a stored procedure.

Stored Procedure InsertIntoBlah:
@RecID int

insert into tblBlah ([This],[That],[TheOther])
select This, 'somevalue', '1234' from tblBlah where RecID = @RecID

select cast(scope_identity() as int) as NewRecID

then in the script:

set hdrrs = conn.execute("exec InsertIntoBlah @RecID = " & rows(0,0))
new_rec_id = hdrrs("NewRecID")
hdrrs.close
set hdrrs = nothing
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39986837
Ok great. You should mark your own comment as the answer for others to see.

Rob.
0
 

Author Closing Comment

by:Rossamino
ID: 40026816
This worked.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

972 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