Solved

with command object in VBScript return the identity of inserted row

Posted on 2014-04-04
7
599 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]
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

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.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

803 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