Run query that concatenates parameters (@p) in Firebird

Asked

Viewed 1,944 times

2

I need to run a query where the value to be concatenated must be passed as a parameter for the query.

SELECT t.id || @p || t.nome FROM Test t;

But when executing this query it returns the following error: Dynamic SQL Error.

In Mysql this same idea works as follows:

SELECT CONCAT(t.id, @p, t.nome) FROM Test t; 

I wonder if there is any way to do this kind of concatenation in Firebird? Because from what I understood the operator || is what causes this problem when a parameter is used.

Obs¹: It is necessary that the value to be concatenated as separator is passed by parameter.

Obs²: I know that if I concatenate the value in the query it works, the query will be executed this way: select t.id || 'stringparaseparar' || t.nome from Test t;, but in my case as mentioned above it is necessary that this value is passed by parameter. Ex: set @p = 'stringparaseparar'.

To clarify better the reason for this need follow that other issue that describes the origin of this problem.

4 answers

1


In this case you should make one cast of the parameter to sweep:

SELECT t.id || cast(:p as varchar(10)) || t.nome FROM Test t;
  • Cool, that works as awesome as it sounds, hehe. The good thing is that it is possible to continue using parameters instead of concatenation which is somewhat bad for various issues (security, performance). I’m gonna change my solution that is currently forcing Nhibernate to concatenate rather than parametrize, to instruct him to do so. Thank you for your attention, I had already obtained several erroneous answers, that I no longer had hope that this was possible. rsrs

  • 1

    Yes. In this case you need to inform Firebird which type of parameter you are wanting to input the value. Note: This is a feature only available from version 2.0 of in Firebird. Reference in English

-1

for you to test this in IBEXPERT or even use inside your application Voce has to do a query of a Soted Procedure Voce has to pass to a STORED PROCEDURE

Your Select Origin Command of a precedent looks like this:

Select cidade From Busca_CEP("17504110")

create or alter procedure BUSCA_CEP (
    PCEP integer)
returns (
    cidade varchar(60)))
as
begin
   Select CEP.cidade From CEP into :cidade;
end

I think that solves your problem...

  • 1

    I don’t understand, what would this help to solve the problem? The problem is the concatenation of parameters in the select command.

  • You are absolutely right... I understood the wrong question... I’m sorry.. I can remove that answer

  • 1

    Okay, no problem, I thought I didn’t understand your answer, but still thanks for the attention and willingness to help.

-1

You should do it like this:

SELECT t.id || :p || t.nome FROM Test t;

and not:

SELECT t.id || @p || t.nome FROM Test t;
  • Doesn’t that seem to change anything here for me? It works for you in Firebird?

  • This parameter you are going through application ? which language ?

  • I executed SELECT t.id || :p || t.nome FROM Test t; here at Ibexpert and continues to occur the same problem of Dynamic SQL Error. There is something I am doing wrong?

  • Iboconsole, the same thing, and I’m running this query through . net, by the Nhibernate framework, in a Linq query(Only this select I posted is a subquery there in the context I use);

-1

I’m not sure I fully understand your question, but come on.

Table: Product

Code | Name | Complement 1 | Pencil | Black 2 | Pen| Blue

 Select Codigo, Nome, Complemento,  Nome|| ' COR' || Complemento as TudoJunto from Produto

Upshot:

1 | Pencil | Black | Lapis COLOR Black

2 | Pen| Blue | Pen COLOR Blue

Otherwise, post a comment that I edit here.

  • Ta but in the case of 'COLOR', it is not being passed by parameter, it is being made explicit in query. And my problem is in case it is passed by parameter.

  • @Fernando - Now I understand. Mark a minute there.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.