-2

I have a number which is very long and need to use it as a parameter in a SQL statement to be executed. I am trying to convert that 'testSessionID' string to double or Long, the function returns Err.description. but when I use testSessionID = "2784863" it works fine.

Here is my code:

testSessionID = "1030000000000000005"
Dim TSID 
TSID = CDbl (testSessionID)
Dim script_testSessionStatus, testSessionStatus
'Getting the TestSession Status of TestSessionID
script_testSessionStatus = ("exec GetTestSession @TestSessionID ='" & TSID & "'")
testSessionStatus = ExecuteSQLStatement_String(script_testSessionStatus, "Value")

Public Function ExecuteSQLStatement_String(Sql_Statement, colname)
    Err.Clear
    On Error Resume Next 
    objRecordSet.open Sql_Statement,objconnection,1
    If Err.Number <> 0 Then
        ExecuteSQLStatement_String = Err.description
        objRecordSet.Close
        Err.Clear
    Else 
        ExecuteSQLStatement_String = objRecordSet.Fields(colname).Value
        objRecordSet.Close
    End If 
    On Error GoTo 0
End Function

Here is my GetTestSession stored procedure:

--Called from GetTestsessions based on input param
go
--Use MfgDB
go
set nocount on
go
-- Begin Create Procedure Script for dbo.GetTestSession
print 'Run Script for Procedure - dbo.GetTestSession'
--go
-- Project       :
-- SQL Objects   : dbo.GetTestSession
-- References To : No Foreign Key References constraints
--
-- Referenced By : Not Referenced by other table Foreign Keys constraints
go
go
------------------------------------------------------------------------
--Create Procedure Script for dbo.GetTestSession
------------------------------------------------------------------------
go
if object_id('[dbo].[GetTestSession]') is not null
    Drop Procedure [dbo].[GetTestSession]
go
CREATE PROCEDURE [dbo].[GetTestSession]
    @TestSessionID          bigint
AS
BEGIN
    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
    SET NOCOUNT ON --always have this on
        --Begin generic part of the stored procedure
    --Do not change this generic block
--  $Date$
--  $Author$
--  $Rev$
--  $URL$
    --Procedure-specific Declaration       
    DECLARE @AttID      bigint

    DECLARE @ts TABLE (
            SuiteID             bigint,
            TestSessionType     varchar(50),
            StartSession        datetime,
            Operator            varchar(100),
             DUTAttributeSetID   bigint,
             SerialNumber        varchar(50),
             Attribute           varchar(50),
             Value               varchar(255),
             Comment             varchar(1000)
            ) 

     --END Procedure-specific Declaration
     --Error/Auditing Declaration
     DECLARE @retVals    RaiseMessages,   -- Error message container
             @dtNow      datetime,        -- Now according to the SP
             @nRes       int,             -- Standard "result" int variable
             @sAudit     nvarchar(max)    -- Audit string; contains this   procedures
 -- parameters with their data
-- inspect and optionally change SET at execution time and
-- disable external started transaction. Check ODBC connection settings.

-- EXEC dbo.xCheckSetup @@ProcID
-------------------------------------
--set start time
-------------------------------------
SELECT  @dtNow  = GETDATE(),
        @nRes   = 0,
        @sAudit = ''

-- Build an audit string for the logging system. This is required for every
-- externally facing stored procedure. This can be autogenerated by running
-- exec apGenerateAuditCode 'apTemplate'
EXEC dbo.xAuditGetTestSession
    @TestSessionID,
    @sAudit OUTPUT
-------------------------------------------------------------------------
--End generic part of the stored procedure
-------------------------------------------------------------------------
--Begin specific part of the stored procedure
-------------------------------------------------------------------------
--scrub data minimize sniffing issues by offsetting to local variables
-------------------------------------------------------------------------
--Initial input value trap
-------------------------------------
INSERT INTO @ts(SuiteID,TestSessionType,StartSession,Operator,DUTAttributeSetID,SerialNumber,Attribute,Value)
SELECT      ts.TestSuiteID,
            tst.TestSessionType,
            ts.Created,
            uc.UserName CreatedBy,
            ts.DUTAttributeSetID,
            d.SerialNumber,
            a.Description Att_Name,
            v.Value Att_Value
