help with SQL Nvarchar

Asked

Viewed 142 times

1

Hello,

I had some problems with a code on SELECT...

example:

SELECT dados FROM banco WHERE CPF='$CPF' // com o código assim estava dando alguns erros pois o bd era em SQL e o Servidor windows.

The solution was:

SELECT CAST(CAST([dados] AS VARCHAR(8000)) AS TEXT) AS dados FROM dbo.banco WHERE CPF='$CPF;

I was wondering if there’s another simpler solution that doesn’t require such extensive code...

type, if I want to detail the table fields and use 4 fields, it only works if I put the CAST where is nVARCHAR. Note: I can not change in the bank nVARCHAR to just VARCHAR.

Thank you in advance!

  • 1

    It would be nice to report the error.

  • 1

    Which driver are you using to connect to SQL Server in PHP? I think you need to set the maximum size allowed for text (reference), otherwise the values are truncated.

  • No mistake buddy, just wanted a better solution... because the code gets too long...

  • "with the code so was giving some errors" (SELECT dados FROM banco WHERE CPF='$CPF'). Is there an error or is there not? If not, what’s the problem of using it like this (without CAST)?

  • It is that making the query with CAST requires a lot of the memory used by php(I think that’s it) and in some cases as the code gets very extensive, ends up presenting the following "Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 2184 bytes) in /home/Storage/6/C4/26/amb8/public_html/user/data.php on line 105" ... So I wanted a cleaner Cód too, i understand the basics of php so I’m complicating myself a little to explain rsrs ...

  • 1

    I still don’t understand why you don’t use SELECT dados FROM banco WHERE CPF='$CPF'

  • Which driver you can connect mssql, sqlsrv, PDO to?

  • Caffé use Cód this way no funfa... because in SQL the fields are in nVARCHAR and not only in VARCHAR... rray I am using "mssql" ... The Locaweb attendant gave me the following information: "You should then improve your select, which as you can notice makes several conversions and pulls several records, thus causing exhaustion...

  • 2

    @Ursones "in SQL the fields are in nVARCHAR and not only in VARCHAR" - That’s no reason to SELECT dados FROM banco WHERE CPF='$CPF' not work. What is missing is to say which unwanted behavior or error was obtained with this query so that it was forced to use conversions (CAST); thus we would help solve the real problem instead of the problem caused by a strange conversion (the TEXT type is deprecated in MSSQL). I was able to explain?

  • 1

    @Caffé Truth friend, vlw the explanation, in the end I changed php.ini pq the configuration was not authorizing the use of mssql... mssql.compatability_mode = Off, I switched to On And I didn’t have to CAST... Vlw for the tips and patience with me haha...

  • @Ursones No problem! You’re welcome.

  • @Ursones, put the procedure you did as a response, so it can help other people with the same problem :).

  • Okay and thank you all :D

Show 8 more comments

1 answer

1


In the thin of the accounts the solution was in the php.ini file, but why?

By doing the $query = mssql_query("SELECT SEXO, NOME, RG...) WHERE... gave an error because the use of the parameter MSSQL was turned off in the file "php.ini", searching the file I found the following line mssql.compatability_mode = Off I switched to On, I needed to use the CAST only in some fields nVarChar(max) that then made a mistake.

I guess that’s all and I hope to help someone who’s in the same situation.

Browser other questions tagged

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