Solved

VBSCRIPT SQL Query issue Multiple Line Variable

Posted on 2014-04-11
7
1,545 Views
Last Modified: 2014-04-11
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
Comment
Question by:smyers051972
  • 4
  • 2
7 Comments
 
LVL 15

Accepted Solution

by:
gplana earned 500 total points
ID: 39994858
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
 
LVL 1

Author Comment

by:smyers051972
ID: 39994894
It still complained about a syntax error near )
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 39994909
The update line has a parenthesis at the end that is not needed.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 1

Author Comment

by:smyers051972
ID: 39994924
ok one sec :)
0
 
LVL 1

Author Comment

by:smyers051972
ID: 39994931
Didnt work could you repost code? sorry!
0
 
LVL 15

Expert Comment

by:gplana
ID: 39994968
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
 
LVL 1

Author Closing Comment

by:smyers051972
ID: 39995005
Thanks for the help!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 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