FROM        dbo.DUTs d
INNER JOIN  dbo.DUTAttributeSets das
ON          d.DUTID = das.DUTID
INNER JOIN  dbo.TestSessions ts
ON          das.DUTAttributeSetID = ts.DUTAttributeSetID
INNER JOIN  dbo.TestSessionTypes tst
ON          ts.TestSessionTypeID = tst.TestSessionTypeID
INNER JOIN  dbo.TSAttributes ta
ON          ts.TestSessionID = ta.TestSessionID
INNER JOIN  dbo.Attributes a
ON          ta.AttributeID = a.AttributeID
INNER JOIN  dbo.TSValues v
ON          ta.TSValueID = v.TSValueID
INNER JOIN  dbo.Statuses s1
ON          ta.StatusID = s1.StatusID
INNER JOIN  dbo.Users uc
ON          ts.CreatedBy = uc.UserID
WHERE       ts.TestSessionID = @TestSessionID
/*
    INNER JOIN  dbo.vTestSessions ts
    ON          das.DUTAttributeSetID = ts.DUTAttributeSetID
    WHERE       ts.TestSessionID = @TestSessionID
*/
    IF @@ROWCOUNT = 0
        BEGIN
            --This is an Error(nISError=1),Implicit Default no email    (AlertGroup=null), logs the error (LogSuppress=0), and does not return a simple resultset (HasResultSet=0).
            INSERT INTO @retVals(
                            Started,
                            Audit,
                            nIsError,
                            Severity,
                            Operation,
                            Code,
                            InitialMessage,
                            Description
                            )
        SELECT              @dtNow,
                            @sAudit,
                            1,
                            10,
                            'Initialize',
                            'ParameterVal',
                            'Invalid Parameter Value',
                            'Parameter @TestSessionID is not valid'
        EXEC @nRes = dbo.apRaiseMessage @retVals
        RETURN @nRes
    END

    -- -----------------------------------
    -- Main body
    -- -----------------------------------
    BEGIN TRY
    SELECT @AttID = dbo.fn_GetAttributeID('dbo.Tests','Iteration')
-- Expection is that Iteration will only be supplied at EndTestSession, so if Active, there should be no Iteration in TestSession
        IF EXISTS (SELECT * FROM @ts WHERE Attribute = 'Status' AND Value = 'ACTIVE')
        AND @AttID IS NOT NULL
             BEGIN
             INSERT INTO @ts (SuiteID, TestSessionType, StartSession, Operator, DUTAttributeSetID, SerialNumber, Attribute, Value)
            SELECT TOP 1 ts.SuiteID, ts.TestSessionType, ts.StartSession, ts.Operator, ts.DUTAttributeSetID, ts.SerialNumber, 'Iteration', ISNULL(x.Value, 1)
                FROM            @ts ts
                CROSS APPLY (   SELECT      MAX(CAST(tav.Value AS int))  Value
                            FROM        dbo.Tests t
                            INNER JOIN  dbo.TestAttributes ta
                            ON          t.TestID = ta.TestID
                            INNER JOIN  dbo.TAValues tav
                            ON          ta.TAValueID = tav.TAValueID
                            WHERE       t.TestSessionID = @TestSessionID
                            AND         ta.AttributeID = @AttID            
                        ) x
        END

        SELECT      SuiteID, TestSessionType, Operator, StartSession, DUTAttributeSetID, SerialNumber, Attribute, Value
        FROM @ts

END TRY
BEGIN CATCH
    --This is an Error(nISError=1), Explicit Default has email (AlertGroup!=null), logs the error (LogSuppress=0), and does not return a simple resultset (HasResultSet=0).
    INSERT INTO @retVals(
                        Started,
                        Audit,
                        nIsError,
                        Severity,
                        Operation,
                        Code,
                        InitialMessage,
                        Description,
                        HasResultSet,
                        AlertGroup
                        )
    SELECT              @dtNow,
                        @sAudit,
                        1,
                        10,
                        'GetTestSession',
                        'SevereError',
                        'Unexpected Error',
                        'A severe error occurred : "' +    CONVERT(varchar,ERROR_NUMBER()) + '" - ' + ERROR_MESSAGE(),
                        0,
                        'Monitor'
        EXEC @nRes = dbo.apRaiseMessage @retVals
        RETURN @nRes
    END CATCH
