[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1825
  • Last Modified:

VBSCRIPT SQL Query issue Multiple Line Variable

Hello,

I have this script I wrote to track users when they log onto AD for a future project.  The SQL portion does a IF THEN ELSE scenario the issue is my vbs script is returning an error:

Incorrect Syntax near 'UPDATE logons SET Timestamp=GETDATE() WHERE Username='
Code: 80040E14

I checked the syntax of Line 27 which is the beginning of the query statement so I dont know which part I have wrong.

I attached the error and the VB script.

Thanks!
logons-error.vbs
vberror.png
0
smyers051972
Asked:
smyers051972
  • 4
  • 2
1 Solution
 
gplanaCommented:
I think this is the line which is not ok:

myCommand.CommandText = "IF EXISTS (SELECT * FROM logons (nolock) WHERE Username='" & UserString & "')'" &_
	+ " UPDATE logons SET Timestamp=GETDATE() WHERE Username='" & UserString & "')'" &_
	+ " ELSE" &_
	+ " INSERT INTO [logons] (Computername,Username,Timestamp,Action) VALUES ('" & DomainString & "','" & UserString & "', GETDATE(),'LOGON')"

Open in new window


I would do this:

myCommand.CommandText = "IF EXISTS (SELECT * FROM logons (nolock) WHERE Username='" & UserString & "')" &_
	+ " UPDATE logons SET Timestamp=GETDATE() WHERE Username='" & UserString & "')" &_
	+ " ELSE" &_
	+ " INSERT INTO [logons] (Computername,Username,Timestamp,Action) VALUES ('" & DomainString & "','" & UserString & "', GETDATE(),'LOGON')"

Open in new window

0
 
smyers051972Author Commented:
It still complained about a syntax error near )
0
 
Shaun KlineLead Software EngineerCommented:
The update line has a parenthesis at the end that is not needed.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
smyers051972Author Commented:
ok one sec :)
0
 
smyers051972Author Commented:
Didnt work could you repost code? sorry!
0
 
gplanaCommented:
Try to put the string on a variable, then print the variable, then try to execute directly on SQL-Server. This is the usual way to debug SQL queries
0
 
smyers051972Author Commented:
Thanks for the help!
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now