Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Sql Like command help

Author  Topic 

Nightmare
Starting Member

8 Posts

Posted - 2006-12-27 : 15:09:03
Hi all

criteria = "Select * From Familias Where Print LIKE '"%text2%"'"

Can someone help me with this cmd? The error is on "%Text2%" part.

I dont know the sintax!

Email: Nightmare.gmr@gmail.com
MNS: Fjulr@hotmail.com

Thanks in advance

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2006-12-27 : 15:17:59
Is that line of code from an application? The final SQL query should be

Select * From Familias Where Print LIKE 'abc%'

The percent sign is a wild card, so that would give you all records that have the string abc... (starting with the letters abc, followed by anything) in them.
Go to Top of Page

Nightmare
Starting Member

8 Posts

Posted - 2006-12-27 : 16:47:38
Yes its part of an Visual Basic application where "Text2" is a text box...
If i put "Select * From Familias Where Print LIKE '%abc%'" it works.. but i need to read the value in that Text Box...

Thanks for the answer
Go to Top of Page

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2006-12-27 : 17:03:11
OK, so if it's VB, then it would be like this

criteria = "Select * From Familias Where Print LIKE '" & Text2.Text & "%'"

Note that this is open to a serious security problem known as a SQL Injection Attack. You should rather use a paramter in the SQL code and set the value of the parameter in your VB code. That would be something like

criteria = "Select * From Familias Where Print LIKE @Text"
cmd.CommandText = criteria
cmd.Parameters.Add(New SqlParameter('@Text', Text2.Text & "%"))
Go to Top of Page

Nightmare
Starting Member

8 Posts

Posted - 2006-12-27 : 17:22:21
Thanks again for answer...but still didnt working (no recordes found) I test with "Dreia" and there are 1 record.

Its just a sintax error but i dont know what to do.

Thanks
Go to Top of Page

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2006-12-27 : 17:27:29
So if you run this in Query Analyzer, do you get records

Select * From Familias Where Print LIKE 'Dreia%'

And if you run it in the VB code and you type Dreia (no quotes) in the text box, you get no records?
Go to Top of Page

Nightmare
Starting Member

8 Posts

Posted - 2006-12-27 : 17:31:27
yes
Go to Top of Page

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2006-12-27 : 17:37:59
OK, so if you put a breakpoint in your VB code right before it executes the SQL, what is the value of the command text? It should be exactly the same as the SQL statement that works in Query Analyzer, if it isn't then you have found your problem.

It's not a syntax error because a syntax error would be caught by the VB compiler or by SQL Server - the SQL statement that is being executed is not like the one that works in QA.
Go to Top of Page

Nightmare
Starting Member

8 Posts

Posted - 2006-12-27 : 17:41:16
Well i tried with this code: criteria = "Select * From Familias Where Print LIKE '" & Text2.Text & "%'" <---- No VB erros but no records found.

With this one:
criteria = "Select * From Familias Where Print LIKE @Text"
cmd.CommandText = criteria
cmd.Parameters.Add(New SqlParameter('@Text', Text2.Text & "%")) <--- VB give me a sintax error here

Thanks
Go to Top of Page

Nightmare
Starting Member

8 Posts

Posted - 2006-12-27 : 17:42:13
All code:

Private Sub Command2_Click()
Call dblidhja
With ar
criteria = "Select * From Familias Where Print LIKE @Text"
cmd.CommandText = criteria
cmd.Parameters.Add(New SqlParameter('@Text', Text2.Text & "%"))
If .RecordCount >= 1 Then
Text3.Text = !Familia
Text2.Text = ""
End If
End With
End Sub
Go to Top of Page

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2006-12-27 : 17:47:04
That's not all the code - what is ar? You never execute the command so certainly there are no records returned.

Are you using VB .NET or VB 6.0? The code I gave you won't work on VB 6.0, it's VB .NET code.
Go to Top of Page

Nightmare
Starting Member

8 Posts

Posted - 2006-12-27 : 17:52:13
Thats it im using VB 6.0

Thank you
Go to Top of Page

Nightmare
Starting Member

8 Posts

Posted - 2006-12-27 : 19:51:36
criteria = "Select * From Familias Where Print Like " & "'%" & Text2 & "%'"

Thanks for the help SnSQL
Go to Top of Page
   

- Advertisement -