--This is NOT an Error(nISError=0),
INSERT INTO @retVals(
                    Started,
                    Audit,
                    nIsError,
                    Operation,
                    Code,
                    InitialMessage,
                    Description
                    )
SELECT              @dtNow,
                    @sAudit,
                    0,
                    'End',
                    'End',
                    'Completed',
                    'Normal Code Execution'
    EXEC @nRes = dbo.apRaiseMessage @retVals
    RETURN @nRes

 END
 go
 ------------------------------------------------------------------------
 -- End Create Procedure Script
------------------------------------------------------------------------
go
go
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Maddy
  • 37
  • 1
  • 12
  • What is the error message? May be the DB is throwing error as double can handle such long values – Pankaj Jaju Oct 06 '17 at 15:40
  • A `BIGINT` should convert to a double ok, with `CDbl()`. I would be inclined to not use `On Error Resume Next` until you're positive what is being returned *([mentioned this](https://stackoverflow.com/a/46600082/692942) in [your other question](https://stackoverflow.com/q/46595963/692942))*, just assuming because you pass a larger value it breaks is dangerous. It may be related, but not necessarily the cause being assuming. – user692942 Oct 06 '17 at 15:54
  • @Lankymart - I suspect that the `GetTestSession` procedure is unable to handle such a big number. He should check the vartype for that procedure's parameter. – Pankaj Jaju Oct 06 '17 at 15:58
  • @PankajJaju like I already said, assumption is dangerous. – user692942 Oct 06 '17 at 16:00
  • Very true. He need to debug the suspect piece of code as you said. – Pankaj Jaju Oct 06 '17 at 16:01
  • The line you say you are getting the error on... line 3 of your sample code, you don't need this step... since you are just building a string of SQL to execute ("exec GetTestSession @TestSessionID ='" & TSID & "'") Keep it in the quotes, since it's really what you need... a string to concatenate to the rest of your string, which you then execute. – GWR Oct 06 '17 at 16:14
  • If I remove On Error Resume Next, I immediately get an error as soon as i ObjRecordset.open. This is the error i am getting: Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done. Line (103): "objRecordSet.open Sql_Statement,objconnection,1". – Maddy Oct 06 '17 at 16:26
  • Try to execute the SQL procedure manually with the big value and see if that works – Pankaj Jaju Oct 06 '17 at 16:31
  • 1
    Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Oct 06 '17 at 16:49
  • Yes my sql commands when I run them manually they work fine. my sql statement get executed without any error if testSessionID = "2784863", a small integer. The problem is if testSessionID is a very big number, my SQL statement is not forming correctly. – Maddy Oct 06 '17 at 16:59
  • Have you ran the sql command directly on your DB (using the big number)? – Pankaj Jaju Oct 06 '17 at 17:02
  • Yes. Yes @Pankaj my sql commands when I run them manually they work fine. exec GetTestSession (at symbol)TestSessionID ='1020000000000000005' or exec GetTestSession (at symbol)TestSessionID = 1020000000000000005 These two sql procedures when run manually execute properly. – Maddy Oct 06 '17 at 17:05
  • The Multiple-step OLE DB operation is likely because you don't use `SET NOCOUNT ON;` in the stored procedure which means ADODB will see two results returned, a closed recordset followed by the result of the query. Also different providers behave differently, so check your using the correct one in your connection string. – user692942 Oct 06 '17 at 17:33
  • I'd also suggest you move to using [`ADODB.Command` to build parameterised queries](https://stackoverflow.com/a/21944948/692942). – user692942 Oct 06 '17 at 17:39
  • Also, this might be relevant - https://stackoverflow.com/a/23803535/692942 – user692942 Oct 06 '17 at 20:58
  • This question is a classic example of a question not including enough information to answer. Worth reviewing [ask] before posting next time. – user692942 Oct 06 '17 at 21:08
  • @user2329418 hmm...that is not a straightforward stored procedure. – user692942 Oct 09 '17 at 21:21

3 Answers3

0

Since you said using `` The line you say you are getting the error on... line 3 of your sample code:

TSID = CDbl (testSessionID)

...you don't need this step... Why? Because you are later using the TSID variable to just build a string of SQL to execute.

"exec GetTestSession @TestSessionID ='" & TSID & "'"

Keep it in the quotes, since it's really what you need... a string to concatenate to the rest of your string, which you then execute as SQL statement.

