Hi,

I'm unsure if this is the correct project to post this to but seeing as it appears to be SQL server specific I thought I would start here. It appears that after installing sqlsrv with Drupal 7, and then setting up the Views module (version 7.x-3.0-beta3), that some of the default Views do not work.

For example, enabling the 'Glossary' view causes the following:

Debug:
'Exception: SQLSTATE[07002]: [Microsoft][SQL Server Native Client 10.0]COUNT field incorrect or syntax error'
in views_plugin_query_default->execute() (line 1356 of C:\inetpub\sites\-\sites\all\modules\views\plugins\views_plugin_query_default.inc).
Error message


SQLSTATE[42000]: [Microsoft][SQL Server Native Client 10.0][SQL Server]Argument data type nvarchar is invalid for argument 3 of substring function.

I've run the SQL profiler while this is happening and found the following query being created:

exec sp_executesql N'SELECT COUNT(*) AS expression
FROM 
(SELECT 1 AS expression
FROM 
drupal_node node
INNER JOIN drupal_users users ON node.uid = users.uid
WHERE ( (( (SUBSTRING(node.title, 1, @P1) = @P2) )) )) subquery',N'@P1 nvarchar(1),@P2 nvarchar(1)',N'1',N'A'

It's easy to see here what is wrong; @P1 is being declared as nvarchar(1) which is incompatible with the type expected for the third parameter of SUBSTRING - it should be a numerical value. Simply changing @P1 to type int and running the query manually works.

I've only been playing with Drupal for about a week so I'm not at all up to play with it's internals yet.. I've had a big of a dig through the code but the DB abstractions make it difficult to find why this is happening without knowing a whole lot more about the codebase. Is this likely to be a sqlsrv issue or a Views issue?

Any help or tips would be great, I'll keep looking too and report back if I find anything. Cheers.

Comments

CapBBeard’s picture

Version: 7.x-1.x-dev » 7.x-1.0-rc3
Category: bug » support

Well I found the following on MSDN: http://social.msdn.microsoft.com/Forums/en-US/sqldriverforphp/thread/0f0...

This states that similar issues were seen on some versions of PHP (< 5.2.10, and 5.3.0), however I'm running on 5.3.6.

Anyway, I added a dirty fix into the Views module to always add a CAST(@P1 TO INT) in the last parameter of SUBSTRING, which has resolved the immediate issue (although it was more of a temporary quick fix for my own testing than an actual solution). However I'm now continuing to see problems.

It appears that the initial error message of

Debug:
'Exception: SQLSTATE[07002]: [Microsoft][SQL Server Native Client 10.0]COUNT field incorrect or syntax error'

in views_plugin_query_default->execute() (line 1354 of C:\inetpub\sites\*\sites\all\modules\views\plugins\views_plugin_query_default.inc).

Is in fact related to another query, not the one I had initially thought. This particular query does not appear in the SQL profiler so I'm assuming it doesn't even hit the SQL server but is rejected by the SQL driver [? just a guess, I'm no expert]. Dumping the query out with the exception reveals it's contents:

SELECT TOP(10) SUBSTRING(node.title, 1, CAST(:node_title2 AS INT)) AS title_truncated, COUNT(node.nid) AS num_records
FROM 
{node} node
GROUP BY SUBSTRING(node.title, 1, CAST(:node_title2 AS INT))
ORDER BY title_truncated ASC

After finding the following similar case here http://drupal.org/node/1057538 it seems that using the same placeholder twice causes this problem, due to it being technically invalid in PDO. So perhaps a Views bug?

damien tournoud’s picture

Version: 7.x-1.0-rc3 » 7.x-1.x-dev
Category: support » bug

Ew. This is unfortunate. We are expanding aliases in GROUP BY, because SQL Server cowardly refuses to do that itself, and this causes the duplicate use of the placeholders.

This is technically a bug in here. Triaging.

damien tournoud’s picture

Version: 7.x-1.0-rc3 » 7.x-1.x-dev
Category: support » bug
Priority: Normal » Critical

Raising to critical. I want to fix this before the new release that needs to happen soon to support Drupal 7.4.

damien tournoud’s picture

Priority: Critical » Major

Ok, it seems that the core issue:

Argument data type nvarchar is invalid for argument 3 of substring function.

Might actually be an issue somewhere down the stack (PDO driver or SQL Server), so I have released the 1.1 version without that fix.

damien tournoud’s picture

Reproduction test case:

error_reporting(-1);
$db = new PDO('sqlsrv:Server=localhost;Database=drupal', 'drupal', 'drupal');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

$stmt = $db->prepare('SELECT SUBSTRING(:title, 1, :title_size)');

$result = $stmt->execute(array(':title' => 'A title', ':title_size' => 3));
print_r($result);

Results in Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: [Microsoft][SQL Server Native Client 10.0][SQL Server]Argument data type nvarchar is invalid for argument 3 of substring function.'

artusamak’s picture

Title: SQL Query error with Views module - Argument data type nvarchar is invalid for argument 3 of substring function. » SUBSTRING() function must have integers for arg2 and arg3.
Status: Active » Needs review

Here is a patch to fix this issue with the SUBSTRING() casting.
Here is a link for quick review:
http://drupalcode.org/sandbox/Artusamak/1254272.git/commitdiff/3217e1a4e...

To check the code here is the repository url:

git remote add artusamak-1169202 git://git.drupal.org/sandbox/Artusamak/1254272.git
git fetch artusamak-1169202
git checkout -b artusamak-1169202 --track artusamak-1169202/artusamak-1169202
artusamak’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.