Solved

How to get sp_executesql result into a variable?

Posted on 2012-03-29
2
579 Views
Last Modified: 2012-06-27
Hi,
  I have a query like below. I want to get count(*) result into a variable when I execute the sql. Please let me know how can I do it.


SET @sql = '   SELECT count(*), (SELECT name from users where user_no='+CAST(@physicianID AS varchar(10))+') as physicianName,null,null,null,null,null,null,null,null,null,null,NULL,NULL from(
      SELECT
            examdate,
            patient_no
      FROM exams WHERE
          examdate>='''+LEFT(CONVERT(VARCHAR, @startDate, 112), 8)
+''' AND
          examdate<='''+LEFT(CONVERT(VARCHAR, @endDate, 112), 8) + ''''

IF @physicianID > 0
    SET @sql = @sql + ' AND
           requestedby = ' + CAST(@physicianID AS varchar(10))

SET @sql = @sql + '
        GROUP BY examdate,patient_no) AS SourceQuery'
EXEC sp_executesql @sql

Thank you
0
Comment
Question by:Sthokala
2 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 37783832
here is a sample

declare @sql nvarchar(max) = 'select @ResultOut = 3'
DECLARE @ParmDefinition nvarchar(500);
SET @ParmDefinition = N'@ResultOut integer OUTPUT';

declare @result integer
EXEC dbo.sp_executesql @sql, @ParmDefinition, @ResultOut=@result output;
print 'Result = ' + cast(@result as varchar)

>>>>

Result = 3

Open in new window


more info : http://msdn.microsoft.com/en-us/library/ms188001.aspx
0
 
LVL 14

Accepted Solution

by:
nishant joshi earned 500 total points
ID: 37786008
get your count using sp_executesql

declare @yourcount int
SET @sql = 'Declare @totcount int  SET @totcount=SELECT count(*), (SELECT name from users where user_no='+CAST(@physicianID AS varchar(10))+') as physicianName,null,null,null,null,null,null,null,null,null,null,NULL,NULL from(
      SELECT
            examdate,
            patient_no
      FROM exams WHERE
          examdate>='''+LEFT(CONVERT(VARCHAR, @startDate, 112), 8)
+''' AND
          examdate<='''+LEFT(CONVERT(VARCHAR, @endDate, 112), 8) + ''''

IF @physicianID > 0
    SET @sql = @sql + ' AND
           requestedby = ' + CAST(@physicianID AS varchar(10))

SET @sql = @sql + '
        GROUP BY examdate,patient_no) AS SourceQuery'
EXEC sp_executesql @sql,'totcount int OUTPUT',@yourcount output

Open in new window


Regards,
nishant
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

13 Experts available now in Live!

Get 1:1 Help Now