ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Call Stored Procedure in Select query in SQL Server<p>Hi <a class="username" rel="phonghue">phonghue</a>,</p> <p>You can not call a stored procedure from within a select query.</p> <p>First created a function that will return the count and then call it inside the select statement.</p> <p>Else check with the below query.</p> <pre class="brush: sql">SELECT AutoNum,OrgCode,OPTitle,OPPubDescrip,number1,COUNT(number1) AS number,OPMissionS FROM ( SELECT PROGRAMS.AutoNum, PROGRAMS.OrgCode, PROGRAMS.OPTitle, PROGRAMS.OPPubDescrip, CASE WHEN LEN(PROGRAMS.OPPubDescrip) &gt; 700 THEN 2 ELSE 1 END AS number1, PROGRAMS.OPMissionS FROM ORGANIZATIONS INNER JOIN PROGRAMS ON ORGANIZATIONS.OrgCode = PROGRAMS.OrgCode WHERE ORGANIZATIONS.Status = &#39;Approved&#39; AND PROGRAMS.OPStatus = &#39;Active&#39;) t GROUP BY AutoNum,OrgCode,OPTitle,OPPubDescrip,number1,OPMissionS ORDER BY OrgCode</pre> <p>Chack the sample query.</p> <p><strong><span style="text-decoration: underline;">SQL</span></strong></p> <pre class="brush: sql">SELECT Name,number1,COUNT(number1) Number FROM ( SELECT EmployeeID,FirstName + &#39; &#39; + LastName Name, CASE WHEN LEN(CONVERT(VARCHAR(MAX),Notes)) &gt; 200 THEN 2 ELSE 1 END AS number1 FROM Employees)t GROUP BY EmployeeID,Name,number1 ORDER BY EmployeeID</pre> <p><strong><span style="text-decoration: underline;">Output</span></strong></p> <table border="1"> <tbody> <tr> <td><strong>Name</strong></td> <td><strong>number1</strong></td> <td><strong>Number</strong></td> </tr> <tr> <td>Nancy Davolio</td> <td>1</td> <td>1</td> </tr> <tr> <td>Andrew Fuller</td> <td>2</td> <td>1</td> </tr> <tr> <td>Janet Leverling</td> <td>2</td> <td>1</td> </tr> <tr> <td>Margaret Peacock</td> <td>2</td> <td>1</td> </tr> <tr> <td>Steven Buchanan</td> <td>2</td> <td>1</td> </tr> <tr> <td>Michael Suyama</td> <td>2</td> <td>1</td> </tr> <tr> <td>Robert King</td> <td>2</td> <td>1</td> </tr> <tr> <td>Laura Callahan</td> <td>1</td> <td>1</td> </tr> <tr> <td>Anne Dodsworth</td> <td>1</td> <td>1</td> </tr> </tbody> </table> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/141358/Call-Stored-Procedure-in-Select-query-in-SQL-Server/https://www.aspforums.net:443/Threads/141358/Call-Stored-Procedure-in-Select-query-in-SQL-Server/Wed, 14 Aug 2019 07:12:56 GMT