Solved

How to get sp_executesql result into a variable?

Posted on 2012-03-29
2
590 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

914 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

15 Experts available now in Live!

Get 1:1 Help Now