Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Problem with sql statement in vb script using variables.


Problem with sql statement in vb script using variables.

Author
Message
10e5x
10e5x
SSC-Enthusiastic
SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)

Group: General Forum Members
Points: 109 Visits: 180
Hi all,
I have been trying to resolve this for hours but failed, hence have to post here. I have this vbscript:

Private Function CheckNeedSendMail() As Integer
Dim startFilterDate As DateTime
Dim endFilterDate As DateTime
endFilterDate = CType(Dts.Variables("StgEndDate").Value, DateTime)
startFilterDate = endFilterDate.AddHours(-25)
Dim strStart As String = startFilterDate.ToString()
Dim strEnd As String = endFilterDate.ToString()


Dim sqlConnStr As String = "Data Source=" & RdVarStr("DB_Server_Name") & ";Initial Catalog=" & RdVarStr("DB_Init_Cat") & ";Integrated Security=True;"

Dim rowCounter As Integer
Using sqlConn As New Data.SqlClient.SqlConnection(sqlConnStr)
sqlConn.Open()
Using sqlCmd As New Data.SqlClient.SqlCommand("SELECT Count(*) from RPT_TACS_ACCESS_DTL where EXIT_DT >= " & startFilterDate & " and EXIT_DT <= " & endFilterDate & " and WORKED_HRS >= (select VALUE from TERMACCESS_CONFIG where MODULE = 'SSIS TACSReport' and [KEY]= 'Var_WorkHrs_Limit') +':00'", sqlConn)
Dim sqlDtReader As Data.SqlClient.SqlDataReader
sqlDtReader = sqlCmd.ExecuteReader()
While sqlDtReader.Read()
rowCounter = sqlDtReader.GetInt32(0)
End While
sqlDtReader.Close()
End Using
End Using
Return rowCounter

End Function



I keep hitting incorrect syntax near '7' and '+' when i dont even have this two value.... what went wrong?????
John Mitchell-245523
John Mitchell-245523
SSCertifiable
SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)

Group: General Forum Members
Points: 7414 Visits: 15114
Looks like it's to do with the way the command string is being built. Perphaps you could put a line in your code to print the command string before it's executed? What is the "+':00'" at the end for? If you're trying to append ":00" on to the end of a number, you need to do something like this:

SELECT CAST(COUNT(*) AS varchar(6)) + ':00'
...



John
10e5x
10e5x
SSC-Enthusiastic
SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)

Group: General Forum Members
Points: 109 Visits: 180
John Mitchell-245523 (1/3/2013)
Perphaps you could put a line in your code to print the command string before it's executed?

John


How do i do this?
John Mitchell-245523
John Mitchell-245523
SSCertifiable
SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)

Group: General Forum Members
Points: 7414 Visits: 15114
That's more a VB question than a SQL Server question, and outside my area of expertise. You could try using MsgBox.

John
10e5x
10e5x
SSC-Enthusiastic
SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)

Group: General Forum Members
Points: 109 Visits: 180
John Mitchell-245523 (1/3/2013)
What is the "+':00'" at the end for? If you're trying to append ":00" on to the end of a number, you need to do something like this:

SELECT CAST(COUNT(*) AS varchar(6)) + ':00'
...



John


Thanks for helping. The "+':00'" is to add :00 to my variable to make the query works. For example after using sub query if the Var_WorkHrs_Limit is 07, then it will query WORKED_HRS >= 07:00 I have been using this way for another similar method and no problem for the script. I do not think that is the cause.
10e5x
10e5x
SSC-Enthusiastic
SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)

Group: General Forum Members
Points: 109 Visits: 180
John Mitchell-245523 (1/3/2013)
That's more a VB question than a SQL Server question, and outside my area of expertise. You could try using MsgBox.

John


Oh i tried using msgbox to see whats wrong before posting here. MsgBox doesnt really helps pointing out where is the problem.
John Mitchell-245523
John Mitchell-245523
SSCertifiable
SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)

Group: General Forum Members
Points: 7414 Visits: 15114
10e5x (1/3/2013)
Oh i tried using msgbox to see whats wrong before posting here. MsgBox doesnt really helps pointing out where is the problem.

Why not? If you put this in your code:

MsgBox("SELECT Count(*) from RPT_TACS_ACCESS_DTL where EXIT_DT >= " & startFilterDate & " and EXIT_DT <= " & endFilterDate & " and WORKED_HRS >= (select VALUE from TERMACCESS_CONFIG where MODULE = 'SSIS TACSReport' and [KEY]= 'Var_WorkHrs_Limit') +':00'")


how does that not help? If it really doesn't, you'll need to do your own research to find out how to display the code some other way.

John
davidandrews13
davidandrews13
SSC Eights!
SSC Eights! (824 reputation)SSC Eights! (824 reputation)SSC Eights! (824 reputation)SSC Eights! (824 reputation)SSC Eights! (824 reputation)SSC Eights! (824 reputation)SSC Eights! (824 reputation)SSC Eights! (824 reputation)

Group: General Forum Members
Points: 824 Visits: 4450
do you have to put quotes round your dates?

instead of

where EXIT_DT >= " & startFilterDate & " and



you do

where EXIT_DT >= ' " & startFilterDate & " ' and


derek.colley
derek.colley
Mr or Mrs. 500
Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)

Group: General Forum Members
Points: 520 Visits: 603
Look at line 15, you are using two variables to represent your filter dates, but the string representation doesn't include the single quotes ' ' around the date. So you're putting together your SELECT clause without single quotes around the date variables. Try including the single quotes before and after each variable as part of the strings and see if that does the trick.

Your error message is simply due to SQL Server not being able to parse the line as 'single quotes' are required around values like these.


EDIT: Sorry davidandrews13, didn't spot your post above. Ditto that.

---

Note to developers:
CAST(SUBSTRING(CAST(FLOOR(NULLIF(ISNULL(COALESCE(1,NULL),NULL),NULL)) AS CHAR(1)),1,1) AS INT) == 1
So why complicate your code AND MAKE MY JOB HARDER??!Crazy

Want to get the best help? Click here http://www.sqlservercentral.com/articles/Best+Practices/61537/ (Jeff Moden)
My blog: http://uksqldba.blogspot.com
Visit http://www.DerekColley.co.uk to find out more about me.

Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16547 Visits: 17001
For an even better solution stop using dynamic pass through sql and do this with a stored proc. The advantages are numerous. You can avoid sql injection, debugging is a lot less difficult, changes to the data logic does not require a recompile and deployment of the software, the list goes on and on.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search