Solved

with command object in VBScript return the identity of inserted row

Posted on 2014-04-04
7
580 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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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
Comment Utility
Somehow the creation of the variable @BlahID of type table seems to have got lost.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
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
Comment Utility
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
Comment Utility
Ok great. You should mark your own comment as the answer for others to see.

Rob.
0
 

Author Closing Comment

by:Rossamino
Comment Utility
This worked.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now