Click here to Skip to main content
15,917,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to concat 2 sql statements?

Declaration in global:
Dim sqlSearch As String


Declaration in first function:
sqlSearch = " AND MONTH_T = '" + MonthSearch + "'"


Declaration in second function:
SQL
Dim sql1 As String = "SELECT DISTINCT MONTH_T FROM Q_VIEWREG WHERE CATEGORY_T = '" + Me.ddlSearchType.SelectedItem.Text + "' " & sqlSearch 


After debugging, this is the result that I get for sql1:
SELECT DISTINCT MONTH_T FROM Q_VIEWREG WHERE CATEGORY_T = 'INTERNAL'


It doesnt concat the second statement. Why does it happen?
Posted
Comments
graciax8 19-Jul-12 22:50pm    
how did you see that is doesn't concat the second statement?
snamyna 19-Jul-12 23:51pm    
the full statement shud be SELECT DISTINCT MONTH_T FROM Q_VIEWREG WHERE CATEGORY_T = 'INTERNAL' AND MONTH_T = '201205'
but i only get SELECT DISTINCT MONTH_T FROM Q_VIEWREG WHERE CATEGORY_T = 'INTERNAL'

I don't know, because I don't do VB, but it's a good thing it didn't. You NEVER build SQL this way, it leaves your code open to all sorts of attacks. Create paramaterised queries, or use stored procs. If anyone working for me built SQL like this, it would be instant dismissal.
 
Share this answer
 
Comments
snamyna 19-Jul-12 22:47pm    
Owh.. That wud be a good info. TQ Christian..
I would have a read of this article

Using SqlParameter Class[^].

As I would also support Christians statement of using parameterised queries as it removes the possibility of sql injection.
 
Share this answer
 
Do not use + sign on strings in VB you have to use &......
 
Share this answer
 
I just wrongly put the SQL statement in correct order. I have solved this. Thanks for your time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900