?
Solved

Use SQL Statment in Store procedure as a table

Posted on 2013-01-10
1
Medium Priority
?
302 Views
Last Modified: 2013-01-10
Hi Experts,

I have the below mentioned SQL statment, which I need to use in a store procedure as a table On the top to refer any where under the same SP. Please help


SELECT     TOP (100) PERCENT a.dt, a.dn, a.isholiday, a.holiday_name, b.EmpCode, b.EmpName,
                      CASE WHEN b.Direct = '1' THEN '1' ELSE '0' END AS Direct
FROM         dbo.AllDates a CROSS JOIN
             dbo.Emp_Info b
WHERE    b.CC=@CC and (b.Branch = 'KHI') and a.dt between @Dt and @Dt
and @Dt Between coalesce(JoiningDate,@Dt) and coalesce(RegsinedOn,@Dt)
ORDER BY a.dt, b.EmpCode
0
Comment
Question by:Mehram
[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
1 Comment
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 38762473
as it has parameters, you cannot refer to it as a table Unless you store the results in a table.
it may be a table variable:

declare @t table ( dt datetime, dn varchar(10) ... etc .. )
insert into @t select top (100) percent ... etc

and from there you can then use the @t table:

select * from @t

etc ..
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

770 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