Solved

sql 2005 query error

Posted on 2008-06-13
2
163 Views
Last Modified: 2010-03-19
hi i have written a query when i parse the query it says everything is successfull,

but when i execute the query i am getting an error

my query is

declare @Tablebname varchar(50),@RecCount int
SET @Tablebname = 'OCRD';
EXEC ('select '+ @RecCount +  ' = Count(*) from ' + @Tablebname + ' where  cardType = "S"')

and the error i am getting is

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '='.
0
Comment
Question by:romeiovasu
2 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
ID: 21779676
You can't do that w/ EXEC...you have to use sp_executesql instead:

declare @Tablename varchar(50)
SET @Tablename = 'OCRD';
DECLARE @SQLString nvarchar(500);
DECLARE @ParmDefinition nvarchar(500);
DECLARE @RecCount INT;


SET @SQLString = N'SELECT @RecCount = COUNT(*)
   FROM ' + @TableName + '
   WHERE where  cardType = ''S'''
SET @ParmDefinition = N'@RecCount INT OUTPUT';

EXECUTE sp_executesql @SQLString, @ParmDefinition, @RecCount=@RecCount OUTPUT;
SELECT @RecCount;

0
 

Author Comment

by:romeiovasu
ID: 21780005
thanks
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Query Help - MSSQL - Averages 5 30
Downgrading MS SQL 2008 R2 Enterprise to MS SQL 2005 Standard? 12 68
Format Data Field - SQL 11 37
Syntax for query to update table 2 29
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question