Forum Discussion

georgiadonut's avatar
georgiadonut
New Contributor
8 years ago
Solved

Newid() function

Hello All -- I am very new to TestComplete and am trying to do a database insert using newid() but keeping getting the error "NEWID" is not defined.  

 

I've tried various different formatting (single quotes, double quotes, etc) as well as creating a variable to store the uniqueidentifier but always get same error.

 

Is there a special library I need to include to use this SQL function or special formating?

 

Thank you in advance for your help.

 

query = "INSERT INTO "+ProjectSuite["Variables"]["PrimaryDB"]+".[dbo].[Workstations](ID,[LocID],[Name])VALUES('"+newid()+"','"+Project["Variables"]["PrimaryLocID"]+"','"+Project["Variables"]["Workstation"]+"')";

  • Hi,

     

    Your code tries to call newid() function, get its result and use this result as a value in the query. As newid() is an internal SQL function, TestComplete cannot call it and reports this as an error.

    You must include the call to the newid() function into your SQL statement so that it is SQL engine that will call it in its context and use the returned result. Something like this:

    query = "INSERT INTO "+ProjectSuite["Variables"]["PrimaryDB"]+".[dbo].[Workstations](ID,[LocID],[Name])VALUES(newid(),"+Project["Variables"]["PrimaryLocID"]+"','"+Project["Variables"]["Workstation"]+"')";

     

    P.S. For better visual clarity I prefer to use the aqString.Format() function. The above line in this case looks like this:

    query =
      aqString.Format("INSERT INTO %s.[dbo].[Workstations] (ID, [LocID], [Name]) VALUES (newid(), %s, %s)",
        ProjectSuite["Variables"]["PrimaryDB"],
        Project["Variables"]["PrimaryLocID"],
        Project["Variables"]["Workstation"] );
    

     

     

4 Replies

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    Your code tries to call newid() function, get its result and use this result as a value in the query. As newid() is an internal SQL function, TestComplete cannot call it and reports this as an error.

    You must include the call to the newid() function into your SQL statement so that it is SQL engine that will call it in its context and use the returned result. Something like this:

    query = "INSERT INTO "+ProjectSuite["Variables"]["PrimaryDB"]+".[dbo].[Workstations](ID,[LocID],[Name])VALUES(newid(),"+Project["Variables"]["PrimaryLocID"]+"','"+Project["Variables"]["Workstation"]+"')";

     

    P.S. For better visual clarity I prefer to use the aqString.Format() function. The above line in this case looks like this:

    query =
      aqString.Format("INSERT INTO %s.[dbo].[Workstations] (ID, [LocID], [Name]) VALUES (newid(), %s, %s)",
        ProjectSuite["Variables"]["PrimaryDB"],
        Project["Variables"]["PrimaryLocID"],
        Project["Variables"]["Workstation"] );
    

     

     

    • georgiadonut's avatar
      georgiadonut
      New Contributor

      Thank you very much Alex!  Worked perfectly and I also implemented aqString.Format() for readability.  Thanks again!

  • NisHera's avatar
    NisHera
    Valued Contributor

    before running queries first you have to connect to data base.

    There are several way to connect ..I use ADO components.

    You can refer  this 

     

    beware you have to provide correct connection string.

    You can build connection string as mentioned hear

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    Curiosity has me wondering... where are you getting the function newid () from? Is this from an article online, code already present in your project, something you have from a different tool? The context might be helpful in getting a good solution for you