In fact you are even treating it as a string in the SQL statement above (as you have single quotes around it). Based on that, you don't need to cast it as a double, or anything else in the vbScript.

If you get SQL errors at the point at which you execute that procedure, then you will need to tell us your variable types in the stored procedure, as well as the exact error message that is printing when you try to execute it.

GWR
  • 1,878
  • 4
  • 26
  • 44
  • 1
    I wouldn't encourage this approach at as its open to SQL Injection and we don't know where `testSessionID` comes from. A better approach would be to use a `ADODB.Command` object to build a parameterised query. – user692942 Oct 06 '17 at 17:28
  • 1
    100% agree. Will edit the answer when I get home later – GWR Oct 06 '17 at 18:29
0

This:

>> s = "1030000000000000005"
>> l = CLng(s)
>>
Error Number:       6
Error Description:  Überlauf
>> d = CDbl(s)
>> WScript.Echo TypeName(d), d
>>
Double 1,03E+18
>>

should explain the error(s). As @GWR said, a conversion to a numeric data type is not needed, so the problem vanishes.

testSessionID1 is initialized by a literal (so we know exactly where is comes from). Using a Command (instead of the string constant "exec GetTestSession @TestSessionID ='1030000000000000005'") is just a waste of time.

Update wrt comment:

The statement:

"exec GetTestSession @TestSessionID ='1030000000000000005'"

works with a STRING (of digits). All attempts that try to convert that string to a Long or a Double (by loosing the "" or CDbl/CLng) and stringifying the number into the statement are misguided, wrong, and bound to fail.

Update wrt other comment(s):

If "but when I use testSessionID = "2784863" it works fine" is true, then the conversion/scientific format is the cause of the problem.

Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • Dim testSessionID testSessionID = "1020000000000000005" Dim script_testSessionStatus, testSessionStatus 'Getting the TestSession Status of TestSessionID script_testSessionStatus = ("exec GetTestSession @TestSessionID ='" & testSessionID & "'") – Maddy Oct 06 '17 at 20:20
  • ignore my comment above. Even If I take the testSessionID as a string, I am getting the same error. If I don't put the quotes around testSessionID, then the testSessionID will look like:"exec GetTestSession (at symbol)TestSessionID ='1.02E+18'" Because of this number 1.02E+18 my sqlstatement is not forming correctly. Can someone please help? I am stuck and not able to proceed since two days. – Maddy Oct 06 '17 at 20:40
  • @ekkehard In that example, yes but you think that's where the `testSessionID` will eventually come from? Don't be so naive!! The error has nothing to do with them passing the value as string, the error is coming from the ADODB provider complaining about a `Multiple-step OLE DB operation`. – user692942 Oct 06 '17 at 20:44
  • @user2329418 GWR and Ekkehard are both right about the value you should be fine to pass it as a string, that is not your issue though. Did you try what I suggested in [this comment](https://stackoverflow.com/questions/46609077/how-to-pass-a-very-long-integer-to-a-function-in-vbscript#comment80174110_46609077)? – user692942 Oct 06 '17 at 20:49
  • @ekkehard judging from [your edit revision](https://stackoverflow.com/revisions/46613246/3) i'm guessing your missing the point and choosing to take my comment personally, which is entirely up to you, but it wasn't meant as an attack. – user692942 Oct 06 '17 at 21:02
  • @user2329418 it would help if you [edit] your question and add the definition of the `GetTestSession` stored procedure. – user692942 Oct 06 '17 at 21:04
  • using ADODB.Command object. Public Function ........ Err.Clear objCommand.ActiveConnection = objconnection objCommand.CommandText = Sql_Statement objCommand.CommandType = 4 Set objRecordSet = objCommand.Execute If Err.Number <> 0 Then ExecuteSQLStatement_String = Err.description objRecordSet.Close Err.Clear Else ....... End ... The error is : Syntax error or access violation Line (107) : "Set objRecordSet = objCommand.Execute" – Maddy Oct 09 '17 at 16:34
  • @Lankymart , I have edited my question and added the GetTestSession stored Procedure. – Maddy Oct 09 '17 at 17:12
0

Got it working. Thank you all for spending time on this question.

There was a typo in my database server name and that's the reason that particular TestSessionID was not there in that server. It was replication problem. Devs are working on that.

Thanks once again to each and every one to look at my question. Basically the code it good.

Maddy
  • 37
  • 1
  • 12