Problem with sql statement in vb script using variables.

  • 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 = '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?????

  • 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

  • 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?

  • That's more a VB question than a SQL Server question, and outside my area of expertise. You could try using MsgBox.

    John

  • 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.

  • 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.

  • 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 = '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

  • do you have to put quotes round your dates?

    instead of

    where EXIT_DT >= " & startFilterDate & " and

    you do

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

  • 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 https://www.sqlservercentral.com/articles/forum-etiquette-how-to-post-datacode-on-a-forum-to-get-the-best-help (Jeff Moden)
    My blog: http://uksqldba.blogspot.com
    Visit http://www.DerekColley.co.uk to find out more about me.

  • 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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Thanks guys....the single quote works!!!!!!!!! So glad:) Love this forum!:-D

    Btw guys i am wrapping up my work after this. I am asked to prepare for a rerun scenario. I have thought of what to do but i am not sure how to make use of package variable in SQL statements inside the Execute SQL task. For now my plan would be, having a batch file to pass in a current DATE parameter to my package variable called ExecDate. Then i would like to use this ExecDate inside my Execute SQL task such as:

    Delete * from Table where CreationDate = @ExecDate.

    how can this be done in Execute Sql Task? Btw thanks for suggesting stored procedures, i know that's a better approach, i definitely agree but for this project i am kinda restraint from using stored procedure due to some security issue.

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply