0

I am trying to run this query by it throws error when run i.e.

EXEC('Select Count(*) Total
      from '+@TableName+'
      where fk_Orgs_PropertyDetails_OrgID='+ 'Cast('+@OrgID+' as varchar')

Why this error I get?

INCORRECT SYNTAX NEAR VARCHAR    

Note: @TableName is Varchar(100) and @OrgID is int

3
  • whats the @OrgID value ? Commented Jun 16, 2016 at 6:16
  • Its just an ID that i am passing. Its datatype is INT Commented Jun 16, 2016 at 6:17
  • 3
    I hope that fk_Orgs_PropertyDetails_OrgID is int? If so, why cast to varchar? Commented Jun 16, 2016 at 6:20

4 Answers 4

8

You are missing a closing parenthesis in your call to CAST. Try this:

EXEC('Select Count(*) AS Total from ' + @TableName +
     ' where fk_Orgs_PropertyDetails_OrgID = Cast(' + @OrgID + ' as varchar)')

As @Felix pointed out, if your fk_Orgs_PropertyDetails_OrgID column is already an INT, then there is no need to cast @OrgID to VARCHAR. So just use the following:

EXEC('Select Count(*) AS Total from ' + @TableName +
     ' where fk_Orgs_PropertyDetails_OrgID = ' + @OrgID)
Sign up to request clarification or add additional context in comments.

1 Comment

I would add a suggestion to use sp_executesql instead.
4

As others have answered, you forgot a ) after the VARCHAR:

EXEC('Select Count(*) Total
      from '+@TableName+'
      where fk_Orgs_PropertyDetails_OrgID='+ 'Cast('+@OrgID+' as varchar)');

However, I suggest not using EXEC. Instead you should use sp_executesql. This has an additional benefit of providing more security by preventing SQL Injection.

DECLARE @TableName  VARCHAR(100),
        @OrgID      INT;

DECLARE @sql NVARCHAR(MAX);

SET @sql = 
'SELECT COUNT(*) Total 
FROM ' + QUOTENAME(@TableName) + '
WHERE fk_Orgs_PropertyDetails_OrgID = @OrgID';

EXEC sp_executesql
    @sql,
    N'@OrgID INT',
    @OrgID
;

The above assumes that fk_Orgs_PropertyDetails_OrgID is an INT.

Further reading on dynamic SQL:

7 Comments

If fk_Orgs_PropertyDetails_OrgID is already an INT then why not use fk_Orgs_PropertyDetails_OrgID = @OrgId directly and be done with it?
@TimBiegeleisen What do you mean? That's exactly what I've done in my sp_executesql solution.
Yes, but in this case the original call to EXEC should also have worked, right?
@TimBiegeleisen It still won't since you're concatenating a string (the query) with an int (the value of @OrgID).
@TimBiegeleisen I might be wrong though. SQL Server might implicitly convert the variable. This query is working: DECLARE @id INT = 1; EXEC ('SELECT * FROM(VALUES (1), (2)) t(Id) WHERE Id =' + @id);
|
2

In your query ")" is missing at last of CAST.

If @OrgID is defined varchar and fk_Orgs_PropertyDetails_OrgID is int then you dont need to use CAST. Execute Following

EXEC('Select Count(*) Total
from '+@TableName+'
where fk_Orgs_PropertyDetails_OrgID='+@OrgID)

If the column fk_Orgs_PropertyDetails_OrgID is varchar type. Execute Following

EXEC('Select Count(*) Total
from '+@TableName+'
where fk_Orgs_PropertyDetails_OrgID='''+@OrgID+'''')

Thanks

Comments

0

you are missing closing bracket

EXEC('Select Count(*) Total from '+ @TableName + ' where fk_Orgs_PropertyDetails_OrgID=' + 'Cast('+ @OrgID + ' as varchar)')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.