Solved

sql 2005 query error

Posted on 2008-06-13
2
167 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

632 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