Solved

VBSCRIPT SQL Query issue Multiple Line Variable

Posted on 2014-04-11
7
1,654 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Progress

628 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