Solved

VBSCRIPT SQL Query issue Multiple Line Variable

Posted on 2014-04-11
7
1,443 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now