Solved

sql 2005 query error

Posted on 2008-06-13
2
157 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now