Solved

with command object in VBScript return the identity of inserted row

Posted on 2014-04-04
7
646 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

734 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