Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sql 2005 query error

Posted on 2008-06-13
2
Medium Priority
?
172 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 1500 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Screencast - Getting to Know the Pipeline

885 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