Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sql 2005 query error

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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

670 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