Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot use CONTAINS(column, $parameter) in TSQL (sqlpage adds unsupported CAST as second argument to mssql contains function) #516

Open
prhc0612 opened this issue Jul 31, 2024 · 1 comment
Labels
bug Something isn't working enhancement New feature or request

Comments

@prhc0612
Copy link

prhc0612 commented Jul 31, 2024

What are you building with SQLPage ?

Building a page were a user can enter a "search term" and it queries a table for those terms.
The user would be able to create multiple input boxes, and specify "and / or" to build up a full search term.

What is your problem ? A description of the problem, not the solution you are proposing.
The problem is caused when SQLpage goes to append the $p1 value from the user input box, into my TSQL query. If I am using TSQL where col1 like '% :input1 %' in my query it works fine, but I am trying to use a full text index, with where contains(col1, :input1) the problem here is that in SQL Server you cannot use cast() in the 2nd parameter of contains.

What are you currently doing ? Since your solution is not implemented in SQLPage currently, what are you doing instead ?
Resorting to LIKe for now. but it doesn't allow the power to search that I need.

Describe the solution you'd like
When adding an input field, default it to text anyway, no need to then have code trying to input "cast". Or have different input fields, one for numeric (floats) one for strings (nvarchar) so the developer can specify the input box when building the .sql page.

@prhc0612 prhc0612 added the enhancement New feature or request label Jul 31, 2024
@lovasoa
Copy link
Collaborator

lovasoa commented Jul 31, 2024

Hello and welcome to SQLPage !

I understand your problem. Currently SQLPage adds a CAST around all variables in all cases in order to disambiguate queries for the db engine, but we should be able to remove the casts in some cases.

In the meantime, what you can do is define a database function to perform the search, and just call it from sqlpage:

CREATE FUNCTION dbo.SearchMyTable
(
    @SearchTerm NVARCHAR(100)
)
RETURNS TABLE
AS
RETURN
(
    SELECT *
    FROM my_table
    WHERE CONTAINS(text_column, @SearchTerm)
)
GO

and then in your sql, replace select * from my_table where contains(text_column, :input1) with SELECT * FROM dbo.SearchMyTable(:input1)

@lovasoa lovasoa changed the title Input boxes and data types cannot use CONTAINS(column, $parameter) in TSQL (sqlpage adds unsupported CAST as second argument to mssql contains function) Jul 31, 2024
@lovasoa lovasoa added the bug Something isn't working label Jul 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants