如何将一个很长的整数传递给VBScript中的函数?

时间:2017-10-06 15:23:18

标签: sql sql-server vbscript

我有一个非常长的数字,需要将它用作要执行的SQL语句中的参数。我试图转换那个' testSessionID' string为double或Long,该函数返回Err.description。但是当我使用testSessionID =" 2784863"它工作正常。

这是我的代码:

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

这是我的GetTestSession存储过程:

--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

3 个答案:

答案 0 :(得分:0)

既然你说使用``你说你在第3行示例代码中收到错误的行:

  

TSID = CDbl(testSessionID)

......你不需要这一步......为什么?因为您稍后使用TSID变量来构建要执行的SQL字符串。

  

“exec GetTestSession @TestSessionID ='”&amp; TSID&amp; “'”

将它保留在引号中,因为它确实是你需要的...一个字符串,用于连接到字符串的其余部分,然后将其作为SQL语句执行。

实际上,您甚至将它视为上面SQL语句中的字符串(因为您有单引号)。基于此,您不需要将其作为double或其他任何内容投射到vbScript中。

如果在执行该过程时出现SQL错误,则需要告诉我们存储过程中的变量类型,以及尝试执行时打印的确切错误消息。

答案 1 :(得分:0)

此:

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

应该解释错误。正如@GWR所说,不需要转换为数字数据类型,因此问题就会消失。

testSessionID1由文字初始化(因此我们确切知道来自哪里)。使用命令(而不是字符串常量“exec GetTestSession @TestSessionID ='1030000000000000005'”)只是浪费时间。

更新评论:

声明:

"exec GetTestSession @TestSessionID ='1030000000000000005'"

使用 STRING (数字)。 所有尝试将该字符串转换为Long或Double(通过丢失“”或CDbl / CLng)并将数字串行化为语句是错误的,错误的并且绑定失败。

更新其他评论:

如果“但是当我使用testSessionID =”2784863“它工作正常”是真的,那么转换/科学格式 是问题的原因。

答案 2 :(得分:0)

搞定了。谢谢大家花时间在这个问题上。

我的数据库服务器名称中存在拼写错误,这就是该服务器中不存在特定TestSessionID的原因。这是复制问题。开发人员正在研究这个问题。

再次感谢每一个人看我的问题。基本上代码很好。